You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ofbiz.apache.org by Adrian Crum <ad...@hlmksw.com> on 2009/11/05 01:08:56 UTC

Discussion: Improved Java Object Type Conversion

I have an idea for improved Java object type conversion (converting from 
one Java object type to another), and I would like to solicit comments 
from the community.

Currently, the project handles object type conversions in the 
ObjectType.simpleTypeConvert(...) method, and there are small conversion 
code snippets in other classes (UtilDateTime for example). Basically, 
object type conversion is scattered all over the project. This can lead 
to code inconsistency, or inconsistent conversion results.

I think it would be helpful to consolidate object type conversion into a 
simple "framework" that can be easily extended to handle additional 
conversions.

I came up with a simple interface:

public interface Converter<T, F> {
     public T to(F obj);
}

where T is the object type to convert to, and F is the object type to 
convert from.

A factory class is used to get a Converter instance:

public class ConverterFactory {
     public static <T, F> Converter<T, F> getConverter(Class<T> toClass, 
Class<F> fromClass) {}
}

To convert an object:

String str = "1234";
Converter<Long, String> converter = ConverterFactory 
.getConverter(Long.class, String.class);
Long result = converter.to(str);

The framework would include converter implementations for all the 
conversions currently being used. The ObjectType.simpleTypeConvert(...) 
method's complicated if-else code would be replaced with the simpler 
converter code.

Users can write their own converters and "register" them with the 
converter framework.

What do you think?

-Adrian

Re: Discussion: Improved Java Object Type Conversion

Posted by Jacques Le Roux <ja...@les7arts.com>.
Seems convenient at 1st glance

Jacques

From: "Adrian Crum" <ad...@hlmksw.com>
>I have an idea for improved Java object type conversion (converting from 
> one Java object type to another), and I would like to solicit comments 
> from the community.
> 
> Currently, the project handles object type conversions in the 
> ObjectType.simpleTypeConvert(...) method, and there are small conversion 
> code snippets in other classes (UtilDateTime for example). Basically, 
> object type conversion is scattered all over the project. This can lead 
> to code inconsistency, or inconsistent conversion results.
> 
> I think it would be helpful to consolidate object type conversion into a 
> simple "framework" that can be easily extended to handle additional 
> conversions.
> 
> I came up with a simple interface:
> 
> public interface Converter<T, F> {
>     public T to(F obj);
> }
> 
> where T is the object type to convert to, and F is the object type to 
> convert from.
> 
> A factory class is used to get a Converter instance:
> 
> public class ConverterFactory {
>     public static <T, F> Converter<T, F> getConverter(Class<T> toClass, 
> Class<F> fromClass) {}
> }
> 
> To convert an object:
> 
> String str = "1234";
> Converter<Long, String> converter = ConverterFactory 
> .getConverter(Long.class, String.class);
> Long result = converter.to(str);
> 
> The framework would include converter implementations for all the 
> conversions currently being used. The ObjectType.simpleTypeConvert(...) 
> method's complicated if-else code would be replaced with the simpler 
> converter code.
> 
> Users can write their own converters and "register" them with the 
> converter framework.
> 
> What do you think?
> 
> -Adrian
>


Re: Discussion: Improved Java Object Type Conversion

Posted by Adrian Crum <ad...@yahoo.com>.
I suppose we could collaborate on it like we did with UEL. When I have it finished I'll post how-tos on the mailing list. We can go from there in the Wiki.

-Adrian

--- On Sun, 11/8/09, Jacques Le Roux <ja...@les7arts.com> wrote:

