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
Set
interface through to the underlying set implementation, while handling the serialisation and deserialisation of the set’s state via the contract provided by theEncodable
interface.In order to use this set, a subclass must be implemented that:
-
Provides at least a public constructor satisfying the contract of the
Encodable
interface. -
Implements the abstract
writeValue
andreadValue
methods 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 constructor
passingnull
as thebackingStore
parameter; 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 class
EncodableSet.BackingStore
Pre-defined backing stores for theEncodableSet
.
-
Constructor Summary
Constructors Modifier Constructor Description EncodableSet()
Create anEncodableSet
usingEncodableSet.BackingStore.HASH
as the default backing store.EncodableSet(EncodableSet.BackingStore backingStore)
Create anEncodableSet
using the specified backing store.protected
EncodableSet(EncodableSet.BackingStore backingStore, Set<E> set)
Create anEncodableSet
using the specified backing store and set.protected
EncodableSet(DataInput in, ClassLoader cl, DecoderUtils utils)
Creates anEncodableSet
with state restored from the specified input stream.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
add(E e)
boolean
addAll(Collection<? extends E> c)
void
clear()
boolean
contains(Object o)
boolean
containsAll(Collection<?> c)
void
encode(DataOutput out, EncoderUtils utils)
Encode the state of this object to the given data output stream.boolean
equals(Object o)
protected void
fromStream(DataInput in, ClassLoader cl, DecoderUtils utils)
Populate set elements with state restored from the specified input stream.int
hashCode()
boolean
isEmpty()
Iterator<E>
iterator()
protected boolean
manageNullElements()
Determine if thisEncodableSet
type 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 E
readElement(DataInput in, ClassLoader cl, DecoderUtils utils)
Deserialise a set element from the input stream.boolean
remove(Object o)
boolean
removeAll(Collection<?> c)
boolean
retainAll(Collection<?> c)
int
size()
Object[]
toArray()
<T> T[]
toArray(T[] a)
protected void
toStream(DataOutput out, EncoderUtils utils)
Serialise the state of the set to the given output stream.String
toString()
protected abstract void
writeElement(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
EncodableSet
usingEncodableSet.BackingStore.HASH
as the default backing store.
-
EncodableSet
public EncodableSet(EncodableSet.BackingStore backingStore)
Create an
EncodableSet
using the specified backing store.- Parameters:
backingStore
- the backing store to use.
-
EncodableSet
protected EncodableSet(EncodableSet.BackingStore backingStore, Set<E> set)
Create an
EncodableSet
using the specified backing store and set. This constructor may be used to create anEncodableSet
that 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
EncodableSet
with 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:Encodable
Encode the state of this object to the given data output stream.
- Specified by:
encode
in 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 theEncodable
contract, 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:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in 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
EncodableSet
type 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:
true
if null elements should be managed,false
otherwise.
-
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 theEncodableSet
was 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 benull
if 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.
-
-