All Downloads are FREE. Search and download functionalities are using the official Maven repository.

freemarker.template.SimpleSequence Maven / Gradle / Ivy

Go to download

Google App Engine compliant variation of FreeMarker. FreeMarker is a "template engine"; a generic tool to generate text output based on templates.

There is a newer version: 2.3.33
Show newest version
/*
 * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package freemarker.template;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import freemarker.ext.beans.BeansWrapper;

/**
 * 

A convenient implementation of a list. This * object implements {@link 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, and you have published it * safely. * *

Note:
* As of 2.0, this class is unsynchronized by default. * To obtain a synchronized wrapper, call the {@link #synchronizedWrapper} method.

* * @see SimpleHash * @see SimpleScalar */ public class SimpleSequence extends WrappingTemplateModel implements TemplateSequenceModel, Serializable { /** * @serial The List that this SimpleSequence wraps. */ protected final List list; private List unwrappedList; /** * Constructs an empty simple sequence that will use the the default object * wrapper set in * {@link WrappingTemplateModel#setDefaultObjectWrapper(ObjectWrapper)}. * * @deprecated Use {@link #SimpleSequence(ObjectWrapper)} instead. */ public SimpleSequence() { this((ObjectWrapper) null); } /** * Constructs an empty simple sequence with preallocated capacity and using * the default object wrapper set in * {@link WrappingTemplateModel#setDefaultObjectWrapper(ObjectWrapper)}. * * @deprecated Use {@link #SimpleSequence(Collection, ObjectWrapper)}. */ public SimpleSequence(int capacity) { list = new ArrayList(capacity); } /** * Constructs a simple sequence that will contain the elements * from the specified {@link Collection} and will use the the default * object wrapper set in * {@link WrappingTemplateModel#setDefaultObjectWrapper(ObjectWrapper)}. * @param collection the collection containing initial values. Note that a * copy of the collection is made for internal use. * * @deprecated Use {@link #SimpleSequence(Collection, ObjectWrapper)}. */ public SimpleSequence(Collection collection) { this(collection, null); } /** * Constructs a simple sequence from the passed collection model using the * default object wrapper set in * {@link WrappingTemplateModel#setDefaultObjectWrapper(ObjectWrapper)}. */ public SimpleSequence(TemplateCollectionModel tcm) throws TemplateModelException { ArrayList alist = new ArrayList(); for (TemplateModelIterator it = tcm.iterator(); it.hasNext();) { alist.add(it.next()); } alist.trimToSize(); list = alist; } /** * Constructs an empty simple sequence using the specified object wrapper. * @param wrapper The object wrapper to use to wrap objects into * {@link TemplateModel} instances. If null, the default wrapper set in * {@link WrappingTemplateModel#setDefaultObjectWrapper(ObjectWrapper)} is * used. */ public SimpleSequence(ObjectWrapper wrapper) { super(wrapper); list = new ArrayList(); } /** * Constructs an empty simple sequence with preallocated capacity. * * @since 2.3.21 */ public SimpleSequence(int capacity, ObjectWrapper wrapper) { super(wrapper); list = new ArrayList(capacity); } /** * Constructs a simple sequence that will contain the elements * from the specified {@link Collection} and will use the specified object * wrapper. * @param collection the collection containing initial values. Note that a * copy of the collection is made for internal use. * @param wrapper The object wrapper to use to wrap objects into * {@link TemplateModel} instances. If null, the default wrapper set in * {@link WrappingTemplateModel#setDefaultObjectWrapper(ObjectWrapper)} is * used. */ public SimpleSequence(Collection collection, ObjectWrapper wrapper) { super(wrapper); list = new ArrayList(collection); } /** * Adds an arbitrary object to the end of this SimpleSequence. * If the object itself does not implement the {@link TemplateModel} * interface, it will be wrapped into an appropriate adapter on the first * call to {@link #get(int)}. * * @param obj the boolean to be added. */ public void add(Object obj) { list.add(obj); unwrappedList = null; } /** * Adds a boolean to the end of this SimpleSequence, by * coercing the boolean into {@link TemplateBooleanModel#TRUE} or * {@link TemplateBooleanModel#FALSE}. * * @param b the boolean to be added. */ public void add(boolean b) { if (b) { add(TemplateBooleanModel.TRUE); } else { add(TemplateBooleanModel.FALSE); } } /** * 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 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy