KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > util > NoOverwriteMap


1 /*
2  * $Id: NoOverwriteMap.java,v 1.1 2002/01/03 00:51:06 lhoriman Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/util/NoOverwriteMap.java,v $
4  */

5
6 package org.infohazard.maverick.util;
7
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Map wrapper which prevents overwriting existing keys.
15  */

16 public class NoOverwriteMap implements Map JavaDoc
17 {
18     /**
19      */

20     public static class OverwriteException extends IllegalArgumentException JavaDoc
21     {
22         /** */
23         protected Object JavaDoc key;
24
25         /** */
26         public OverwriteException(Object JavaDoc key)
27         {
28             super("Tried to overwrite key: " + key);
29
30             this.key = key;
31         }
32
33         /** */
34         public Object JavaDoc getDuplicateKey()
35         {
36             return this.key;
37         }
38     }
39
40     /**
41      */

42     protected Map JavaDoc wrapped;
43
44     /**
45      */

46     public NoOverwriteMap(Map JavaDoc wrap)
47     {
48         this.wrapped = wrap;
49     }
50
51     /**
52      */

53     public int size() { return wrapped.size(); }
54     public boolean isEmpty() { return wrapped.isEmpty(); }
55     public boolean containsKey(Object JavaDoc key) { return wrapped.containsKey(key); }
56     public boolean containsValue(Object JavaDoc value) { return wrapped.containsValue(value); }
57     public Object JavaDoc get(Object JavaDoc key) { return wrapped.get(key); }
58     public Object JavaDoc remove(Object JavaDoc key) { return wrapped.remove(key); }
59     public void clear() { wrapped.clear(); }
60     public Set JavaDoc keySet() { return wrapped.keySet(); }
61     public Collection JavaDoc values() { return wrapped.values(); }
62     public Set JavaDoc entrySet() { return wrapped.entrySet(); }
63     public boolean equals(Object JavaDoc o) { return wrapped.equals(o); }
64     public int hashCode() { return wrapped.hashCode(); }
65
66     /**
67      * Prevents overwriting existing keys.
68      * @throws OverwriteException if the key is already present.
69      */

70     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
71     {
72         if (this.containsKey(key))
73             throw new OverwriteException(key);
74
75         return this.wrapped.put(key, value);
76     }
77
78     /**
79      * Prevents overwriting existing keys.
80      * @throws OverwriteException if the key is already present.
81      */

82     public void putAll(Map JavaDoc t)
83     {
84         Iterator JavaDoc it = t.entrySet().iterator();
85
86         while (it.hasNext())
87         {
88             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
89             this.put(entry.getKey(), entry.getValue());
90         }
91     }
92 }
93
Popular Tags