001: package freemarker.ext.beans;
002:
003: import java.util.AbstractMap;
004: import java.util.AbstractSet;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import freemarker.template.TemplateHashModel;
010: import freemarker.template.TemplateHashModelEx;
011: import freemarker.template.TemplateModel;
012: import freemarker.template.TemplateModelAdapter;
013: import freemarker.template.TemplateModelException;
014: import freemarker.template.TemplateModelIterator;
015: import freemarker.template.utility.UndeclaredThrowableException;
016:
017: /**
018: * @author Attila Szegedi
019: * @version $Id: HashAdapter.java,v 1.2 2005/06/12 19:03:04 szegedia Exp $
020: */
021: public class HashAdapter extends AbstractMap implements
022: TemplateModelAdapter {
023: private final BeansWrapper wrapper;
024: private final TemplateHashModel model;
025: private Set entrySet;
026:
027: HashAdapter(TemplateHashModel model, BeansWrapper wrapper) {
028: this .model = model;
029: this .wrapper = wrapper;
030: }
031:
032: public TemplateModel getTemplateModel() {
033: return model;
034: }
035:
036: public boolean isEmpty() {
037: try {
038: return model.isEmpty();
039: } catch (TemplateModelException e) {
040: throw new UndeclaredThrowableException(e);
041: }
042: }
043:
044: public Object get(Object key) {
045: try {
046: return wrapper.unwrap(model.get(String.valueOf(key)));
047: } catch (TemplateModelException e) {
048: throw new UndeclaredThrowableException(e);
049: }
050: }
051:
052: public boolean containsKey(Object key) {
053: // A quick check that doesn't require TemplateHashModelEx
054: if (get(key) != null) {
055: return true;
056: }
057: return super .containsKey(key);
058: }
059:
060: public Set entrySet() {
061: if (entrySet != null) {
062: return entrySet;
063: }
064: return entrySet = new AbstractSet() {
065: public Iterator iterator() {
066: final TemplateModelIterator i;
067: try {
068: i = getModelEx().keys().iterator();
069: } catch (TemplateModelException e) {
070: throw new UndeclaredThrowableException(e);
071: }
072: return new Iterator() {
073: public boolean hasNext() {
074: try {
075: return i.hasNext();
076: } catch (TemplateModelException e) {
077: throw new UndeclaredThrowableException(e);
078: }
079: }
080:
081: public Object next() {
082: final Object key;
083: try {
084: key = wrapper.unwrap(i.next());
085: } catch (TemplateModelException e) {
086: throw new UndeclaredThrowableException(e);
087: }
088: return new Map.Entry() {
089: public Object getKey() {
090: return key;
091: }
092:
093: public Object getValue() {
094: return get(key);
095: }
096:
097: public Object setValue(Object value) {
098: throw new UnsupportedOperationException();
099: }
100:
101: public boolean equals(Object o) {
102: if (!(o instanceof Map.Entry))
103: return false;
104: Map.Entry e = (Map.Entry) o;
105: Object k1 = getKey();
106: Object k2 = e.getKey();
107: if (k1 == k2
108: || (k1 != null && k1.equals(k2))) {
109: Object v1 = getValue();
110: Object v2 = e.getValue();
111: if (v1 == v2
112: || (v1 != null && v1
113: .equals(v2)))
114: return true;
115: }
116: return false;
117: }
118:
119: public int hashCode() {
120: Object value = getValue();
121: return (key == null ? 0 : key
122: .hashCode())
123: ^ (value == null ? 0 : value
124: .hashCode());
125: }
126: };
127: }
128:
129: public void remove() {
130: throw new UnsupportedOperationException();
131: }
132: };
133: }
134:
135: public int size() {
136: try {
137: return getModelEx().size();
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(
150: "Operation supported only on TemplateHashModelEx. "
151: + model.getClass().getName()
152: + " does not implement it though.");
153: }
154:
155: }
|