title: JAXB XmlJavaTypeAdapter Map from List : pros/cons of this design pattern
body: <p>I have written a JAXB mapping that stored a sublist inside a root element in a <code>LinkedHashMap&lt;String, Object&gt;</code> instead of a <code>Collection&lt;Object&gt;</code> maintained through a specific <code>XmlJavaTypeAdapter</code>. Hereunder a sample:</p>
  
  <pre>@XmlRootElement
  public class Parent {
      @XmlJavaTypeAdapter(ListToMapAdapter.class)
      @XmlElement
      private LinkedHashMap&lt;String, Child&gt; children;
  
      ...
  }</pre>
  
  <p>The key is built from the attributes of the child tag (e.g. <code>&lt;child id="key"&gt;</code> give a <code>Map.Entry&lt;String, Child&gt;</code> like <code>{key =&gt; child}</code>.</p>
  
  <p>One of my coworker says its bad designed, and that this <code>Map</code> should be in the object in charge of unmarshalling the XML. I'm not agree with him. Here are some pros and cons on such an approach. Which design do you think is the best? And which pros and cons did you see to complete the debate?</p>
  
  <p><strong>Goal of this design:</strong></p>
  
  <ul>
  <li>the <code>Map</code> give the ability to search efficiently children with a criteria (the key in my sample) instead of search by iteration on a Collecion.</li>
  </ul>
  
  <p><strong>Pros:</strong></p>
  
  <ul>
  <li>responsability for searching and filtering the sublist is at the nearest of the object and the parent object is responsible of retrieving and filtering its child,</li>
  <li>the building of the <code>Map</code> is automatically made by JAXB when unmarshalling: elegant, efficient, and <strong>preserve from building the <code>Map</code> with a post process of the list after umarshalling (iteration)</strong>,</li>
  <li>the same object in different readers has still the ability of filtering its children,</li>
  <li>subjective, it's really a pretty way of doing that :),</li>
  <li>if <code>@XmlJavaTypeAdapter</code> exists, it's partly for this (numerous samples of this on the Web).</li>
  </ul>
  
  <p><strong>Cons</strong> (some are from my coworker)<strong>:</strong></p>
  
  <ul>
  <li>JAXB limitation constraints to code to a concrete implementation instead of an interface: could not declare <code>Map&lt;String, Child&gt;</code> in my sample (not instantiable by JAXB while unmarshalling),</li>
  <li>maybe (and its the argument of my coworker), its not the role of a mapping object (simple POJO according to him) to have such a behavior,</li>
  <li><code>@XmlJavaTypeAdapter</code> aims only at converting simple object to specific types: <code>xs:date</code> to Joda time <code>Calendar</code> for instance.</li>
  </ul>
  
  <p>Thanks in advance for your 2 cents.</p>