1 7 8 package javax.script; 9 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import java.util.Collection ; 13 import java.util.Set ; 14 15 23 public class SimpleBindings implements Bindings { 24 25 28 private Map <String ,Object > map; 29 30 35 public SimpleBindings(Map <String ,Object > m) { 36 if (m == null) { 37 throw new NullPointerException (); 38 } 39 this.map = m; 40 } 41 42 45 public SimpleBindings() { 46 this(new HashMap <String ,Object >()); 47 } 48 49 61 public Object put(String name, Object value) { 62 checkKey(name); 63 return map.put(name,value); 64 } 65 66 76 public void putAll(Map <? extends String , ? extends Object > toMerge) { 77 if (toMerge == null) { 78 throw new NullPointerException ("toMerge map is null"); 79 } 80 for (Map.Entry <? extends String , ? extends Object > entry : toMerge.entrySet()) { 81 String key = entry.getKey(); 82 checkKey(key); 83 put(key, entry.getValue()); 84 } 85 } 86 87 88 public void clear() { 89 map.clear(); 90 } 91 92 107 public boolean containsKey(Object key) { 108 checkKey(key); 109 return map.containsKey(key); 110 } 111 112 113 public boolean containsValue(Object value) { 114 return map.containsValue(value); 115 } 116 117 118 public Set <Map.Entry <String , Object >> entrySet() { 119 return map.entrySet(); 120 } 121 122 143 public Object get(Object key) { 144 checkKey(key); 145 return map.get(key); 146 } 147 148 149 public boolean isEmpty() { 150 return map.isEmpty(); 151 } 152 153 154 public Set <String > keySet() { 155 return map.keySet(); 156 } 157 158 180 public Object remove(Object key) { 181 checkKey(key); 182 return map.remove(key); 183 } 184 185 186 public int size() { 187 return map.size(); 188 } 189 190 191 public Collection <Object > values() { 192 return map.values(); 193 } 194 195 private void checkKey(Object key) { 196 if (key == null) { 197 throw new NullPointerException ("key can not be null"); 198 } 199 if (!(key instanceof String )) { 200 throw new ClassCastException ("key should be a String"); 201 } 202 if (key.equals("")) { 203 throw new IllegalArgumentException ("key can not be empty"); 204 } 205 } 206 } 207 | Popular Tags |