commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matthew P Mann (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (SANDBOX-501) Add configurable type conversion support
Date Sun, 15 Nov 2015 21:05:10 GMT

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

Matthew P Mann edited comment on SANDBOX-501 at 11/15/15 9:04 PM:
------------------------------------------------------------------

... and more tinkering. See [attached patch|https://issues.apache.org/jira/secure/attachment/12772417/commons-beanutils2.2015-11-15.patch].

{code}
public class StringToArrayTest {

    private StringToArray converter;
    private ConverterRegistry converterRegistry;

    @Before
    public void setUp() {
        converter = new StringToArray();
        converterRegistry = new ConverterRegistry()
            .register(converter)
            .register(new JsonNumberConverter())
            .register(new JsonStringConverter())
            .register(new JsonArrayToArray())
            .register(BigDecimal::toBigIntegerExact)
            .register(Address::parse)
            .register(FunctionConverter.from(BigDecimal.class).to(double.class).using(BigDecimal::doubleValue))
            .register(FunctionConverter.from(BigDecimal.class).to(int.class).using(BigDecimal::intValueExact))
            .register(FunctionConverter.from(String.class).to(BigDecimal.class).using(BigDecimal::new))
            .register(FunctionConverter.from(String.class).to(char.class).using(string ->
{
                if (length(string) != 1) {
                    throw new IllegalArgumentException(string);
                }
                return string.charAt(0);
            }));
    }

    @Test
    public void convert_StringToAddressArray() {
        final String source = "[ \"1600 Pennsylvania Avenue Northwest, Washington, DC  20500\",
\"11 Wall Street, New York, NY  10005, US\", \"350 Fifth Avenue, New York, NY  10118\", \"4059
Mt Lee Drive, Hollywood, CA  90068\"]";
        assertTrue(converter.canConvert(String.class, Address[].class));
        final Address[] addresses = converter.convert(source, Address[].class, converterRegistry);
        assertEquals(4, addresses.length);
        assertEquals("1600 Pennsylvania Avenue Northwest", addresses[0].getStreetAddress());
        assertEquals("Washington", addresses[0].getCity());
        assertEquals(State.DC, addresses[0].getStateCode());
        assertEquals("20500", addresses[0].getPostalCode());
        assertNull(addresses[0].getCountryCode());
        assertEquals("11 Wall Street", addresses[1].getStreetAddress());
        assertEquals("New York", addresses[1].getCity());
        assertEquals(State.NY, addresses[1].getStateCode());
        assertEquals("10005", addresses[1].getPostalCode());
        assertEquals("US", addresses[1].getCountryCode());
        assertEquals("350 Fifth Avenue", addresses[2].getStreetAddress());
        assertEquals("New York", addresses[2].getCity());
        assertEquals(State.NY, addresses[2].getStateCode());
        assertEquals("10118", addresses[2].getPostalCode());
        assertNull(addresses[2].getCountryCode());
        assertEquals("4059 Mt Lee Drive", addresses[3].getStreetAddress());
        assertEquals("Hollywood", addresses[3].getCity());
        assertEquals(State.CA, addresses[3].getStateCode());
        assertEquals("90068", addresses[3].getPostalCode());
        assertNull(addresses[3].getCountryCode());
    }

...

}
{code}


was (Author: mattmann):
... and more tinkering. See [attached patch|https://issues.apache.org/jira/secure/attachment/12772417/commons-beanutils2.2015-11-15.patch].

{code}
public class StringToArrayTest {

	private StringToArray converter;
	private ConverterRegistry converterRegistry;

	@Before
	public void setUp() {
		converter = new StringToArray();
		converterRegistry = new ConverterRegistry()
			.register(converter)
			.register(new JsonNumberConverter())
			.register(new JsonStringConverter())
			.register(new JsonArrayToArray())
			.register(BigDecimal::toBigIntegerExact)
			.register(Address::parse)
			.register(FunctionConverter.from(BigDecimal.class).to(double.class).using(BigDecimal::doubleValue))
			.register(FunctionConverter.from(BigDecimal.class).to(int.class).using(BigDecimal::intValueExact))
			.register(FunctionConverter.from(String.class).to(BigDecimal.class).using(BigDecimal::new))
			.register(FunctionConverter.from(String.class).to(char.class).using(string -> {
				if (length(string) != 1) {
					throw new IllegalArgumentException(string);
				}
				return string.charAt(0);
			}));
	}

	@Test
	public void convert_StringToAddressArray() {
		final String source = "[ \"1600 Pennsylvania Avenue Northwest, Washington, DC  20500\",
\"11 Wall Street, New York, NY  10005, US\", \"350 Fifth Avenue, New York, NY  10118\", \"4059
Mt Lee Drive, Hollywood, CA  90068\"]";
		assertTrue(converter.canConvert(String.class, Address[].class));
		final Address[] addresses = converter.convert(source, Address[].class, converterRegistry);
		assertEquals(4, addresses.length);
		assertEquals("1600 Pennsylvania Avenue Northwest", addresses[0].getStreetAddress());
		assertEquals("Washington", addresses[0].getCity());
		assertEquals(State.DC, addresses[0].getStateCode());
		assertEquals("20500", addresses[0].getPostalCode());
		assertNull(addresses[0].getCountryCode());
		assertEquals("11 Wall Street", addresses[1].getStreetAddress());
		assertEquals("New York", addresses[1].getCity());
		assertEquals(State.NY, addresses[1].getStateCode());
		assertEquals("10005", addresses[1].getPostalCode());
		assertEquals("US", addresses[1].getCountryCode());
		assertEquals("350 Fifth Avenue", addresses[2].getStreetAddress());
		assertEquals("New York", addresses[2].getCity());
		assertEquals(State.NY, addresses[2].getStateCode());
		assertEquals("10118", addresses[2].getPostalCode());
		assertNull(addresses[2].getCountryCode());
		assertEquals("4059 Mt Lee Drive", addresses[3].getStreetAddress());
		assertEquals("Hollywood", addresses[3].getCity());
		assertEquals(State.CA, addresses[3].getStateCode());
		assertEquals("90068", addresses[3].getPostalCode());
		assertNull(addresses[3].getCountryCode());
	}

...

}
{code}

> 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.2015-11-13.patch, commons-beanutils2.2015-11-14.patch,
commons-beanutils2.2015-11-15.patch, 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)

Mime
View raw message