Package freemarker.template
Class SimpleSequence
java.lang.Object
freemarker.template.WrappingTemplateModel
freemarker.template.SimpleSequence
- All Implemented Interfaces:
TemplateModel,TemplateSequenceModel,Serializable
- Direct Known Subclasses:
SimpleList
public class SimpleSequence
extends WrappingTemplateModel
implements TemplateSequenceModel, Serializable
A convenient implementation of a list. This
object implements TemplateSequenceModel, using an underlying
java.util.List implementation.
A SimpleSequence can act as a cache for a TemplateCollectionModel, e.g. one that gets data from a database. When passed a TemplateCollectionModel as an argument to its constructor, the SimpleSequence immediately copies all the elements and discards the TemplateCollectionModel.
This class is thread-safe if you don't call the add method after you have made the object available for multiple threads.
Note:
As of 2.0, this class is unsynchronized by default.
To obtain a synchronized wrapper, call the synchronizedWrapper() method.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface freemarker.template.TemplateModel
TemplateModel.InvalidExpressionModel, TemplateModel.JavaNull -
Field Summary
FieldsFields inherited from interface freemarker.template.TemplateModel
INVALID_EXPRESSION, JAVA_NULL, NOTHING -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty simple sequence that will use the the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).SimpleSequence(int capacity) Constructs an empty simple sequence with preallocated capacity and using the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).SimpleSequence(ObjectWrapper wrapper) Constructs an empty simple sequence using the specified object wrapper.Constructs a simple sequence from the passed collection model using the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).SimpleSequence(Collection collection) Constructs a simple sequence that will contain the elements from the specifiedCollectionand will use the the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).SimpleSequence(Collection<?> collection, ObjectWrapper wrapper) Constructs a simple sequence that will contain the elements from the specifiedCollectionand will use the specified object wrapper. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(boolean b) Adds a boolean to the end of this SimpleSequence, by coercing the boolean intoTemplateBooleanModel.TRUEorTemplateBooleanModel.FALSE.voidAdds an arbitrary object to the end of this SimpleSequence.get(int i) Retrieves the i-th template model in this sequence.intsize()toList()Note that this method creates and returns a deep-copy of the underlying list used internally.toString()Methods inherited from class freemarker.template.WrappingTemplateModel
getDefaultObjectWrapper, getObjectWrapper, setDefaultObjectWrapper, setObjectWrapper, wrap
-
Field Details
-
list
-
-
Constructor Details
-
SimpleSequence
public SimpleSequence()Constructs an empty simple sequence that will use the the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper). -
SimpleSequence
public SimpleSequence(int capacity) Constructs an empty simple sequence with preallocated capacity and using the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper). -
SimpleSequence
Constructs a simple sequence that will contain the elements from the specifiedCollectionand will use the the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).- Parameters:
collection- the collection containing initial values. Note that a copy of the collection is made for internal use.
-
SimpleSequence
Constructs a simple sequence from the passed collection model using the default object wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).- Throws:
TemplateModelException
-
SimpleSequence
Constructs an empty simple sequence using the specified object wrapper.- Parameters:
wrapper- The object wrapper to use to wrap objects intoTemplateModelinstances. If null, the default wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)is used.
-
SimpleSequence
Constructs a simple sequence that will contain the elements from the specifiedCollectionand will use the specified object wrapper.- Parameters:
collection- the collection containing initial values. Note that a copy of the collection is made for internal use.wrapper- The object wrapper to use to wrap objects intoTemplateModelinstances. If null, the default wrapper set inWrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)is used.
-
-
Method Details
-
add
Adds an arbitrary object to the end of this SimpleSequence. If the object itself does not implement theTemplateModelinterface, it will be wrapped into an appropriate adapter on the first call toget(int).- Parameters:
obj- the boolean to be added.
-
add
public void add(boolean b) Adds a boolean to the end of this SimpleSequence, by coercing the boolean intoTemplateBooleanModel.TRUEorTemplateBooleanModel.FALSE.- Parameters:
b- the boolean to be added.
-
toList
Note that this method creates and returns a deep-copy of the underlying list used internally. This could be a gotcha for some people at some point who want to alter something in the data model, but we should maintain our immutability semantics (at least using default SimpleXXX wrappers) for the data model. It will recursively unwrap the stuff in the underlying container.- Throws:
TemplateModelException
-
get
Description copied from interface:TemplateSequenceModelRetrieves the i-th template model in this sequence.- Specified by:
getin interfaceTemplateSequenceModel- Returns:
- the specified index in the list
- Throws:
TemplateModelException
-
size
public int size()- Specified by:
sizein interfaceTemplateSequenceModel- Returns:
- the number of items in the list.
-
synchronizedWrapper
- Returns:
- a synchronized wrapper for list.
-
toString
-