Class SimpleSequence

All Implemented Interfaces:
TemplateModel, TemplateSequenceModel, Serializable
Direct Known Subclasses:
SimpleList

public class SimpleSequence extends WrappingTemplateModel implements TemplateSequenceModel, Serializable
A simple implementation of the TemplateSequenceModel interface, using its own underlying List for storing the list items. If you are wrapping an already existing List or array, you should certainly use DefaultMapAdapter or DefaultArrayAdapter (see comparison below).

This class is thread-safe if you don't call modifying methods (like add(Object)) after you have made the object available for multiple threads (assuming you have published it safely to the other threads; see JSR-133 Java Memory Model). These methods aren't called by FreeMarker, so it's usually not a concern.

SimpleSequence VS DefaultListAdapter/DefaultArrayAdapter - Which to use when?

For a List or array that exists regardless of FreeMarker, only you need to access it from templates, DefaultMapAdapter should be the default choice, as it can be unwrapped to the originally wrapped object (important when passing it to Java methods from the template). It also has more predictable performance (no spikes).

For a sequence that's made specifically to be used from templates, creating an empty SimpleSequence then filling it with add(Object) is usually the way to go, as the resulting sequence is significantly faster to read from templates than a DefaultListAdapter (though it's somewhat slower to read from a plain Java method to which it had to be passed adapted to a List).

It also matters if for how many times will the same List entry be read from the template(s) later, on average. If, on average, you read each entry for more than 4 times, SimpleSequence will be most certainly faster, but if for 2 times or less (and especially if not at all) then DefaultMapAdapter will be faster. Before choosing based on performance though, pay attention to the behavioral differences; SimpleSequence will shallow-copy the original List at construction time, so it won't reflect List content changes after the SimpleSequence construction, also SimpleSequence can't be unwrapped to the original wrapped instance.

See Also:
DefaultListAdapter, DefaultArrayAdapter, TemplateSequenceModel, Serialized Form