You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Matthew P Mann (JIRA)" <ji...@apache.org> on 2015/11/08 21:56:11 UTC

[jira] [Updated] (SANDBOX-501) Add configurable type conversion support

     [ https://issues.apache.org/jira/browse/SANDBOX-501?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Matthew P Mann updated SANDBOX-501:
-----------------------------------
    Attachment: commons-beanutils2.java8.patch

Benedikt,

What do you think of this patch?

{code}
@Test
public void test() throws MalformedURLException, ParseException {
    final DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
    final BeanUtils beanUtils = BeanUtils
        .builder()
        .withDefaultTransformers()
        .withTransformer(MethodTransformer.from(String.class).to(Date.class).using(dateFormat, "parse"))
        .withTransformer(FunctionTransformer.from(String.class).to(PhoneNumber.class).using(PhoneNumber::parse))
        .withTransformer(FunctionTransformer.from(String.class).to(State.class).using(State::valueOf))
        .build();
    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(State.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,
        beanUtils
            .bean(new Person())
            .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(
                beanUtils
                    .bean(new Address())
                    .set("streetAddress").with("1600 Pennsylvania Avenue Northwest")
                    .set("city").with("Washington")
                    .set("stateCode").with("DC")
                    .set("postalCode").with(20500)
                    .set("countryCode").with("US")
                    .get()
            )
            .get()
    );
}
{code}

-Matt

> Add configurable type conversion support
> ----------------------------------------
>
>                 Key: SANDBOX-501
>                 URL: https://issues.apache.org/jira/browse/SANDBOX-501
>             Project: Commons Sandbox
>          Issue Type: New Feature
>          Components: BeanUtils2
>            Reporter: Benedikt Ritter
>         Attachments: commons-beanutils2.java8.patch
>
>
> BeanUtils1 supports automatic type conversion when setting properties. This should be added to BeanUtils2. 
> A problem of the implementation of BeanUtils1 is, that the registry of converts is cached in the BeanUtils class. BeanUtils2 should provide an API for creating new instances of the o.a.c.beanutils2.BeanUtils with a customized typ conversion registry.



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