> From: Jacques Le Roux <ja...@les7arts.com>
> Subject: Re: Discussion: Improved Java Object Type Conversion
> To: dev@ofbiz.apache.org
> Date: Sunday, November 8, 2009, 1:11 AM
> Thanks Adrian,
> 
> This will be really useful. Do you plan to create a small
> doc wiht examples (I may help) or only API doc ?
> 
> Jacques
> 
> From: "Adrian Crum" <ad...@yahoo.com>
> >I just checked in the new conversion code. Some work
> still needs to be done on it.
> >
> > When it is finished, users will be able to introduce
> custom Java object types into the framework. This will make
> the framework 
> > more flexible and extensible.
> >
> > Using Harmeet's phone number example, we will be able
> to do things like:
> >
> > <entity-one entity-name="TelecomNumber"
> value-field="phoneNumberValue"/>
> > <set field="phoneNumber"
> from-field="phoneNumberValue" type="PhoneNumber"/>
> > <!-- Do something with phoneNumber -->
> >
> > -Adrian
> >
> >
> > --- On Wed, 11/4/09, Adrian Crum <ad...@hlmksw.com>
> wrote:
> >
> >> From: Adrian Crum <ad...@hlmksw.com>
> >> Subject: Discussion: Improved Java Object Type
> Conversion
> >> To: dev@ofbiz.apache.org
> >> Date: Wednesday, November 4, 2009, 4:08 PM
> >> I have an idea for improved Java
> >> object type conversion (converting from one Java
> object type
> >> to another), and I would like to solicit comments
> from the
> >> community.
> >>
> >> Currently, the project handles object type
> conversions in
> >> the ObjectType.simpleTypeConvert(...) method, and
> there are
> >> small conversion code snippets in other classes
> >> (UtilDateTime for example). Basically, object
> type
> >> conversion is scattered all over the project. This
> can lead
> >> to code inconsistency, or inconsistent conversion
> results.
> >>
> >> I think it would be helpful to consolidate object
> type
> >> conversion into a simple "framework" that can be
> easily
> >> extended to handle additional conversions.
> >>
> >> I came up with a simple interface:
> >>
> >> public interface Converter<T, F> {
> >> public T to(F obj);
> >> }
> >>
> >> where T is the object type to convert to, and F is
> the
> >> object type to convert from.
> >>
> >> A factory class is used to get a Converter
> instance:
> >>
> >> public class ConverterFactory {
> >> public static <T, F> Converter<T,
> >> F> getConverter(Class<T> toClass,
> Class<F>
> >> fromClass) {}
> >> }
> >>
> >> To convert an object:
> >>
> >> String str = "1234";
> >> Converter<Long, String> converter =
> ConverterFactory
> >> .getConverter(Long.class, String.class);
> >> Long result = converter.to(str);
> >>
> >> The framework would include converter
> implementations for
> >> all the conversions currently being used. The
> >> ObjectType.simpleTypeConvert(...) method's
> complicated
> >> if-else code would be replaced with the simpler
> converter
> >> code.
> >>
> >> Users can write their own converters and
> "register" them
> >> with the converter framework.
> >>
> >> What do you think?
> >>
> >> -Adrian
> >>
> >
> >
> >
> > 
> 
> 
> 


      

Re: Discussion: Improved Java Object Type Conversion

Posted by Jacques Le Roux <ja...@les7arts.com>.
Thanks Adrian,

This will be really useful. Do you plan to create a small doc wiht examples (I may help) or only API doc ?

Jacques

