KickJava   Java API By Example, From Geeks To Geeks.

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


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

21 public class HashAdapter extends AbstractMap JavaDoc implements TemplateModelAdapter {
22     private final BeansWrapper wrapper;
23     private final TemplateHashModel model;
24     private Set JavaDoc entrySet;
25     
26     HashAdapter(TemplateHashModel model, BeansWrapper wrapper) {
27         this.model = model;
28         this.wrapper = wrapper;
29     }
30     
31     public TemplateModel getTemplateModel() {
32         return model;
33     }
34     
35     public boolean isEmpty() {
36         try {
37             return model.isEmpty();
38         }
39         catch(TemplateModelException e) {
40             throw new UndeclaredThrowableException(e);
41         }
42     }
43     
44     public Object JavaDoc get(Object JavaDoc key) {
45         try {
46             return wrapper.unwrap(model.get(String.valueOf(key)));
47         }
48         catch(TemplateModelException e) {
49             throw new UndeclaredThrowableException(e);
50         }
51     }
52
53     public boolean containsKey(Object JavaDoc key) {
54         // A quick check that doesn't require TemplateHashModelEx
55
if(get(key) != null) {
56             return true;
57         }
58         return super.containsKey(key);
59     }
60     
61     public Set JavaDoc entrySet() {
62         if(entrySet != null) {
63             return entrySet;
64         }
65         return entrySet = new AbstractSet JavaDoc() {
66             public Iterator JavaDoc iterator() {
67                 final TemplateModelIterator i;
68                 try {
69                      i = getModelEx().keys().iterator();
70                 }
71                 catch(TemplateModelException e) {
72                     throw new UndeclaredThrowableException(e);
73                 }
74                 return new Iterator JavaDoc() {
75                     public boolean hasNext() {
76                         try {
77                             return i.hasNext();
78                         }
79                         catch(TemplateModelException e) {
80                             throw new UndeclaredThrowableException(e);
81                         }
82                     }
83                     
84                     public Object JavaDoc next() {
85                         final Object JavaDoc key;
86                         try {
87                             key = wrapper.unwrap(i.next());
88                         }
89                         catch(TemplateModelException e) {
90                             throw new UndeclaredThrowableException(e);
91                         }
92                         return new Map.Entry JavaDoc() {
93                             public Object JavaDoc getKey() {
94                                 return key;
95                             }
96                             
97                             public Object JavaDoc getValue() {
98                                 return get(key);
99                             }
100                             
101                             public Object JavaDoc setValue(Object JavaDoc value) {
102                                 throw new UnsupportedOperationException JavaDoc();
103                             }
104                             
105                             public boolean equals(Object JavaDoc o) {
106                                 if (!(o instanceof Map.Entry JavaDoc))
107                                     return false;
108                                 Map.Entry JavaDoc e = (Map.Entry JavaDoc)o;
109                                 Object JavaDoc k1 = getKey();
110                                 Object JavaDoc k2 = e.getKey();
111                                 if (k1 == k2 || (k1 != null && k1.equals(k2))) {
112                                     Object JavaDoc v1 = getValue();
113                                     Object JavaDoc v2 = e.getValue();
114                                     if (v1 == v2 || (v1 != null && v1.equals(v2)))
115                                         return true;
116                                 }
117                                 return false;
118                             }
119                         
120                             public int hashCode() {
121                                 Object JavaDoc value = getValue();
122                                 return (key==null ? 0 : key.hashCode()) ^
123                                        (value==null ? 0 : value.hashCode());
124                             }
125                         };
126                     }
127                     
128                     public void remove() {
129                         throw new UnsupportedOperationException JavaDoc();
130                     }
131                 };
132             }
133             
134             public int size() {
135                 try {
136                     return getModelEx().size();
137                 }
138                 catch(TemplateModelException e) {
139                     throw new UndeclaredThrowableException(e);
140                 }
141             }
142         };
143     }
144     
145     private TemplateHashModelEx getModelEx() {
146         if(model instanceof TemplateHashModelEx) {
147             return ((TemplateHashModelEx)model);
148         }
149         throw new UnsupportedOperationException JavaDoc(
150                 "Operation supported only on TemplateHashModelEx. " +
151                 model.getClass().getName() + " does not implement it though.");
152     }
153     
154 }
155
Popular Tags