freemarker.template
public class: SimpleSequence [javadoc |
source]
java.lang.Object
freemarker.template.WrappingTemplateModel
freemarker.template.SimpleSequence
All Implemented Interfaces:
TemplateSequenceModel, Serializable
Direct Known Subclasses:
NodeListModel, SynchronizedSequence, AncestorSequence, SimpleList
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.
| Field Summary |
|---|
| protected final List | list | - serial:
The - List that this SimpleSequence wraps.
|
| Method from freemarker.template.SimpleSequence Detail: |
public void add(Object obj) {
list.add(obj);
unwrappedList = null;
}
Adds an arbitrary object to the end of this SimpleSequence.
If the object itself does not implement the TemplateModel
interface, it will be wrapped into an appropriate adapter on the first
call to #get(int) . |
public void add(boolean b) {
if (b) {
add(TemplateBooleanModel.TRUE);
}
else {
add(TemplateBooleanModel.FALSE);
}
}
|
public TemplateModel get(int i) throws TemplateModelException {
try {
Object value = list.get(i);
if (value instanceof TemplateModel) {
return (TemplateModel) value;
}
TemplateModel tm = wrap(value);
list.set(i, tm);
return tm;
}
catch(IndexOutOfBoundsException e) {
return null;
// throw new TemplateModelException(i + " out of bounds [0, " + list.size() + ")");
}
}
|
public int size() {
return list.size();
}
|
public SimpleSequence synchronizedWrapper() {
return new SynchronizedSequence();
}
|
public List toList() throws TemplateModelException {
if (unwrappedList == null) {
Class listClass = list.getClass();
List result = null;
try {
result = (List) listClass.newInstance();
} catch (Exception e) {
throw new TemplateModelException("Error instantiating an object of type " + listClass.getName() + "\n" + e.getMessage());
}
BeansWrapper bw = BeansWrapper.getDefaultInstance();
for (int i=0; i< list.size(); i++) {
Object elem = list.get(i);
if (elem instanceof TemplateModel) {
elem = bw.unwrap((TemplateModel) elem);
}
result.add(elem);
}
unwrappedList = result;
}
return unwrappedList;
}
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. |
public String toString() {
return list.toString();
}
|