freemarker.template.SimpleCollection Maven / Gradle / Ivy
Show all versions of freemarker-gae Show documentation
/*
* 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.Collection;
import java.util.Iterator;
/**
* A simple implementation of {@link TemplateCollectionModel}.
* It's able to wrap java.util.Iterator-s and java.util.Collection-s.
* If you wrap an Iterator, the variable can be <#list>-ed only once!
*
* Consider using {@link SimpleSequence} instead of this class if you want to wrap Iterators.
* SimpleSequence will read all elements of the Iterator, and store them in a List
* (this may cause too high resource consumption in some applications), so you can list the variable
* for unlimited times. Also, if you want to wrap Collections, and then list the resulting
* variable for many times, SimpleSequence may gives better performance, as the
* wrapping of non-TemplateModel objects happens only once.
*
*
This class is thread-safe. The returned TemplateModelIterator-s
* are not thread-safe.
*/
public class SimpleCollection extends WrappingTemplateModel
implements TemplateCollectionModel, Serializable {
private boolean iteratorDirty;
private final Iterator iterator;
private final Collection collection;
/**
* @deprecated Use {@link #SimpleCollection(Iterator, ObjectWrapper)}
*/
public SimpleCollection(Iterator iterator) {
this.iterator = iterator;
collection = null;
}
/**
* @deprecated Use {@link #SimpleCollection(Collection, ObjectWrapper)}
*/
public SimpleCollection(Collection collection) {
this.collection = collection;
iterator = null;
}
public SimpleCollection(Iterator iterator, ObjectWrapper wrapper) {
super(wrapper);
this.iterator = iterator;
collection = null;
}
public SimpleCollection(Collection collection, ObjectWrapper wrapper) {
super(wrapper);
this.collection = collection;
iterator = null;
}
/**
* Retrieves a template model iterator that is used to iterate over the elements in this collection.
*
*
When you wrap an Iterator and you get TemplateModelIterator for multiple times,
* only on of the returned TemplateModelIterator instances can be really used. When you have called a
* method of a TemplateModelIterator instance, all other instance will throw a
* TemplateModelException when you try to call their methods, since the wrapped Iterator
* can't return the first element.
*/
public TemplateModelIterator iterator() {
if (iterator != null) {
return new SimpleTemplateModelIterator(iterator, true);
} else {
synchronized (collection) {
return new SimpleTemplateModelIterator(collection.iterator(), false);
}
}
}
/*
* An instance of this class must be accessed only from a single thread.
* The encapsulated Iterator may accessible from multiple threads (as multiple
* SimpleTemplateModelIterator instance can wrap the same Iterator instance),
* but the first thread which uses the shared Iterator will monopolize that.
*/
private class SimpleTemplateModelIterator implements TemplateModelIterator {
private Iterator iterator;
private boolean iteratorShared;
SimpleTemplateModelIterator(Iterator iterator, boolean iteratorShared) {
this.iterator = iterator;
this.iteratorShared = iteratorShared;
}
public TemplateModel next() throws TemplateModelException {
if (iteratorShared) makeIteratorDirty();
if (!iterator.hasNext()) {
throw new TemplateModelException("The collection has no more elements.");
}
Object value = iterator.next();
if (value instanceof TemplateModel) {
return (TemplateModel) value;
} else {
return wrap(value);
}
}
public boolean hasNext() throws TemplateModelException {
/*
* Theorically this should not make the iterator dirty,
* but I met sync. problems if I don't do it here. :(
*/
if (iteratorShared) makeIteratorDirty();
return iterator.hasNext();
}
private void makeIteratorDirty() throws TemplateModelException {
synchronized (SimpleCollection.this) {
if (iteratorDirty) {
throw new TemplateModelException(
"This collection variable wraps a java.util.Iterator, "
+ "thus it can be -ed or -ed only once");
} else {
iteratorDirty = true;
iteratorShared = false;
}
}
}
}
}