Class EncodableSet<E>
- java.lang.Object
-
- com.opencloud.rhino.util.EncodableSet<E>
-
- Type Parameters:
E- set element type.
- All Implemented Interfaces:
Encodable,Iterable<E>,Collection<E>,Set<E>
public abstract class EncodableSet<E> extends Object implements Set<E>, Encodable
Generic implementation of a set structure that can be stored in SLEE CMP fields with sensible serialisation semantics. This class essentially provides a layer on top of some other set implementation, delegating method calls on the
Setinterface through to the underlying set implementation, while handling the serialisation and deserialisation of the set’s state via the contract provided by theEncodableinterface.In order to use this set, a subclass must be implemented that:
-
Provides at least a public constructor satisfying the contract of the
Encodableinterface. -
Implements the abstract
writeValueandreadValuemethods defined in this class.
This implementation provides the ability to specify what type of set structure to use as a backing store from a set of predefined types. A subclass may use an alternative set implementation by:
-
Invoking the
one-arg class constructorpassingnullas thebackingStoreparameter; and -
Overriding the
newSetInstance(BackingStore)method to create the required set structure, disregarding the method argument.
- Since:
- Rhino 2.4.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEncodableSet.BackingStorePre-defined backing stores for theEncodableSet.
-
Constructor Summary
Constructors Modifier Constructor Description EncodableSet()Create anEncodableSetusingEncodableSet.BackingStore.HASHas the default backing store.EncodableSet(EncodableSet.BackingStore backingStore)Create anEncodableSetusing the specified backing store.protectedEncodableSet(EncodableSet.BackingStore backingStore, Set<E> set)Create anEncodableSetusing the specified backing store and set.protectedEncodableSet(DataInput in, ClassLoader cl, DecoderUtils utils)Creates anEncodableSetwith state restored from the specified input stream.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanadd(E e)booleanaddAll(Collection<? extends E> c)voidclear()booleancontains(Object o)booleancontainsAll(Collection<?> c)voidencode(DataOutput out, EncoderUtils utils)Encode the state of this object to the given data output stream.booleanequals(Object o)protected voidfromStream(DataInput in, ClassLoader cl, DecoderUtils utils)Populate set elements with state restored from the specified input stream.inthashCode()booleanisEmpty()Iterator<E>iterator()protected booleanmanageNullElements()Determine if thisEncodableSettype should manage null elements within the serialisation logic.protected Set<E>newSetInstance(EncodableSet.BackingStore backingStore)Create a new set instance for the backing store.protected abstract EreadElement(DataInput in, ClassLoader cl, DecoderUtils utils)Deserialise a set element from the input stream.booleanremove(Object o)booleanremoveAll(Collection<?> c)booleanretainAll(Collection<?> c)intsize()Object[]toArray()<T> T[]toArray(T[] a)protected voidtoStream(DataOutput out, EncoderUtils utils)Serialise the state of the set to the given output stream.StringtoString()protected abstract voidwriteElement(E element, DataOutput out, EncoderUtils utils)Serialise the specified set element 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.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
spliterator
-
-
-
-
Constructor Detail
-
EncodableSet
public EncodableSet()
Create an
EncodableSetusingEncodableSet.BackingStore.HASHas the default backing store.
-
EncodableSet
public EncodableSet(EncodableSet.BackingStore backingStore)
Create an
EncodableSetusing the specified backing store.- Parameters:
backingStore- the backing store to use.
-
EncodableSet
protected EncodableSet(EncodableSet.BackingStore backingStore, Set<E> set)
Create an
EncodableSetusing the specified backing store and set. This constructor may be used to create anEncodableSetthat wraps a pre-existing set.- Parameters:
backingStore- the backing store to use.set- the initial set state.
-
EncodableSet
protected EncodableSet(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Creates an
EncodableSetwith state restored from the specified input stream.- Parameters:
in- the input stream to read set 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 set elements with state restored from the specified input stream. This method typically would only be used after the set has been cleared, and can be used to recycle the underlying set object.
Warning: This method can only be used to deserialise a set serialised using the
toStream(DataOutput, EncoderUtils)method. It will not work with a set serialised using theencode(DataOutput, EncoderUtils)method.- Parameters:
in- the input stream to read set 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 set 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 set state to.utils- additional utility encoding functions.- Throws:
IOException- if an I/O error occurs.
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(Object o)
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
add
public boolean add(E e)
-
remove
public boolean remove(Object o)
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>
-
addAll
public boolean addAll(Collection<? extends E> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
removeAll
public boolean removeAll(Collection<?> c)
-
clear
public void clear()
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
manageNullElements
protected boolean manageNullElements()
Determine if this
EncodableSettype should manage null elements within the serialisation logic.The result of this method determines whether or not null checks are included in the output stream when the set is serialised. If null elements are never expected, not supported by the underlying backing store, or the implementation of
writeElement(E, java.io.DataOutput, com.opencloud.rhino.cmp.codecs.EncoderUtils)andreadElement(java.io.DataInput, java.lang.ClassLoader, com.opencloud.rhino.cmp.codecs.DecoderUtils)will handle null elements, then this method may safely returnfalse.The default implementation of this method always returns
false.- Returns:
trueif null elements should be managed,falseotherwise.
-
newSetInstance
protected Set<E> newSetInstance(EncodableSet.BackingStore backingStore)
Create a new set instance for the backing store. The default behaviour of this method simply returns the value of
backingStore.createSet().- Parameters:
backingStore- the backing store originally passed in to the class constructor when theEncodableSetwas first created.- Returns:
- a set.
-
writeElement
protected abstract void writeElement(E element, DataOutput out, EncoderUtils utils) throws IOException
Serialise the specified set element to the output stream.
- Parameters:
element- the set element, may benullif the underlying set supports null elements and manageNullElements() return false.out- the output stream to write the element state to.utils- additional utility encoding functions that may be useful when serialising the element.- Throws:
IOException- if an I/O error occurs.
-
readElement
protected abstract E readElement(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Deserialise a set element from the input stream.
- Parameters:
in- the input stream to read the element state from.cl- classloader that can be used to resolve class dependencies.utils- additional utility decoding functions that may be useful when deserialising the element.- Returns:
- a set element.
- Throws:
IOException- if an I/O error occurs.
-
-