KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > beans > CollectionAdapter


1 package freemarker.ext.beans;
2
3 import java.util.AbstractCollection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import freemarker.template.TemplateCollectionModel;
7 import freemarker.template.TemplateModel;
8 import freemarker.template.TemplateModelAdapter;
9 import freemarker.template.TemplateModelException;
10 import freemarker.template.TemplateModelIterator;
11 import freemarker.template.utility.UndeclaredThrowableException;
12
13 /**
14  * @author Attila Szegedi
15  * @version $Id: CollectionAdapter.java,v 1.2 2005/06/12 19:03:04 szegedia Exp $
16  */

17 class CollectionAdapter extends AbstractCollection JavaDoc implements TemplateModelAdapter {
18     private final BeansWrapper wrapper;
19     private final TemplateCollectionModel model;
20     
21     CollectionAdapter(TemplateCollectionModel model, BeansWrapper wrapper) {
22         this.model = model;
23         this.wrapper = wrapper;
24     }
25     
26     public TemplateModel getTemplateModel() {
27         return model;
28     }
29     
30     public int size() {
31         throw new UnsupportedOperationException JavaDoc();
32     }
33
34     public Iterator JavaDoc iterator() {
35         try {
36             return new Iterator JavaDoc() {
37                 final TemplateModelIterator i = model.iterator();
38     
39                 public boolean hasNext() {
40                     try {
41                         return i.hasNext();
42                     }
43                     catch(TemplateModelException e) {
44                         throw new UndeclaredThrowableException(e);
45                     }
46                 }
47                 
48                 public Object JavaDoc next() {
49                     try {
50                         return wrapper.unwrap(i.next());
51                     }
52                     catch(TemplateModelException e) {
53                         throw new UndeclaredThrowableException(e);
54                     }
55                 }
56                 
57                 public void remove() {
58                     throw new UnsupportedOperationException JavaDoc();
59                 }
60             };
61         }
62         catch(TemplateModelException e) {
63             throw new UndeclaredThrowableException(e);
64         }
65     }
66 }
67
Popular Tags