ca.grimoire.formtree.receiver.constructor
Class CollectionReceiver<E,T>

java.lang.Object
  extended by ca.grimoire.formtree.receiver.constructor.CollectionReceiver<E,T>
Type Parameters:
E - the type of the elements of the resulting collection.
T - the type of the resulting collection. This doesn't have to be a subclass of Collection - other collection types, such as arrays, are possible.
All Implemented Interfaces:
FormElementReceiver, FormReceiver<T>
Direct Known Subclasses:
ArrayReceiver, ListReceiver, SetReceiver

public abstract class CollectionReceiver<E,T>
extends java.lang.Object
implements FormReceiver<T>

Supporting behaviour allowing for form receivers that produce collections. Manages collating and populating the individual elements of the collection. Subclasses are responsible for turning the elements into a finished collection.

See Also:
ArrayReceiver, ListReceiver, SetReceiver

Field Summary
protected  java.lang.Class<E> elementClass
          The Class of the elements of the collection.
 
Constructor Summary
CollectionReceiver(java.lang.Class<E> elementClass)
          Creates a CollectionReceiver for some constructible element class.
 
Method Summary
static java.lang.Class<?> acceptableElementClass(java.lang.Class<?> elementClass)
          Given a Class representing the elements of some collection, this method determines whether it counts as constructible (either via ConstructorReceiver or via StringConstructorReceiver).
static java.lang.Class<?> acceptableElementClass(java.lang.reflect.ParameterizedType collectionType)
          Given a ParameterizedType, this method examines its parameter to determine if it's a constructible type (either via ConstructorReceiver or via StringConstructorReceiver).
protected abstract  T createCollection(java.util.List<E> elements)
          Converts the raw sequence of form objects to a collection.
 T finished()
          Converts all of the received form data into the appropriate product type.
 FormElementReceiver index(int index)
          Returns an appropriate FormElementReceiver for the index'th element of the collection, creating it if necessary.
 FormElementReceiver key(java.lang.String field)
          Ignores keys - the collection itself has no key fields.
 void values(java.lang.Iterable<java.lang.String> values)
          Applies a value sequence to the collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

elementClass

protected final java.lang.Class<E> elementClass
The Class of the elements of the collection.

Constructor Detail

CollectionReceiver

public CollectionReceiver(java.lang.Class<E> elementClass)
Creates a CollectionReceiver for some constructible element class.

Parameters:
elementClass - the class of the elements of the collection.
Method Detail

acceptableElementClass

public static java.lang.Class<?> acceptableElementClass(java.lang.reflect.ParameterizedType collectionType)
Given a ParameterizedType, this method examines its parameter to determine if it's a constructible type (either via ConstructorReceiver or via StringConstructorReceiver).

Parameters:
collectionType - the ParameterizedType representing the full collection type to create.
Returns:
the Class of the elements of the collection, or null if the collection would not contain constructible elements.

acceptableElementClass

public static java.lang.Class<?> acceptableElementClass(java.lang.Class<?> elementClass)
Given a Class representing the elements of some collection, this method determines whether it counts as constructible (either via ConstructorReceiver or via StringConstructorReceiver).

Parameters:
elementClass - the element class to examine.
Returns:
elementClass, or null if elementClass is not a constructible class.

index

public FormElementReceiver index(int index)
Returns an appropriate FormElementReceiver for the index'th element of the collection, creating it if necessary. This method can create "sparse" collections: for lists and other sequential collection types, any indices not given values will be left null.

Specified by:
index in interface FormElementReceiver
Parameters:
index - the index to decode.
Returns:
a form receiver for the index's contents.
See Also:
FormElementReceiver.index(int)

key

public FormElementReceiver key(java.lang.String field)
Ignores keys - the collection itself has no key fields.

Specified by:
key in interface FormElementReceiver
Parameters:
field - the field to decode.
Returns:
a form receiver for the key's contents.
See Also:
FormElementReceiver.key(java.lang.String)

values

public void values(java.lang.Iterable<java.lang.String> values)
Applies a value sequence to the collection. This method creates a non-sparse collection: the only elements are exactly those in the values sequence.

Specified by:
values in interface FormElementReceiver
Parameters:
values - the values to store.
See Also:
FormElementReceiver.values(java.lang.Iterable)

finished

public T finished()
Converts all of the received form data into the appropriate product type. Subclasses must override createCollection(List) to convert the received collection into the target type.

Specified by:
finished in interface FormReceiver<T>
Returns:
the result of the form decoding process.
See Also:
FormReceiver.finished()

createCollection

protected abstract T createCollection(java.util.List<E> elements)
Converts the raw sequence of form objects to a collection. The elements list will either be in index order (for collections populated via index(int)) or in iteration order (for collections populated via values(Iterable).

Parameters:
elements - the collection contents.
Returns:
the corresponding collection.


Copyright © 2011 Grimoire Alchymia. All Rights Reserved.