Class MapEntry<K,V>
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> void Helper method intended to be used by a custom JiBX "add-method".static <K,
V> void Helper method intended to be used by a custom JiBX "add-method".getKey()
Get this map entry's key.getValue()
Get this map entry's value.Helper method intended to be used by a custom JiBX "iter-method".void
void
-
Constructor Details
-
MapEntry
public MapEntry()
-
-
Method Details
-
getKey
Get this map entry's key.- Returns:
- map entry key
-
setKey
-
getValue
Get this map entry's value.- Returns:
- map entry value
-
setValue
-
iterate
public static <K,V, Iterator<E> iterateE extends MapEntry<K, V>> (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 keysV
- type of map valuesE
- map entry type- Parameters:
map
- map to iterateentryClass
- the subclass ofMapEntry
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, throws JiBXParseException? extends V> entry) Helper method intended to be used by a custom JiBX "add-method". If there is an existing entry with the same key, aJiBXParseException
is thrown.- Type Parameters:
K
- type of map keysV
- type of map values- Parameters:
map
- map to which to add an new entryentry
- 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, throws JiBXParseException? extends V> entry, boolean allowDuplicate) Helper method intended to be used by a custom JiBX "add-method".- Type Parameters:
K
- type of map keysV
- type of map values- Parameters:
map
- map to which to add an new entryentry
- new entry to addallowDuplicate
-true
to replace any existing entry having the same key, orfalse
to throw aJiBXParseException
if there is an existing entry- Throws:
JiBXParseException
- ifallowDuplicate
isfalse
and an entry with the same key already exists inmap
-