1   
15  
16  package gate.util;
17  
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  import java.util.*;
21  
22  
26  
27  class SimpleMapImpl implements Map, java.lang.Cloneable, java.io.Serializable {
28  
29    
32    int capacity = 3;
33  
34    
37    int count = 0;
38  
39    
44    Object theKeys[];
45  
46    
51    Object theValues[];
52  
53    
54    static final long serialVersionUID = -6747241616127229116L;
55  
56    
57    transient static Object nullKey = new Object();
58  
59    
60    transient public static HashMap theKeysHere = new HashMap();
61  
62    
63    static {
64      theKeysHere.put(nullKey, nullKey);
65    } 
67    
70    public SimpleMapImpl() {
71      theKeys = new Object[capacity];
72      theValues = new Object[capacity];
73    } 
75    
78    public int size() {
79      return count;
80    } 
82    
85    public boolean isEmpty() {
86      return (count == 0);
87    } 
89    
92    public Collection values() {
93      throw new UnsupportedOperationException(
94        "SimpleMapImpl.values() not implemented!");
95    } 
97    
101   public Set keySet()
102   {
103     HashSet s = new HashSet(size());
104     Object k;
105     for (int i = 0; i < count; i++) {
106       k = theKeys[i];
107       if (k == nullKey)
108            s.add(null);
109         else
110            s.add(k);
111     }     return s;
113   } 
115   
118   public void clear()
119   {
120     for (int i = 0; i < count; i++) {
121       theKeys[i] = null;
122       theValues[i] = null;
123     }     count = 0;
125   } 
127   
130   public boolean containsKey(Object key) {
131     return (getPostionByKey(key) != -1);
132   }
134   
137   public boolean containsValue(Object value) {
138     return (getPostionByValue(value) != -1);
139   }
141   
145   public Object get(Object key) {
146     int pos = getPostionByKey(key);
147     return (pos == -1) ? null : theValues[pos];
148   } 
150   
154   public Object put(Object key, Object value) {
155     Object gKey;
156     if (key == null) {
157       key = nullKey;
158       gKey = nullKey;
159     } else
160       gKey = theKeysHere.get(key);
161             if (gKey != null) {
164       for (int i = 0; i < count; i++) {
165         if (gKey == theKeys[i]) {
166                     Object oldVal = theValues[i];
168           theValues[i] = value;
169           return oldVal;
170         }
171       }     } else {            theKeysHere.put(key, key);
175       gKey = key;
176     }
177         if (count == capacity)
179       increaseCapacity();
180 
181         theKeys[count] = gKey;
183     theValues[count] = value;
184     count++;
185     return null;
186   } 
188   
191   public Object remove(Object key) {
192     int pos = getPostionByKey(key);
193     if (pos == -1)
194         return null;
195 
196         Object oldVal = theValues[pos];
198     count--;
199         if (count != 0) {
201         theKeys[pos] = theKeys[count];
202         theValues[pos] = theValues[count];
203     }
204         theKeys[count] = null;
206     theValues[count] = null;
207 
208         return oldVal;
210   } 
212   
215   public void putAll(Map t)
216   {
217     if (t == null) {
218       throw new UnsupportedOperationException(
219       "SimpleMapImpl.putAll argument is null");
220     } 
222     if (t instanceof SimpleMapImpl) {
223       SimpleMapImpl sfm = (SimpleMapImpl)t;
224       Object key;
225       for (int i = 0; i < sfm.count; i++) {
226         key = sfm.theKeys[i];
227         put(key, sfm.theValues[i]);
228       }     } else {       Iterator entries = t.entrySet().iterator();
231       Map.Entry e;
232       while (entries.hasNext()) {
233         e = (Map.Entry)entries.next();
234         put(e.getKey(), e.getValue());
235       }     }   } 
239   
243   private int getPostionByKey(Object key) {
244     if (key == null)
245       key = nullKey;
246         key = theKeysHere.get(key);
248     if (key == null)
249       return -1;
250 
251     for (int i = 0; i < count; i++) {
252       if (key == theKeys[i])
253         return i;
254     }     return -1;
256   } 
258   
262   protected int getSubsumeKey(Object key) {
263     for (int i = 0; i < count; i++) {
264       if (key == theKeys[i])
265         return i;
266     }     return -1;
268   } 
270   
273   private int getPostionByValue(Object value) {
274     Object av;
275     for (int i = 0; i < count; i++) {
276       av = theValues[i];
277       if (value == null) {
278         if (av == null)
279           return i;
280       } else {        if (value.equals(av))
282           return i;
283       }     } 
286     return -1;
287   } 
289     private void increaseCapacity() {
291     int oldCapacity = capacity;
292     capacity *= 2;
293     Object oldKeys[] = theKeys;
294     theKeys = new Object[capacity];
295 
296     Object oldValues[] = theValues;
297     theValues = new Object[capacity];
298 
299     System.arraycopy(oldKeys, 0, theKeys, 0, oldCapacity);
300     System.arraycopy(oldValues, 0, theValues, 0, oldCapacity);
301   } 
303   
306   private static class Entry implements Map.Entry {
307     int hash;
308     Object key;
309     Object value;
310 
311     Entry(int hash, Object key, Object value) {
312       this.hash = hash;
313       this.key = key;
314       this.value = value;
315     }
316 
317     protected Object clone() {
318       return new Entry(hash, key, value);
319     }
320 
321     public Object getKey() {
322       return key;
323     }
324 
325     public Object getValue() {
326       return value;
327     }
328 
329     public Object setValue(Object value) {
330       Object oldValue = this.value;
331       this.value = value;
332       return oldValue;
333     }
334 
335     public boolean equals(Object o) {
336       if (!(o instanceof Map.Entry))
337         return false;
338       Map.Entry e = (Map.Entry)o;
339 
340       return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
341         (value==null ? e.getValue()==null : value.equals(e.getValue()));
342     }
343 
344     public int hashCode() {
345       return hash ^ (key==null ? 0 : key.hashCode());
346     }
347 
348     public String toString() {
349       return key+"="+value;
350     }
351   } 
353 
354   public Set entrySet() {
355     HashSet s = new HashSet(size());
356     Object v, k;
357     for (int i = 0; i < count; i++) {
358       k = theKeys[i];
359       s.add(new Entry(k.hashCode(), ((k==nullKey)?null:k), theValues[i]));
360     }     return s;
362   } 
364     public boolean equals(Object o) {
366     if (!(o instanceof Map)) {
367       return false;
368     }
369 
370     Map m = (Map)o;
371     if (m.size() != count) {
372       return false;
373     }
374 
375     Object v, k;
376     for (int i = 0; i < count; i++) {
377       k = theKeys[i];
378       v = m.get(k);
379       if (v==null) {
380         if (theValues[i]!=null)
381           return false;
382       }
383       else if (!v.equals(theValues[i])){
384         return false;
385       }
386     } 
388     return true;
389   } 
391   
394   public int hashCode() {
395     int h = 0;
396     Iterator i = entrySet().iterator();
397     while (i.hasNext())
398       h += i.next().hashCode();
399     return h;
400   } 
402   
405   public Object clone() {
406     SimpleMapImpl newMap;
407     try {
408       newMap = (SimpleMapImpl)super.clone();
409     } catch (CloneNotSupportedException e) {
410       throw(new InternalError(e.toString()));
411     }
412 
413     newMap.count = count;
414     newMap.theKeys = new Object[capacity];
415     System.arraycopy(theKeys, 0, newMap.theKeys, 0, capacity);
416 
417     newMap.theValues = new Object[capacity];
418     System.arraycopy(theValues, 0, newMap.theValues, 0, capacity);
419 
420     return newMap;
421   } 
423   public String toString() {
424     int max = size() - 1;
425     StringBuffer buf = new StringBuffer();
426     Iterator i = entrySet().iterator();
427 
428     buf.append("{");
429     for (int j = 0; j <= max; j++) {
430       Entry e = (Entry) (i.next());
431       buf.append(e.getKey() + "=" + e.getValue());
432       if (j < max)
433         buf.append(", ");
434     }
435     buf.append("}");
436     return buf.toString();
437   } 
439   
445   private void readObject(ObjectInputStream s)
446       throws IOException, ClassNotFoundException {
447     s.defaultReadObject();
448     if (theKeysHere == null) {
449       theKeysHere = new HashMap();
450       theKeysHere.put(nullKey, nullKey);
451     }
452     for (int i = 0; i < theKeys.length; i++) {
453             Object o = theKeysHere.get(theKeys[i]);
455       if (o != null)         theKeys[i] = o;
457       else         theKeysHere.put(theKeys[i], theKeys[i]);
459     }  }}