E
- set element type.public abstract class EncodableSet<E> extends Object implements Set<E>, Encodable
Set
interface through to the underlying set implementation, while handling the
serialisation and deserialisation of the set's state via the contract provided by
the Encodable
interface.
In order to use this set, a subclass must be implemented that:
Encodable
interface.
writeValue
and readValue
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:
one-arg class constructor
passing null
as the backingStore
parameter; and
newSetInstance(BackingStore)
method to create the
required set structure, disregarding the method argument.
Modifier and Type | Class and Description |
---|---|
static class |
EncodableSet.BackingStore
Pre-defined backing stores for the
EncodableSet . |
Modifier | Constructor and Description |
---|---|
|
EncodableSet()
Create an
EncodableSet using EncodableSet.BackingStore.HASH as the default backing store. |
protected |
EncodableSet(DataInput in,
ClassLoader cl,
DecoderUtils utils)
Creates an
EncodableSet with state restored from the specified input stream. |
|
EncodableSet(EncodableSet.BackingStore backingStore)
Create an
EncodableSet using the specified backing store. |
protected |
EncodableSet(EncodableSet.BackingStore backingStore,
Set<E> set)
Create an
EncodableSet using the specified backing store and set. |
Modifier and Type | Method and 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 this
EncodableSet 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.
|
public EncodableSet()
EncodableSet
using EncodableSet.BackingStore.HASH
as the default backing store.public EncodableSet(EncodableSet.BackingStore backingStore)
EncodableSet
using the specified backing store.backingStore
- the backing store to use.protected EncodableSet(EncodableSet.BackingStore backingStore, Set<E> set)
EncodableSet
using the specified backing store and set.
This constructor may be used to create an EncodableSet
that wraps a pre-existing set.backingStore
- the backing store to use.set
- the initial set state.protected EncodableSet(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
EncodableSet
with state restored from the specified input stream.in
- the input stream to read set state from.cl
- classloader that can be used to resolve class dependencies.utils
- additional utility decoding functions.IOException
- if an I/O error occurs.protected void fromStream(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
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 the encode(DataOutput, EncoderUtils)
method.
in
- the input stream to read set state from.cl
- classloader that can be used to resolve class dependencies.utils
- additional utility decoding functions.IOException
- if an I/O error occurs.public void encode(DataOutput out, EncoderUtils utils) throws IOException
Encodable
encode
in interface Encodable
out
- the data output stream to encode the value to.utils
- additional utility encoding functions that may be useful to the encoder.IOException
- if an I/O error occurs.protected void toStream(DataOutput out, EncoderUtils utils) throws IOException
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 the Encodable
contract, use the encode(DataOutput, EncoderUtils)
method.
out
- the output stream to write set state to.utils
- additional utility encoding functions.IOException
- if an I/O error occurs.public int size()
public boolean isEmpty()
public boolean contains(Object o)
public Object[] toArray()
public <T> T[] toArray(T[] a)
public boolean add(E e)
public boolean remove(Object o)
public boolean containsAll(Collection<?> c)
containsAll
in interface Collection<E>
containsAll
in interface Set<E>
public boolean addAll(Collection<? extends E> c)
public boolean retainAll(Collection<?> c)
public boolean removeAll(Collection<?> c)
public void clear()
public boolean equals(Object o)
public int hashCode()
protected boolean manageNullElements()
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)
and readElement(java.io.DataInput, java.lang.ClassLoader, com.opencloud.rhino.cmp.codecs.DecoderUtils)
will handle null elements, then this method may safely return false
.
The default implementation of this method always returns false
.
true
if null elements should be managed, false
otherwise.protected Set<E> newSetInstance(EncodableSet.BackingStore backingStore)
backingStore.createSet()
.backingStore
- the backing store originally passed in to the class constructor when the
EncodableSet
was first created.protected abstract void writeElement(E element, DataOutput out, EncoderUtils utils) throws IOException
element
- the set element, may be null
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.IOException
- if an I/O error occurs.protected abstract E readElement(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
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.IOException
- if an I/O error occurs.