Class EncodableList<E>
- java.lang.Object
-
- com.opencloud.rhino.util.EncodableList<E>
-
- Type Parameters:
E
- list element type.
- All Implemented Interfaces:
Encodable
,Iterable<E>
,Collection<E>
,List<E>
public abstract class EncodableList<E> extends Object implements List<E>, Encodable
Generic implementation of a list structure that can be stored in SLEE CMP fields with sensible serialisation semantics. This class essentially provides a layer on top of some other list implementation, delegating method calls on the
List
interface through to the underlying list implementation, while handling the serialisation and deserialisation of the list’s state via the contract provided by theEncodable
interface.In order to use this list, a subclass must be implemented that:
-
Provides at least a public constructor satisfying the contract of the
Encodable
interface. -
Implements the abstract
newInstance
,writeValue
, andreadValue
methods defined in this class.
This implementation provides the ability to specify what type of list structure to use as a backing store from a set of predefined types. A subclass may use an alternative list implementation by:
-
Invoking the
one-arg class constructor
passingnull
as thebackingStore
parameter; and -
Overriding the
newListInstance(BackingStore)
method to create the required list structure, disregarding the method argument.
- Since:
- Rhino 2.4.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
EncodableList.BackingStore
Pre-defined backing stores for theEncodableList
.
-
Constructor Summary
Constructors Modifier Constructor Description EncodableList()
Create anEncodableList
usingEncodableList.BackingStore.ARRAY
as the default backing store.EncodableList(EncodableList.BackingStore backingStore)
Create anEncodableList
using the specified backing store.protected
EncodableList(EncodableList.BackingStore backingStore, List<E> list)
Create anEncodableList
using the specified backing store and list.protected
EncodableList(DataInput in, ClassLoader cl, DecoderUtils utils)
Creates anEncodableList
with state restored from the specified input stream.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
add(int index, E element)
boolean
add(E e)
boolean
addAll(int index, Collection<? extends E> c)
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 list elements with state restored from the specified input stream.E
get(int index)
int
hashCode()
int
indexOf(Object o)
boolean
isEmpty()
Iterator<E>
iterator()
int
lastIndexOf(Object o)
ListIterator<E>
listIterator()
ListIterator<E>
listIterator(int index)
protected boolean
manageNullElements()
Determine if thisEncodableList
type should manage null elements within the serialisation logic.protected abstract EncodableList<E>
newInstance(EncodableList.BackingStore backingStore, List<E> list)
Create a new instance of thisEncodableList
wrapping the specified list.protected List<E>
newListInstance(EncodableList.BackingStore backingStore)
Create a new list instance for the backing store.protected abstract E
readElement(DataInput in, ClassLoader cl, DecoderUtils utils)
Deserialise a list element from the input stream.E
remove(int index)
boolean
remove(Object o)
boolean
removeAll(Collection<?> c)
boolean
retainAll(Collection<?> c)
E
set(int index, E element)
int
size()
List<E>
subList(int fromIndex, int toIndex)
Object[]
toArray()
<T> T[]
toArray(T[] a)
protected void
toStream(DataOutput out, EncoderUtils utils)
Serialise the state of the list to the given output stream.String
toString()
protected abstract void
writeElement(E element, DataOutput out, EncoderUtils utils)
Serialise the specified list 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.List
replaceAll, sort, spliterator
-
-
-
-
Constructor Detail
-
EncodableList
public EncodableList()
Create an
EncodableList
usingEncodableList.BackingStore.ARRAY
as the default backing store.
-
EncodableList
public EncodableList(EncodableList.BackingStore backingStore)
Create an
EncodableList
using the specified backing store.- Parameters:
backingStore
- the backing store to use.
-
EncodableList
protected EncodableList(EncodableList.BackingStore backingStore, List<E> list)
Create an
EncodableList
using the specified backing store and list. This constructor exists primarily to facilitate the implementation ofsubList(int, int)
, but may also be used to create anEncodableList
that wraps a pre-existing list.- Parameters:
backingStore
- the backing store to use.list
- the initial list state.
-
EncodableList
protected EncodableList(DataInput in, ClassLoader cl, DecoderUtils utils) throws IOException
Creates an
EncodableList
with state restored from the specified input stream.- Parameters:
in
- the input stream to read list 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 list elements with state restored from the specified input stream. This method typically would only be used after the list has been cleared, and can be used to recycle the underlying list object.
Warning: This method can only be used to deserialise a list serialised using the
toStream(DataOutput, EncoderUtils)
method. It will not work with a list serialised using theencode(DataOutput, EncoderUtils)
method.- Parameters:
in
- the input stream to read list 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 list 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 list 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 interfaceList<E>
-
addAll
public boolean addAll(Collection<? extends E> c)
-
addAll
public boolean addAll(int index, Collection<? extends E> c)
-
removeAll
public boolean removeAll(Collection<?> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
clear
public void clear()
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
lastIndexOf
public int lastIndexOf(Object o)
- Specified by:
lastIndexOf
in interfaceList<E>
-
listIterator
public ListIterator<E> listIterator()
- Specified by:
listIterator
in interfaceList<E>
-
listIterator
public ListIterator<E> listIterator(int index)
- Specified by:
listIterator
in interfaceList<E>
-
manageNullElements
protected boolean manageNullElements()
Determine if this
EncodableList
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 list 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.
-
newListInstance
protected List<E> newListInstance(EncodableList.BackingStore backingStore)
Create a new list instance for the backing store. The default behaviour of this method simply returns the value of
backingStore.createList()
.- Parameters:
backingStore
- the backing store originally passed in to the class constructor when theEncodableList
was first created.- Returns:
- a list.
-
newInstance
protected abstract EncodableList<E> newInstance(EncodableList.BackingStore backingStore, List<E> list)
Create a new instance of this
EncodableList
wrapping the specified list.- Parameters:
backingStore
- the backing store originally passed in to the class constructor when thisEncodableList
was first created.list
- the list that the newEncodableList
instance should wrap.- Returns:
- a new
EncodableList
instance wrapping the list
-
writeElement
protected abstract void writeElement(E element, DataOutput out, EncoderUtils utils) throws IOException
Serialise the specified list element to the output stream.
- Parameters:
element
- the list element, may benull
if the underlying list supports null keys 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 list 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 list element.
- Throws:
IOException
- if an I/O error occurs.
-
-