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

freemarker.template.SimpleSequence Maven / Gradle / Ivy

There is a newer version: 2.3.32_1
Show newest version
/*
 * Copyright (c) 2003 The Visigoth Software Society. All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowledgement:
 *       "This product includes software developed by the
 *        Visigoth Software Society (http://www.visigoths.org/)."
 *    Alternately, this acknowledgement may appear in the software itself,
 *    if and wherever such third-party acknowledgements normally appear.
 *
 * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the 
 *    project contributors may be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [email protected].
 *
 * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
 *    nor may "FreeMarker" or "Visigoth" appear in their names
 *    without prior written permission of the Visigoth Software Society.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Visigoth Software Society. For more
 * information on the Visigoth Software Society, please see
 * http://www.visigoths.org/
 */

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. * *

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

* * @version $Id: SimpleSequence.java,v 1.53 2005/06/21 18:17:54 ddekany Exp $ * @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)}. */ 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)}. */ 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. */ 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 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