From: "Adrian Crum" <ad...@yahoo.com>
>I just checked in the new conversion code. Some work still needs to be done on it.
>
> When it is finished, users will be able to introduce custom Java object types into the framework. This will make the framework 
> more flexible and extensible.
>
> Using Harmeet's phone number example, we will be able to do things like:
>
> <entity-one entity-name="TelecomNumber" value-field="phoneNumberValue"/>
> <set field="phoneNumber" from-field="phoneNumberValue" type="PhoneNumber"/>
> <!-- Do something with phoneNumber -->
>
> -Adrian
>
>
> --- On Wed, 11/4/09, Adrian Crum <ad...@hlmksw.com> wrote:
>
>> From: Adrian Crum <ad...@hlmksw.com>
>> Subject: Discussion: Improved Java Object Type Conversion
>> To: dev@ofbiz.apache.org
>> Date: Wednesday, November 4, 2009, 4:08 PM
>> I have an idea for improved Java
>> object type conversion (converting from one Java object type
>> to another), and I would like to solicit comments from the
>> community.
>>
>> Currently, the project handles object type conversions in
>> the ObjectType.simpleTypeConvert(...) method, and there are
>> small conversion code snippets in other classes
>> (UtilDateTime for example). Basically, object type
>> conversion is scattered all over the project. This can lead
>> to code inconsistency, or inconsistent conversion results.
>>
>> I think it would be helpful to consolidate object type
>> conversion into a simple "framework" that can be easily
>> extended to handle additional conversions.
>>
>> I came up with a simple interface:
>>
>> public interface Converter<T, F> {
>> public T to(F obj);
>> }
>>
>> where T is the object type to convert to, and F is the
>> object type to convert from.
>>
>> A factory class is used to get a Converter instance:
>>
>> public class ConverterFactory {
>> public static <T, F> Converter<T,
>> F> getConverter(Class<T> toClass, Class<F>
>> fromClass) {}
>> }
>>
>> To convert an object:
>>
>> String str = "1234";
>> Converter<Long, String> converter = ConverterFactory
>> .getConverter(Long.class, String.class);
>> Long result = converter.to(str);
>>
>> The framework would include converter implementations for
>> all the conversions currently being used. The
>> ObjectType.simpleTypeConvert(...) method's complicated
>> if-else code would be replaced with the simpler converter
>> code.
>>
>> Users can write their own converters and "register" them
>> with the converter framework.
>>
>> What do you think?
>>
>> -Adrian
>>
>
>
>
> 



Re: Discussion: Improved Java Object Type Conversion

Posted by Adrian Crum <ad...@yahoo.com>.
I just checked in the new conversion code. Some work still needs to be done on it.

When it is finished, users will be able to introduce custom Java object types into the framework. This will make the framework more flexible and extensible.

Using Harmeet's phone number example, we will be able to do things like:

<entity-one entity-name="TelecomNumber" value-field="phoneNumberValue"/>
<set field="phoneNumber" from-field="phoneNumberValue" type="PhoneNumber"/>
<!-- Do something with phoneNumber -->

-Adrian


--- On Wed, 11/4/09, Adrian Crum <ad...@hlmksw.com> wrote:

> From: Adrian Crum <ad...@hlmksw.com>
> Subject: Discussion: Improved Java Object Type Conversion
> To: dev@ofbiz.apache.org
> Date: Wednesday, November 4, 2009, 4:08 PM
> I have an idea for improved Java
> object type conversion (converting from one Java object type
> to another), and I would like to solicit comments from the
> community.
> 
> Currently, the project handles object type conversions in
> the ObjectType.simpleTypeConvert(...) method, and there are
> small conversion code snippets in other classes
> (UtilDateTime for example). Basically, object type
> conversion is scattered all over the project. This can lead
> to code inconsistency, or inconsistent conversion results.
> 
> I think it would be helpful to consolidate object type
> conversion into a simple "framework" that can be easily
> extended to handle additional conversions.
> 
> I came up with a simple interface:
> 
> public interface Converter<T, F> {
>     public T to(F obj);
> }
> 
> where T is the object type to convert to, and F is the
> object type to convert from.
> 
> A factory class is used to get a Converter instance:
> 
> public class ConverterFactory {
>     public static <T, F> Converter<T,
> F> getConverter(Class<T> toClass, Class<F>
> fromClass) {}
> }
> 
> To convert an object:
> 
> String str = "1234";
> Converter<Long, String> converter = ConverterFactory
> .getConverter(Long.class, String.class);
> Long result = converter.to(str);
> 
> The framework would include converter implementations for
> all the conversions currently being used. The
> ObjectType.simpleTypeConvert(...) method's complicated
> if-else code would be replaced with the simpler converter
> code.
> 
> Users can write their own converters and "register" them
> with the converter framework.
> 
> What do you think?
> 
> -Adrian
>