You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by b p <tr...@yahoo.com> on 2004/04/16 23:00:21 UTC
[betwixt] Problem with Maps and Arrays
Hi Robert,
I've hit a problem with Maps and arrays that I haven't been able to resolve. I would like to write/read a Map that has an array (either java array or collection) as the values of the map. When I write, everything looks ok. When I read back in, I'm getting a map with no values. If I wrap the array or collection in another bean (so the value of the map is a bean, not an array or collection), it will work. I can live with that if I need to, but I would prefer to have the map values be arrays or collections directly. I haven't been able to track down a good way to do that-- I'm hoping you'll see a relatively easy solution I'm missing :).
Below is a test case that shows the problem. Any suggestions?
Thanks,
Brian
AddressBook.java
---------------------------------
package org.apache.commons.betwixt.io.read;
import java.util.HashMap;
/**
* @author Brian Pugh
*/
public class AddressBook {
private HashMap addressesMap = new HashMap();
public HashMap getAddressBookItems() {
return addressesMap;
}
public void setAddressBookItems(HashMap map) {
this.addressesMap = map;
}
public void addAddressBookItem(String name, AddressBean[] addresses) {
addressesMap.put(name, addresses);
}
}
Test case (patch to TestMaps.java)
----------------------------------------------------
Index: src/test/org/apache/commons/betwixt/io/read/TestMaps.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/io/read/Attic/TestMaps.java,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 TestMaps.java
--- src/test/org/apache/commons/betwixt/io/read/TestMaps.java 22 Feb 2004 17:09:29 -0000 1.1.2.1
+++ src/test/org/apache/commons/betwixt/io/read/TestMaps.java 16 Apr 2004 20:46:55 -0000
@@ -132,4 +132,60 @@
}
+ public void testMapWithArray() throws Exception {
+
+ AddressBook addressBook = new AddressBook();
+ AddressBean[] johnsAddresses = new AddressBean[2];
+ johnsAddresses[0] = new AddressBean("12 here", "Chicago", "USA", "1234");
+ johnsAddresses[1] = new AddressBean("333 there", "Los Angeles", "USA", "99999");
+ String name = "John";
+ addressBook.addAddressBookItem(name, johnsAddresses);
+ StringWriter outputWriter = new StringWriter();
+ outputWriter.write("<?xml version='1.0' ?>\n");
+ BeanWriter beanWriter = new BeanWriter(outputWriter);
+ beanWriter.enablePrettyPrint();
+ beanWriter.write(addressBook);
+ System.out.println(outputWriter.toString());
+ String xml = "<?xml version='1.0' ?>\n" +
+ " <AddressBook id=\"1\">\n" +
+ " <addressBookItems>\n" +
+ " <entry id=\"2\">\n" +
+ " <key>John</key>\n" +
+ " <value id=\"3\">\n" +
+ " <AddressBean id=\"4\">\n" +
+ " <city>Chicago</city>\n" +
+ " <code>1234</code>\n" +
+ " <country>USA</country>\n" +
+ " <street>12 here</street>\n" +
+ " </AddressBean>\n" +
+ " <AddressBean id=\"5\">\n" +
+ " <city>Los Angeles</city>\n" +
+ " <code>99999</code>\n" +
+ " <country>USA</country>\n" +
+ " <street>333 there</street>\n" +
+ " </AddressBean>\n" +
+ " </value>\n" +
+ " </entry>\n" +
+ " </addressBookItems>\n" +
+ " </AddressBook>\n";
+
+ assertEquals(xml, outputWriter.toString());
+ BeanReader reader = new BeanReader();
+ reader.registerBeanClass(AddressBook.class);
+ StringReader xmlReader = new StringReader(outputWriter.toString());
+ AddressBook result = (AddressBook) reader.parse(xmlReader);
+ assertNotNull("Expected to get an AddressBook!", result);
+ assertNotNull("Expected AddressBook to have some address entryitems!", result.getAddressBookItems());
+ AddressBean []resultAddresses = (AddressBean[]) result.getAddressBookItems().get(name);
+ assertNotNull("Expected to have some addresses for " + name, resultAddresses);
+ assertEquals("Got wrong city in first address for " + name,
+ johnsAddresses[0].getCity(),
+ resultAddresses[0].getCity());
+ assertEquals("Got wrong city in second address for " + name,
+ johnsAddresses[1].getCity(),
+ resultAddresses[1].getCity());
+
+
+ }
+
}
---------------------------------
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
Re: [betwixt] Problem with Maps and Arrays
Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
hi brian
you're right: this is quite an interesting problem :)
the tricky part is that i think that an intermediary list is going to
be needed (which will be converted into the array when the element
ends).
i've been look at some refactoring and design improvements today which
should help. i've pushed more functionality from the actions into the
context and simplified the read context API. i hope that i'll be able
to add an updater stack to the context and allow actions to push and
pop updaters. this will allow intermediary objects to be transparently
supported. once this is done, i should be able to use a special action
implementation to set everything up and make the call to the method at
the end. (anyway, this is my plan and it's probably subject to change
if anyone comes up with a better one. as usual, if anyone has any ideas
for better ways to do this, please jump in.)
but unfortunately, i don't think that i'll have the time to complete
this tonight...
- robert
On 16 Apr 2004, at 22:00, b p wrote:
> Hi Robert,
>
> I've hit a problem with Maps and arrays that I haven't been able to
> resolve. I would like to write/read a Map that has an array (either
> java array or collection) as the values of the map. When I write,
> everything looks ok. When I read back in, I'm getting a map with no
> values. If I wrap the array or collection in another bean (so the
> value of the map is a bean, not an array or collection), it will work.
> I can live with that if I need to, but I would prefer to have the map
> values be arrays or collections directly. I haven't been able to
> track down a good way to do that-- I'm hoping you'll see a relatively
> easy solution I'm missing :).
>
> Below is a test case that shows the problem. Any suggestions?
>
> Thanks,
> Brian
>
>
> AddressBook.java
> ---------------------------------
> package org.apache.commons.betwixt.io.read;
> import java.util.HashMap;
> /**
> * @author Brian Pugh
> */
> public class AddressBook {
> private HashMap addressesMap = new HashMap();
>
> public HashMap getAddressBookItems() {
> return addressesMap;
> }
> public void setAddressBookItems(HashMap map) {
> this.addressesMap = map;
> }
> public void addAddressBookItem(String name, AddressBean[] addresses)
> {
> addressesMap.put(name, addresses);
> }
>
> }
>
>
> Test case (patch to TestMaps.java)
> ----------------------------------------------------
>
> Index: src/test/org/apache/commons/betwixt/io/read/TestMaps.java
> ===================================================================
> RCS file:
> /home/cvspublic/jakarta-commons/betwixt/src/test/org/apache/commons/
> betwixt/io/read/Attic/TestMaps.java,v
> retrieving revision 1.1.2.1
> diff -u -r1.1.2.1 TestMaps.java
> --- src/test/org/apache/commons/betwixt/io/read/TestMaps.java 22 Feb
> 2004 17:09:29 -0000 1.1.2.1
> +++ src/test/org/apache/commons/betwixt/io/read/TestMaps.java 16 Apr
> 2004 20:46:55 -0000
> @@ -132,4 +132,60 @@
>
> }
>
> + public void testMapWithArray() throws Exception {
> +
> + AddressBook addressBook = new AddressBook();
> + AddressBean[] johnsAddresses = new AddressBean[2];
> + johnsAddresses[0] = new AddressBean("12 here", "Chicago",
> "USA", "1234");
> + johnsAddresses[1] = new AddressBean("333 there", "Los Angeles",
> "USA", "99999");
> + String name = "John";
> + addressBook.addAddressBookItem(name, johnsAddresses);
> + StringWriter outputWriter = new StringWriter();
> + outputWriter.write("<?xml version='1.0' ?>\n");
> + BeanWriter beanWriter = new BeanWriter(outputWriter);
> + beanWriter.enablePrettyPrint();
> + beanWriter.write(addressBook);
> + System.out.println(outputWriter.toString());
> + String xml = "<?xml version='1.0' ?>\n" +
> + " <AddressBook id=\"1\">\n" +
> + " <addressBookItems>\n" +
> + " <entry id=\"2\">\n" +
> + " <key>John</key>\n" +
> + " <value id=\"3\">\n" +
> + " <AddressBean id=\"4\">\n" +
> + " <city>Chicago</city>\n" +
> + " <code>1234</code>\n" +
> + " <country>USA</country>\n" +
> + " <street>12 here</street>\n" +
> + " </AddressBean>\n" +
> + " <AddressBean id=\"5\">\n" +
> + " <city>Los Angeles</city>\n" +
> + " <code>99999</code>\n" +
> + " <country>USA</country>\n" +
> + " <street>333 there</street>\n" +
> + " </AddressBean>\n" +
> + " </value>\n" +
> + " </entry>\n" +
> + " </addressBookItems>\n" +
> + " </AddressBook>\n";
> +
> + assertEquals(xml, outputWriter.toString());
> + BeanReader reader = new BeanReader();
> + reader.registerBeanClass(AddressBook.class);
> + StringReader xmlReader = new
> StringReader(outputWriter.toString());
> + AddressBook result = (AddressBook) reader.parse(xmlReader);
> + assertNotNull("Expected to get an AddressBook!", result);
> + assertNotNull("Expected AddressBook to have some address
> entryitems!", result.getAddressBookItems());
> + AddressBean []resultAddresses = (AddressBean[])
> result.getAddressBookItems().get(name);
> + assertNotNull("Expected to have some addresses for " + name,
> resultAddresses);
> + assertEquals("Got wrong city in first address for " + name,
> + johnsAddresses[0].getCity(),
> + resultAddresses[0].getCity());
> + assertEquals("Got wrong city in second address for " + name,
> + johnsAddresses[1].getCity(),
> + resultAddresses[1].getCity());
> +
> +
> + }
> +
> }
>
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Tax Center - File online by April 15th
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: [betwixt] Problem with Maps and Arrays
Posted by b p <tr...@yahoo.com>.
Hi Robert,
Thanks a bunch. I just got the code down and ran a few initial tests with arrays and it seems to be meeting my needs. I'll get a chance to spend a little more time looking at it later this week and let you know how it goes.
Thanks again,
Brian
robert burrell donkin <ro...@blueyonder.co.uk> wrote:
hi brian
i've added code supporting the test case you gave me. it's new and
probably under-tested so feedback would be appreciated.
add this functionality was actually quite a nice test of the refactored
reading code. creating special actions to provide this functionality
was clean and (relatively) easy. this was one of the major aims of the
refactoring and i'm now confident that the design's about right and
that the code's approaching the time when it's ready to be merged back
into the main stream.
- robert
On 16 Apr 2004, at 22:00, b p wrote:
> Hi Robert,
>
> I've hit a problem with Maps and arrays that I haven't been able to
> resolve. I would like to write/read a Map that has an array (either
> java array or collection) as the values of the map. When I write,
> everything looks ok. When I read back in, I'm getting a map with no
> values. If I wrap the array or collection in another bean (so the
> value of the map is a bean, not an array or collection), it will work.
> I can live with that if I need to, but I would prefer to have the map
> values be arrays or collections directly. I haven't been able to
> track down a good way to do that-- I'm hoping you'll see a relatively
> easy solution I'm missing :).
>
> Below is a test case that shows the problem. Any suggestions?
>
> Thanks,
> Brian
>
>
> AddressBook.java
> ---------------------------------
> package org.apache.commons.betwixt.io.read;
> import java.util.HashMap;
> /**
> * @author Brian Pugh
> */
> public class AddressBook {
> private HashMap addressesMap = new HashMap();
>
> public HashMap getAddressBookItems() {
> return addressesMap;
> }
> public void setAddressBookItems(HashMap map) {
> this.addressesMap = map;
> }
> public void addAddressBookItem(String name, AddressBean[] addresses)
> {
> addressesMap.put(name, addresses);
> }
>
> }
>
>
> Test case (patch to TestMaps.java)
> ----------------------------------------------------
>
> Index: src/test/org/apache/commons/betwixt/io/read/TestMaps.java
> ===================================================================
> RCS file:
> /home/cvspublic/jakarta-commons/betwixt/src/test/org/apache/commons/
> betwixt/io/read/Attic/TestMaps.java,v
> retrieving revision 1.1.2.1
> diff -u -r1.1.2.1 TestMaps.java
> --- src/test/org/apache/commons/betwixt/io/read/TestMaps.java 22 Feb
> 2004 17:09:29 -0000 1.1.2.1
> +++ src/test/org/apache/commons/betwixt/io/read/TestMaps.java 16 Apr
> 2004 20:46:55 -0000
> @@ -132,4 +132,60 @@
>
> }
>
> + public void testMapWithArray() throws Exception {
> +
> + AddressBook addressBook = new AddressBook();
> + AddressBean[] johnsAddresses = new AddressBean[2];
> + johnsAddresses[0] = new AddressBean("12 here", "Chicago",
> "USA", "1234");
> + johnsAddresses[1] = new AddressBean("333 there", "Los Angeles",
> "USA", "99999");
> + String name = "John";
> + addressBook.addAddressBookItem(name, johnsAddresses);
> + StringWriter outputWriter = new StringWriter();
> + outputWriter.write("\n");
> + BeanWriter beanWriter = new BeanWriter(outputWriter);
> + beanWriter.enablePrettyPrint();
> + beanWriter.write(addressBook);
> + System.out.println(outputWriter.toString());
> + String xml = "\n" +
> + " \n" +
> + " \n" +
> + " \n" +
> + " John\n" +
> + " \n" +
> + " \n" +
> + " Chicago\n" +
> + " 1234\n" +
> + " USA\n" +
> + " 12 here\n" +
> + " \n" +
> + " \n" +
> + " Los Angeles\n" +
> + " 99999\n" +
> + " USA\n" +
> + " 333 there\n" +
> + " \n" +
> + " \n" +
> + " \n" +
> + " \n" +
> + " \n";
> +
> + assertEquals(xml, outputWriter.toString());
> + BeanReader reader = new BeanReader();
> + reader.registerBeanClass(AddressBook.class);
> + StringReader xmlReader = new
> StringReader(outputWriter.toString());
> + AddressBook result = (AddressBook) reader.parse(xmlReader);
> + assertNotNull("Expected to get an AddressBook!", result);
> + assertNotNull("Expected AddressBook to have some address
> entryitems!", result.getAddressBookItems());
> + AddressBean []resultAddresses = (AddressBean[])
> result.getAddressBookItems().get(name);
> + assertNotNull("Expected to have some addresses for " + name,
> resultAddresses);
> + assertEquals("Got wrong city in first address for " + name,
> + johnsAddresses[0].getCity(),
> + resultAddresses[0].getCity());
> + assertEquals("Got wrong city in second address for " + name,
> + johnsAddresses[1].getCity(),
> + resultAddresses[1].getCity());
> +
> +
> + }
> +
> }
>
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Tax Center - File online by April 15th
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
Re: [betwixt] Problem with Maps and Arrays
Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
hi brian
i've added code supporting the test case you gave me. it's new and
probably under-tested so feedback would be appreciated.
add this functionality was actually quite a nice test of the refactored
reading code. creating special actions to provide this functionality
was clean and (relatively) easy. this was one of the major aims of the
refactoring and i'm now confident that the design's about right and
that the code's approaching the time when it's ready to be merged back
into the main stream.
- robert
On 16 Apr 2004, at 22:00, b p wrote:
> Hi Robert,
>
> I've hit a problem with Maps and arrays that I haven't been able to
> resolve. I would like to write/read a Map that has an array (either
> java array or collection) as the values of the map. When I write,
> everything looks ok. When I read back in, I'm getting a map with no
> values. If I wrap the array or collection in another bean (so the
> value of the map is a bean, not an array or collection), it will work.
> I can live with that if I need to, but I would prefer to have the map
> values be arrays or collections directly. I haven't been able to
> track down a good way to do that-- I'm hoping you'll see a relatively
> easy solution I'm missing :).
>
> Below is a test case that shows the problem. Any suggestions?
>
> Thanks,
> Brian
>
>
> AddressBook.java
> ---------------------------------
> package org.apache.commons.betwixt.io.read;
> import java.util.HashMap;
> /**
> * @author Brian Pugh
> */
> public class AddressBook {
> private HashMap addressesMap = new HashMap();
>
> public HashMap getAddressBookItems() {
> return addressesMap;
> }
> public void setAddressBookItems(HashMap map) {
> this.addressesMap = map;
> }
> public void addAddressBookItem(String name, AddressBean[] addresses)
> {
> addressesMap.put(name, addresses);
> }
>
> }
>
>
> Test case (patch to TestMaps.java)
> ----------------------------------------------------
>
> Index: src/test/org/apache/commons/betwixt/io/read/TestMaps.java
> ===================================================================
> RCS file:
> /home/cvspublic/jakarta-commons/betwixt/src/test/org/apache/commons/
> betwixt/io/read/Attic/TestMaps.java,v
> retrieving revision 1.1.2.1
> diff -u -r1.1.2.1 TestMaps.java
> --- src/test/org/apache/commons/betwixt/io/read/TestMaps.java 22 Feb
> 2004 17:09:29 -0000 1.1.2.1
> +++ src/test/org/apache/commons/betwixt/io/read/TestMaps.java 16 Apr
> 2004 20:46:55 -0000
> @@ -132,4 +132,60 @@
>
> }
>
> + public void testMapWithArray() throws Exception {
> +
> + AddressBook addressBook = new AddressBook();
> + AddressBean[] johnsAddresses = new AddressBean[2];
> + johnsAddresses[0] = new AddressBean("12 here", "Chicago",
> "USA", "1234");
> + johnsAddresses[1] = new AddressBean("333 there", "Los Angeles",
> "USA", "99999");
> + String name = "John";
> + addressBook.addAddressBookItem(name, johnsAddresses);
> + StringWriter outputWriter = new StringWriter();
> + outputWriter.write("<?xml version='1.0' ?>\n");
> + BeanWriter beanWriter = new BeanWriter(outputWriter);
> + beanWriter.enablePrettyPrint();
> + beanWriter.write(addressBook);
> + System.out.println(outputWriter.toString());
> + String xml = "<?xml version='1.0' ?>\n" +
> + " <AddressBook id=\"1\">\n" +
> + " <addressBookItems>\n" +
> + " <entry id=\"2\">\n" +
> + " <key>John</key>\n" +
> + " <value id=\"3\">\n" +
> + " <AddressBean id=\"4\">\n" +
> + " <city>Chicago</city>\n" +
> + " <code>1234</code>\n" +
> + " <country>USA</country>\n" +
> + " <street>12 here</street>\n" +
> + " </AddressBean>\n" +
> + " <AddressBean id=\"5\">\n" +
> + " <city>Los Angeles</city>\n" +
> + " <code>99999</code>\n" +
> + " <country>USA</country>\n" +
> + " <street>333 there</street>\n" +
> + " </AddressBean>\n" +
> + " </value>\n" +
> + " </entry>\n" +
> + " </addressBookItems>\n" +
> + " </AddressBook>\n";
> +
> + assertEquals(xml, outputWriter.toString());
> + BeanReader reader = new BeanReader();
> + reader.registerBeanClass(AddressBook.class);
> + StringReader xmlReader = new
> StringReader(outputWriter.toString());
> + AddressBook result = (AddressBook) reader.parse(xmlReader);
> + assertNotNull("Expected to get an AddressBook!", result);
> + assertNotNull("Expected AddressBook to have some address
> entryitems!", result.getAddressBookItems());
> + AddressBean []resultAddresses = (AddressBean[])
> result.getAddressBookItems().get(name);
> + assertNotNull("Expected to have some addresses for " + name,
> resultAddresses);
> + assertEquals("Got wrong city in first address for " + name,
> + johnsAddresses[0].getCity(),
> + resultAddresses[0].getCity());
> + assertEquals("Got wrong city in second address for " + name,
> + johnsAddresses[1].getCity(),
> + resultAddresses[1].getCity());
> +
> +
> + }
> +
> }
>
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Tax Center - File online by April 15th
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org