Class MapEntry<K,V>

java.lang.Object
org.dellroad.stuff.jibx.MapEntry<K,V>

public class MapEntry<K,V> extends Object
Utility class that makes it slightly easier to model Map properties in JiBX. This class can be used to represent entries in the map, each of which is modeled in XML as a separate XML element.

For example, suppose you have a class Company and want to add a directory property that has type Map<String, Person>:


 public class Company {
     private Map<String, Person> directory = new HashMap<String, Person>();

     // Getter and setter for the "directory" property
     public Map<String, Person> getDirectory() {
         return this.directory;
     }
     public void setDirectory(Map<String, Person> directory) {
         this.directory = directory;
     }
 }
 

Because the JiBX binding process modifies class files, you first need to create your own subclass of MapEntry that can be modified. In this example, we'll use an inner class of Company. In addition, you also need to add JiBX "add-method" and "iter-method" helper methods. The resulting new code might look like this:


     // JiBX holder for a single entry in the Directory map
     public static class DirectoryEntry extends MapEntry<String, Person> {
        public String getKey()   { return super.getKey();   }   // JiBX requires exact return types
        public Person getValue() { return super.getValue(); }   // JiBX requires exact return types
     }

     // JiBX "add-method" that adds a new entry to the directory
     void addDirectoryEntry(DirectoryEntry entry) throws JiBXParseException {
         MapEntry.add(this.directory, entry);
     }

     // JiBX "iter-method" that iterates all entries in the directory
     Iterator<DirectoryEntry> iterateDirectoryEntries() {
         return MapEntry.iterate(this.directory, DirectoryEntry.class);
     }
 

Then in your JiBX binding definition, you would do something like this:


 <binding package="com.example">

     <!-- Include XML mapping definition for a Person object (having type-name "person") -->
     <include path="person.xml"/>

     <!-- Define the XML mapping for one entry in the "directory" map -->
     <mapping abstract="true" type-name="directory_entry" class="com.example.Company$DirectoryEntry">
         <value name="name" get-method="getKey" set-method="setKey" type="java.lang.String" style="attribute"/>
         <structure name="Person" get-method="getValue" set-method="setValue" map-as="person"/>
     </mapping>

     <!-- Define XML mapping for a Company object -->
     <mapping abstract="true" type-name="company" class="com.example.Company">
         <collection name="Directory" item-type="com.example.Company$DirectoryEntry"
           add-method="addDirectoryEntry" iter-method="iterateDirectoryEntries">
             <structure name="DirectoryEntry" map-as="directory_entry"/>
         </collection>
         <!-- other properties... -->
     </mapping>
 </binding>
 
Then the resulting XML would end up looking something like this:

 <Company>
     <Directory>
         <DirectoryEntry name="George Washington">
             <Person>
                  <!-- properties of George Washington... -->
             </Person>
         </DirectoryEntry>
         <DirectoryEntry name="Betsy Ross">
             <Person>
                  <!-- properties of Betsy Ross... -->
             </Person>
         </DirectoryEntry>
     </Directory>
     <!-- other properties... -->
 </Company>
 

Note that during unmarshalling, the Map itself is not created; it is expected to already exist and be empty. This will be the case if you provide a field initializer as in the example above.

The map keys are not constrained to being simple values: for complex keys, just adjust the mapping for the DirectoryEntry structure accordingly.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V> void
    add(Map<K,V> map, MapEntry<? extends K,? extends V> entry)
    Helper method intended to be used by a custom JiBX "add-method".
    static <K, V> void
    add(Map<K,V> map, MapEntry<? extends K,? extends V> entry, boolean allowDuplicate)
    Helper method intended to be used by a custom JiBX "add-method".
    Get this map entry's key.
    Get this map entry's value.
    static <K, V, E extends MapEntry<K, V>>
    Iterator<E>
    iterate(Map<K,V> map, Class<E> entryClass)
    Helper method intended to be used by a custom JiBX "iter-method".
    void
    setKey(K key)
     
    void
    setValue(V value)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MapEntry

      public MapEntry()
  • Method Details

    • getKey

      public K getKey()
      Get this map entry's key.
      Returns:
      map entry key
    • setKey

      public void setKey(K key)
    • getValue

      public V getValue()
      Get this map entry's value.
      Returns:
      map entry value
    • setValue

      public void setValue(V value)
    • iterate

      public static <K, V, E extends MapEntry<K, V>> Iterator<E> iterate(Map<K,V> map, Class<E> entryClass)
      Helper method intended to be used by a custom JiBX "iter-method". This method returns an iterator that iterates over all entries in the given map.
      Type Parameters:
      K - type of map keys
      V - type of map values
      E - map entry type
      Parameters:
      map - map to iterate
      entryClass - the subclass of MapEntry used for iterated elements; must have a default constructor
      Returns:
      map entry iterator
    • add

      public static <K, V> void add(Map<K,V> map, MapEntry<? extends K,? extends V> entry) throws JiBXParseException
      Helper method intended to be used by a custom JiBX "add-method". If there is an existing entry with the same key, a JiBXParseException is thrown.
      Type Parameters:
      K - type of map keys
      V - type of map values
      Parameters:
      map - map to which to add an new entry
      entry - new entry to add
      Throws:
      JiBXParseException - if the map already contains an entry with the given key
    • add

      public static <K, V> void add(Map<K,V> map, MapEntry<? extends K,? extends V> entry, boolean allowDuplicate) throws JiBXParseException
      Helper method intended to be used by a custom JiBX "add-method".
      Type Parameters:
      K - type of map keys
      V - type of map values
      Parameters:
      map - map to which to add an new entry
      entry - new entry to add
      allowDuplicate - true to replace any existing entry having the same key, or false to throw a JiBXParseException if there is an existing entry
      Throws:
      JiBXParseException - if allowDuplicate is false and an entry with the same key already exists in map