commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benedikt Ritter (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (BEANUTILS-481) [beanutils2] Support for nested properties and automatic conversion.
Date Fri, 06 Nov 2015 07:35:27 GMT

    [ https://issues.apache.org/jira/browse/BEANUTILS-481?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14993283#comment-14993283
] 

Benedikt Ritter commented on BEANUTILS-481:
-------------------------------------------

Hello [~mattmann],

thanks for splitting up the patch. I've worked some more on the Transformer API. I'm currently
thinking about a fluent API for registering Transformers. The getSourceType() and getTargetType()
Methods on the Transformer feel clumsy to me. Further more, if we update BeanUtils2 to Java
8, we can make use of lambda expressions. Here is what I've come up with, so far:

{code:java}
public class TransformerRegistryBuilder {

    public <S> SourceClassHolder<S> from(Class<S> sourceClass) {
        // TODO implement me
        return null;
    }

    public interface SourceClassHolder<S> {
        <T> TargetClassHolder<S, T> to(Class<T> toClass);
    }

    public interface TargetClassHolder<S, T> {
        void using(Function<S, T> transformer);
    }
}
{code}

An using the registry would look like:

{code:java}
builder.from(String.class).to(Integer.class).using(Integer::parseInt);
{code}

Further more, I think Transformers should not be registered on the BeanAcessor level. It would
be better if users could configure there own BeanUtils instances by passing in the transformers
the like:

{code:java}
BeanUtils.on(myBean).set("intProp").with("12"); // uses a default instance

BeanUtils.newInstance()
   .withDefaultTransformers().
   .withTransformer(from(String.class).to(Integer.class).using(myCustomStringToIntegerFunction))
   .build(); // returns a new instance with the default transformers but also the custom StringToIntegerFunction
{code}

What do you think?

BR,
Benedikt

> [beanutils2] Support for nested properties and automatic conversion.
> --------------------------------------------------------------------
>
>                 Key: BEANUTILS-481
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-481
>             Project: Commons BeanUtils
>          Issue Type: Improvement
>            Reporter: Matthew P Mann
>             Fix For: 2.0
>
>         Attachments: commons-beanutils2.conversion-only.patch, commons-beanutils2.patch
>
>
> Please consider the attached patch for the commons-beanutils2 project. I added support
for nested properties and automatic conversion. Excerpt from AutoConversionTest:
> {code}
> final DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
> final TransformerRegistry transformerRegistry = new TransformerRegistry()
>     .register(new StringToDate(dateFormat))
>     .register(new IntegerToString())
>     .register(new StringToColor())
>     .register(new IntegerToColor())
>     .register(new StringToURL())
>     .register(new StringToPhoneNumber());
> final PhoneNumber phoneNumber = new PhoneNumber();
> phoneNumber.setAreaCode("202");
> phoneNumber.setPrefix("456");
> phoneNumber.setLineNumber("1111");
> final Address address = new Address();
> address.setStreetAddress("1600 Pennsylvania Avenue Northwest");
> address.setCity("Washington");
> address.setStateCode("DC");
> address.setPostalCode("20500");
> address.setCountryCode("US");
> final Person person = new Person();
> person.setFirstName("Barack");
> person.setLastName("Obama");
> person.setBirthDate(dateFormat.parse("August 4, 1961"));
> person.setEyeColor(Color.decode("#362819"));
> person.setHairColor(GRAY);
> person.setPhoneNumber(phoneNumber);
> person.setAddress(address);
> person.setWebsite(new URL("https://www.barackobama.com/"));
> assertEquals(person, on(new Person(), transformerRegistry)
>     .set("firstName").with("Barack")
>     .set("lastName").with("Obama")
>     .set("birthDate").with("August 4, 1961")
>     .set("hairColor").with(0x808080)
>     .set("eyeColor").with("#362819")
>     .set("website").with("https://www.barackobama.com/")
>     .set("phoneNumber").with("202-456-1111")
>     .set("address").with(new Address())
>     .set("address.streetAddress").with("1600 Pennsylvania Avenue Northwest")
>     .set("address.city").with("Washington")
>     .set("address.stateCode").with("DC")
>     .set("address.postalCode").with(20500)
>     .set("address.countryCode").with("US")
>     .get());
> {code}
> Thanks,
> Matt



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message