users@jaxb.java.net

Re: JAXB instantiates extra HashSet layer

From: Ed Mooney <Ed.Mooney_at_Sun.COM>
Date: Fri, 30 Jun 2006 13:18:39 -0400

Hi Thang,

I'm sure this is Java behavior not specific to JAXB, but I couldn't a
reference. Since Set is an interface, I'm speculating that HashSet is
the default implementation returned when you do something like

     Set<Items.Item> s = new
java.util.concurrent.CopyOnWriteArraySet<Items.Item>()

I think the answer should be here[1], but I couldn't find it right away.

Regards,
-- 
Ed Mooney         |Sun Microsystems, Inc.|Time flies like
Java Web Services |UBUR02-201            |an arrow, but
Ed.Mooney_at_Sun.COM |1 Network Drive       |fruit flies like
781-442-0459      |Burlington, MA  01803 |a banana. Groucho
[1] http://java.sun.com/j2se/1.5.0/docs/guide/collections/index.html
Thang X. Le wrote:
> Hi,
> 
> This seems a basic question, but I haven't found it
> anywhere. The default mapping from <xs:sequence>
> is to a List<Object>. But how can I have it mapped
> to a Set<Object>?
> 
> I was looking into the datatypeconverter sample in
> JAXB distribution. I tried to add an Adapter class to
> convert from Items type to a Set. This is the new
> POType:
> public class POType {
>    ....
>     @XmlJavaTypeAdapter(SetAdapter .class)
>     private java.util.Set items;
>  
>     public java.util.Set getItems() {
>         return items;
>     }
>     public void setItems(java.util.Set value) {
>         this.items = value;
>     }
> }
> 
> And this is the adapter
> package primer.myPo;
>  
> import javax.xml.bind.annotation.adapters.XmlAdapter;
> import java.util.*;
>  
> public class SetAdapter
>     extends XmlAdapter<Items, java.util.Set>
> {
>     public java.util.Set unmarshal(Items value) {
>         Set<Items.Item> s = new 
> java.util.concurrent.CopyOnWriteArraySet<Items.Item>();
>         List<Items.Item> l = value.getItem();
>         for (Iterator<Items.Item> ite = l.listIterator(); ite.hasNext(); ) {
> 			Items.Item item = ite.next();
> 			s.add(item);
> 		}
>         return s;
>     }
>  
>     public Items marshal(java.util.Set value) {
>         Items i = new Items();
>         List<Items.Item> l = i.getItem();
>         for (Iterator ite = value.iterator(); ite.hasNext(); ) {
> 			l.add((Items.Item)ite.next());
> 		}
> 		return i;
>     }
> }
> 
> Everything else stays the same (as in the sample).
> Now, the marshalling/unmarshalling does work. However, 
> POType.items is now not a Set<Items.Item>, but a 
> HashSet<Set<Items.Item>>. This I don't understand. Where
> did this HashSet come from? Why did JAXB implicitly creates
> it? Why wasn't the method POType.setItems(Set) called?
> And if it wasn't called, how could POType.items be instantiated,
> when it is a private field?
> 
> Someone with the knowledge please shed some insights.
> Thank you very much.
> 
> ------- End of forwarded message -------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>