1 package freemarker.ext.beans; 2 3 import java.util.AbstractMap ; 4 import java.util.AbstractSet ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Set ; 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 21 public class HashAdapter extends AbstractMap implements TemplateModelAdapter { 22 private final BeansWrapper wrapper; 23 private final TemplateHashModel model; 24 private Set 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 get(Object 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 key) { 54 if(get(key) != null) { 56 return true; 57 } 58 return super.containsKey(key); 59 } 60 61 public Set entrySet() { 62 if(entrySet != null) { 63 return entrySet; 64 } 65 return entrySet = new AbstractSet () { 66 public Iterator 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 () { 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 next() { 85 final Object 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 () { 93 public Object getKey() { 94 return key; 95 } 96 97 public Object getValue() { 98 return get(key); 99 } 100 101 public Object setValue(Object value) { 102 throw new UnsupportedOperationException (); 103 } 104 105 public boolean equals(Object o) { 106 if (!(o instanceof Map.Entry )) 107 return false; 108 Map.Entry e = (Map.Entry )o; 109 Object k1 = getKey(); 110 Object k2 = e.getKey(); 111 if (k1 == k2 || (k1 != null && k1.equals(k2))) { 112 Object v1 = getValue(); 113 Object 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 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 (); 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 ( 150 "Operation supported only on TemplateHashModelEx. " + 151 model.getClass().getName() + " does not implement it though."); 152 } 153 154 } 155 | Popular Tags |