1 5 6 package org.infohazard.maverick.util; 7 8 import java.util.Map ; 9 import java.util.Set ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 13 16 public class NoOverwriteMap implements Map 17 { 18 20 public static class OverwriteException extends IllegalArgumentException 21 { 22 23 protected Object key; 24 25 26 public OverwriteException(Object key) 27 { 28 super("Tried to overwrite key: " + key); 29 30 this.key = key; 31 } 32 33 34 public Object getDuplicateKey() 35 { 36 return this.key; 37 } 38 } 39 40 42 protected Map wrapped; 43 44 46 public NoOverwriteMap(Map wrap) 47 { 48 this.wrapped = wrap; 49 } 50 51 53 public int size() { return wrapped.size(); } 54 public boolean isEmpty() { return wrapped.isEmpty(); } 55 public boolean containsKey(Object key) { return wrapped.containsKey(key); } 56 public boolean containsValue(Object value) { return wrapped.containsValue(value); } 57 public Object get(Object key) { return wrapped.get(key); } 58 public Object remove(Object key) { return wrapped.remove(key); } 59 public void clear() { wrapped.clear(); } 60 public Set keySet() { return wrapped.keySet(); } 61 public Collection values() { return wrapped.values(); } 62 public Set entrySet() { return wrapped.entrySet(); } 63 public boolean equals(Object o) { return wrapped.equals(o); } 64 public int hashCode() { return wrapped.hashCode(); } 65 66 70 public Object put(Object key, Object value) 71 { 72 if (this.containsKey(key)) 73 throw new OverwriteException(key); 74 75 return this.wrapped.put(key, value); 76 } 77 78 82 public void putAll(Map t) 83 { 84 Iterator it = t.entrySet().iterator(); 85 86 while (it.hasNext()) 87 { 88 Map.Entry entry = (Map.Entry )it.next(); 89 this.put(entry.getKey(), entry.getValue()); 90 } 91 } 92 } 93 | Popular Tags |