|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.dellroad.stuff.jibx.MapEntry<K,V>
public class MapEntry<K,V>
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> {
}
// 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 | |
|---|---|
MapEntry()
|
|
| Method Summary | ||
|---|---|---|
static
|
add(Map<K,V> map,
MapEntry<? extends K,? extends V> entry)
Helper method intended to be used by a custom JiBX "add-method". |
|
static
|
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". |
|
K |
getKey()
Get this map entry's key. |
|
V |
getValue()
Get this map entry's value. |
|
static
|
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 Detail |
|---|
public MapEntry()
| Method Detail |
|---|
public K getKey()
public void setKey(K key)
public V getValue()
public void setValue(V value)
public static <K,V,E extends MapEntry<K,V>> Iterator<E> iterate(Map<K,V> map,
Class<E> entryClass)
K - type of map keysV - type of map valuesmap - map to iterateentryClass - the subclass of MapEntry used for iterated elements; must have a default constructor
public static <K,V> void add(Map<K,V> map,
MapEntry<? extends K,? extends V> entry)
throws JiBXParseException
JiBXParseException is thrown.
map - map to which to add an new entryentry - new entry to add
JiBXParseException - if the map already contains an entry with the given key
public static <K,V> void add(Map<K,V> map,
MapEntry<? extends K,? extends V> entry,
boolean allowDuplicate)
throws JiBXParseException
map - map to which to add an new entryentry - new entry to addallowDuplicate - true to replace any existing entry having the same key,
or false to throw a JiBXParseException if there is an existing entry
JiBXParseException - if allowDuplicate is false and an entry
with the same key already exists in map
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||