KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > collections > SlottedHashMap


1 package org.jruby.util.collections;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 public class SlottedHashMap implements Map JavaDoc {
12     private SlottedHashMap parent;
13     private Map JavaDoc map;
14     private List JavaDoc children;
15     private Object JavaDoc owner;
16     
17     public class Slot {
18         private Object JavaDoc value;
19
20         public void setValue(Object JavaDoc value) {
21             this.value = value;
22         }
23
24         public Object JavaDoc getValue() {
25             return value;
26         }
27         
28         public SlottedHashMap getParent() {
29             return SlottedHashMap.this;
30         }
31         
32         public String JavaDoc toString() {
33             return "Slot#" + hashCode() + "[parent = " + getParent().hashCode() + ", value = " + getValue() + "]";
34         }
35     }
36     
37     public SlottedHashMap(Object JavaDoc owner) {
38         super();
39         children = new ArrayList JavaDoc();
40         map = new HashMap JavaDoc();
41         this.owner = owner;
42     }
43     
44     public SlottedHashMap(Object JavaDoc owner, SlottedHashMap smap) {
45         this(owner);
46         parent = smap;
47         smap.addChild(this);
48         
49         for (Iterator JavaDoc iter = parent.entrySet().iterator(); iter.hasNext();) {
50             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
51             
52             map.put(entry.getKey(), entry.getValue());
53         }
54     }
55     
56     public void addChild(SlottedHashMap smap) {
57         children.add(smap);
58     }
59     
60     public void setParent(SlottedHashMap smap) {
61         this.parent = smap;
62     }
63
64     /**
65      * Put a new value into this SHM. If the key provided was originally slotted by a parent,
66      * that association will be replaced. If no parents have provided an association we create a new one.
67      * @param key
68      * @param value
69      * @return
70      */

71     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
72         Slot slot = (Slot)map.get(key);
73         if (slot == null || slot.getParent() != this) {
74             slot = new Slot();
75         }
76         
77         slot.setValue(value);
78         
79         // update all children
80
for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
81             Object JavaDoc obj = iter.next();
82             SlottedHashMap shm = (SlottedHashMap)obj;
83             
84             shm.put(key, value, this);
85         }
86         
87         return map.put(key, slot);
88     }
89     
90     /**
91      * Potentially insert the given key and value, if we have not already overridden it
92      *
93      * @param key
94      * @param value
95      * @param parent
96      * @return
97      */

98     protected Object JavaDoc put(Object JavaDoc key, Object JavaDoc value, SlottedHashMap parent) {
99         Slot slot = getSlot(key);
100         if (slot != null) {
101             if (slot.getParent() == this) {
102                 // do not replace, we've overridden
103
return get(key);
104             }
105         }
106
107         slot = parent.getSlot(key);
108         
109         // update children
110
for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
111             SlottedHashMap shm = (SlottedHashMap)iter.next();
112             shm.put(key, value, parent);
113         }
114         
115         return map.put(key, slot);
116     }
117
118     public Object JavaDoc get(Object JavaDoc key) {
119         Slot slot = getSlot(key);
120         
121         if (slot == null) return null;
122         
123         return slot.getValue();
124     }
125     
126     public Slot getSlot(Object JavaDoc key) {
127         return (Slot)map.get(key);
128     }
129
130     public int size() {
131         return map.size();
132     }
133
134     public void clear() {
135         throw new RuntimeException JavaDoc("clear not implemented");
136     }
137
138     public boolean isEmpty() {
139         return map.isEmpty();
140     }
141
142     public boolean containsKey(Object JavaDoc key) {
143         return map.containsKey(key);
144     }
145
146     public boolean containsValue(Object JavaDoc value) {
147         // TODO Auto-generated method stub
148
return false;
149     }
150
151     public Collection JavaDoc values() {
152         // TODO Auto-generated method stub
153
return null;
154     }
155
156     public void putAll(Map JavaDoc t) {
157         // TODO Auto-generated method stub
158

159     }
160
161     public Set JavaDoc entrySet() {
162         return map.entrySet();
163     }
164
165     public Set JavaDoc keySet() {
166         return map.keySet();
167     }
168
169     public Object JavaDoc remove(Object JavaDoc key) {
170         Slot slot = getSlot(key);
171         
172         if (slot != null) {
173             if (slot.getParent() == this) {
174                 map.remove(key);
175                 
176                 // update children
177
for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
178                     SlottedHashMap child = (SlottedHashMap)iter.next();
179                     
180                     child.remove(key, this);
181                 }
182                 
183                 return slot.getValue();
184             } else {
185                 // do nothing, we have not overridden this
186
}
187         }
188         
189         return null;
190     }
191     
192     protected void remove(Object JavaDoc key, SlottedHashMap parent) {
193         Slot slot = getSlot(key);
194         
195         if (slot.getParent() == this) {
196             // do nothing, we've overridden parent
197
return;
198         } else {
199             map.remove(key);
200         }
201         
202         // update children
203
for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
204             SlottedHashMap child = (SlottedHashMap)iter.next();
205             
206             child.remove(key, parent);
207         }
208     }
209
210     public Object JavaDoc getOwner() {
211         return owner;
212     }
213 }
214
Popular Tags