Class EncodableMap<K,V>
- java.lang.Object
-
- com.opencloud.rhino.util.EncodableMap<K,V>
-
- Type Parameters:
K- map key type.V- map value type.
public abstract class EncodableMap<K,V> extends Object implements Map<K,V>, Encodable
Generic implementation of a map structure that can be stored in SLEE CMP fields with sensible serialisation semantics. This class essentially provides a layer on top of some other map implementation, delegating method calls on the
Mapinterface through to the underlying map implementation, while handling the serialisation and deserialisation of the map’s state via the contract provided by theEncodableinterface.In order to use this map, a subclass must be implemented that:
-
Provides at least a public constructor satisfying the contract of the
Encodableinterface. -
Implements the abstract
write*andread*methods defined in this class to serialise and deserialise individual map keys and values.
This implementation provides the ability to specify what type of map structure to use as a backing store from a set of predefined types. A subclass may use an alternative map implementation by:
-
Invoking the
one-arg class constructorpassingnullas thebackingStoreparameter; and -
Overriding the
newMapInstance(BackingStore)method to create the required map structure, disregarding the method argument.
- Since:
- Rhino 2.4.0
-
-
Constructor Summary
Constructors Modifier Constructor Description EncodableMap()Create anEncodableMapusingEncodableMap.BackingStore.HASHas the default backing store.EncodableMap(EncodableMap.BackingStore backingStore)Create anEncodableMapusing the specified backing store.protectedEncodableMap(EncodableMap.BackingStore backingStore, Map<K,V> map)Create anEncodableSetusing the specified backing store and map.protectedEncodableMap(DataInput in, ClassLoader cl, DecoderUtils utils)Creates anEncodableMapwith state restored from the specified input stream.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)voidencode(DataOutput out, EncoderUtils utils)Encode the state of this object to the given data output stream.Set<Map.Entry<K,V>>entrySet()booleanequals(Object o)protected voidfromStream(DataInput in, ClassLoader cl, DecoderUtils utils)Populate map entries with state restored from the specified input stream.Vget(Object key)inthashCode()booleanisEmpty()Set<K>keySet()protected booleanmanageNullKeys()Determine if thisEncodableMaptype should manage null keys within the serialisation logic.protected booleanmanageNullValues()Determine if thisEncodableMaptype should manage null values within the serialisation logic.protected Map<K,V>newMapInstance(EncodableMap.BackingStore backingStore)Create a new map instance for the backing store.Vput(K key, V value)voidputAll(Map<? extends K,? extends V> m)protected abstract KreadKey(DataInput in, ClassLoader cl, DecoderUtils utils)Deserialise a map key from the input stream.protected abstract VreadValue(DataInput in, ClassLoader cl, DecoderUtils utils)Deserialise a map value from the input stream.Vremove(Object key)intsize()protected voidtoStream(DataOutput out, EncoderUtils utils)Serialise the state of the map to the given output stream.StringtoString()Collection<V>values()protected abstract voidwriteKey(K key, DataOutput out, EncoderUtils utils)Serialise the specified map key to the output stream.protected abstract voidwriteValue(V value, DataOutput out, EncoderUtils utils)Serialise the specified map value to the output stream.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
EncodableMap
public EncodableMap()
Create an
EncodableMapusingEncodableMap.BackingStore.HASHas the default backing store.
-
EncodableMap
public EncodableMap(EncodableMap.BackingStore backingStore)
Create an
EncodableMapusing the specified backing store.- Parameters:
backingStore- the backing store to use.
-
EncodableMap
protected EncodableMap(EncodableMap.BackingStore backingStore, Map<K,V> map)
Create an
EncodableSetusing the specified backing store and map. This constructor may be used to create anEncodableMapthat wraps a pre-existing map.- Parameters:
backingStore- the backing store to use.map- the initial map state.
-
EncodableMap
protected EncodableMap(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Creates an
EncodableMapwith state restored from the specified input stream.- Parameters:
in- the input stream to read map state from.cl- classloader that can be used to resolve class dependencies.utils- additional utility decoding functions.- Throws:
IOException- if an I/O error occurs.
-
-
Method Detail
-
fromStream
protected void fromStream(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Populate map entries with state restored from the specified input stream. This method typically would only be used after the map has been cleared, and can be used to recycle the underlying map object.
Warning: This method can only be used to deserialise a map serialised using the
toStream(DataOutput, EncoderUtils)method. It will not work with a map serialised using theencode(DataOutput, EncoderUtils)method.- Parameters:
in- the input stream to read map state from.cl- classloader that can be used to resolve class dependencies.utils- additional utility decoding functions.- Throws:
IOException- if an I/O error occurs.
-
encode
public void encode(DataOutput out, EncoderUtils utils) throws IOException
Description copied from interface:EncodableEncode the state of this object to the given data output stream.
- Specified by:
encodein interfaceEncodable- Parameters:
out- the data output stream to encode the value to.utils- additional utility encoding functions that may be useful to the encoder.- Throws:
IOException- if an I/O error occurs.
-
toStream
protected void toStream(DataOutput out, EncoderUtils utils) throws IOException
Serialise the state of the map to the given output stream.
Warning: the serialisation data created by this method can only be restored using the
fromStream(DataInput, ClassLoader, DecoderUtils)method. For a more general result that follows theEncodablecontract, use theencode(DataOutput, EncoderUtils)method.- Parameters:
out- the output stream to write map state to.utils- additional utility encoding functions.- Throws:
IOException- if an I/O error occurs.
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<K,V>
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
manageNullKeys
protected boolean manageNullKeys()
Determine if this
EncodableMaptype should manage null keys within the serialisation logic.The result of this method determines whether or not null checks are included for keys in the output stream when the map is serialised. If null keys are never expected, not supported by the underlying backing store, or the implementation of
writeKey(K, java.io.DataOutput, com.opencloud.rhino.cmp.codecs.EncoderUtils)andreadKey(java.io.DataInput, java.lang.ClassLoader, com.opencloud.rhino.cmp.codecs.DecoderUtils)will handle null keys, then this method may safely returnfalse.The default implementation of this method always returns
false.- Returns:
trueif null keys should be managed,falseotherwise.
-
manageNullValues
protected boolean manageNullValues()
Determine if this
EncodableMaptype should manage null values within the serialisation logic.The result of this method determines whether or not null checks are included for values in the output stream when the map is serialised. If null values are never expected, not supported by the underlying backing store, or the implementation of
writeValue(V, java.io.DataOutput, com.opencloud.rhino.cmp.codecs.EncoderUtils)andreadValue(java.io.DataInput, java.lang.ClassLoader, com.opencloud.rhino.cmp.codecs.DecoderUtils)will handle null values, then this method may safely returnfalse.The default implementation of this method always returns
true.- Returns:
trueif null values should be managed,falseotherwise.
-
newMapInstance
protected Map<K,V> newMapInstance(EncodableMap.BackingStore backingStore)
Create a new map instance for the backing store. The default behaviour of this method simply returns the value of
backingStore.createMap().- Parameters:
backingStore- the backing store originally passed in to the class constructor when theEncodableMapwas first created.- Returns:
- a map.
-
writeKey
protected abstract void writeKey(K key, DataOutput out, EncoderUtils utils) throws IOException
Serialise the specified map key to the output stream.
- Parameters:
key- the map key, may benullif the underlying map supports null keys and manageNullKeys() return false.out- the output stream to write the key state to.utils- additional utility encoding functions that may be useful when serialising the key.- Throws:
IOException- if an I/O error occurs.
-
writeValue
protected abstract void writeValue(V value, DataOutput out, EncoderUtils utils) throws IOException
Serialise the specified map value to the output stream.
- Parameters:
value- the map value, may benullif the underlying map supports null values and manageNullValues() return false.out- the output stream to write the value state to.utils- additional utility encoding functions that may be useful when serialising the value.- Throws:
IOException- if an I/O error occurs.
-
readKey
protected abstract K readKey(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Deserialise a map key from the input stream.
- Parameters:
in- the input stream to read the key state from.cl- classloader that can be used to resolve class dependencies.utils- additional utility decoding functions that may be useful when deserialising the key.- Returns:
- a map key.
- Throws:
IOException- if an I/O error occurs.
-
readValue
protected abstract V readValue(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Deserialise a map value from the input stream.
- Parameters:
in- the input stream to read the value state from.cl- classloader that can be used to resolve class dependencies.utils- additional utility decoding functions that may be useful when deserialising the value.- Returns:
- a map value.
- Throws:
IOException- if an I/O error occurs.
-
-