1 52 53 package freemarker.template; 54 55 import java.io.Serializable ; 56 import java.util.ArrayList ; 57 import java.util.Collection ; 58 import java.util.Collections ; 59 import java.util.List ; 60 61 import freemarker.ext.beans.BeansWrapper; 62 63 85 public class SimpleSequence extends WrappingTemplateModel 86 implements TemplateSequenceModel, Serializable { 87 88 91 protected final List list; 92 private List unwrappedList; 93 94 99 public SimpleSequence() { 100 this((ObjectWrapper) null); 101 } 102 103 108 public SimpleSequence(int capacity) { 109 list = new ArrayList (capacity); 110 } 111 112 120 public SimpleSequence(Collection collection) { 121 this(collection, null); 122 } 123 124 129 public SimpleSequence(TemplateCollectionModel tcm) throws TemplateModelException { 130 ArrayList alist = new ArrayList (); 131 for (TemplateModelIterator it = tcm.iterator(); it.hasNext();) { 132 alist.add(it.next()); 133 } 134 alist.trimToSize(); 135 list = alist; 136 } 137 138 145 public SimpleSequence(ObjectWrapper wrapper) { 146 super(wrapper); 147 list = new ArrayList (); 148 } 149 150 161 public SimpleSequence(Collection collection, ObjectWrapper wrapper) { 162 super(wrapper); 163 list = new ArrayList (collection); 164 } 165 166 174 public void add(Object obj) { 175 list.add(obj); 176 unwrappedList = null; 177 } 178 179 186 public void add(boolean b) { 187 if (b) { 188 add(TemplateBooleanModel.TRUE); 189 } 190 else { 191 add(TemplateBooleanModel.FALSE); 192 } 193 } 194 195 202 public List toList() throws TemplateModelException { 203 if (unwrappedList == null) { 204 Class listClass = list.getClass(); 205 List result = null; 206 try { 207 result = (List ) listClass.newInstance(); 208 } catch (Exception e) { 209 throw new TemplateModelException("Error instantiating an object of type " + listClass.getName() + "\n" + e.getMessage()); 210 } 211 BeansWrapper bw = BeansWrapper.getDefaultInstance(); 212 for (int i=0; i<list.size(); i++) { 213 Object elem = list.get(i); 214 if (elem instanceof TemplateModel) { 215 elem = bw.unwrap((TemplateModel) elem); 216 } 217 result.add(elem); 218 } 219 unwrappedList = result; 220 } 221 return unwrappedList; 222 } 223 224 227 public TemplateModel get(int i) throws TemplateModelException { 228 try { 229 Object value = list.get(i); 230 if (value instanceof TemplateModel) { 231 return (TemplateModel) value; 232 } 233 TemplateModel tm = wrap(value); 234 list.set(i, tm); 235 return tm; 236 } 237 catch(IndexOutOfBoundsException e) { 238 return null; 239 } 241 } 242 243 public int size() { 244 return list.size(); 245 } 246 247 250 public SimpleSequence synchronizedWrapper() { 251 return new SynchronizedSequence(); 252 } 253 254 public String toString() { 255 return list.toString(); 256 } 257 258 private class SynchronizedSequence extends SimpleSequence { 259 260 public void add(Object obj) { 261 synchronized (SimpleSequence.this) { 262 SimpleSequence.this.add(obj); 263 } 264 } 265 266 public TemplateModel get(int i) throws TemplateModelException { 267 synchronized (SimpleSequence.this) { 268 return SimpleSequence.this.get(i); 269 } 270 } 271 272 public int size() { 273 synchronized (SimpleSequence.this) { 274 return SimpleSequence.this.size(); 275 } 276 } 277 public List toList() throws TemplateModelException { 278 synchronized (SimpleSequence.this) { 279 return SimpleSequence.this.toList(); 280 } 281 } 282 } 283 } | Popular Tags |