KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > element > MapStore


1 package jfun.yan.element;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import jfun.util.Misc;
7 import jfun.util.StringUtils;
8
9 /**
10  * This implementation stores component instances into a java.util.Map object.
11  * <p>
12  * @author Ben Yu
13  * Nov 12, 2005 2:30:31 PM
14  */

15 public class MapStore<K,T> implements ElementStore<T> {
16   private final java.util.Map JavaDoc<K,T> store;
17   private final K[] keys;
18   
19   /**
20    * Create a MapStore object.
21    * @param keys the keys used to save component instances into the map.
22    * @param store the map object.
23    */

24   public MapStore(K[] keys, Map JavaDoc<K,T> store) {
25     this.keys = keys;
26     this.store = store;
27   }
28
29   public void storeElement(int ind, T obj) {
30     store.put(keys[ind], obj);
31   }
32
33   public void checkElement(int ind, Class JavaDoc type) {
34     if(ind > keys.length)
35       throw new ArrayIndexOutOfBoundsException JavaDoc(ind);
36   }
37
38   /**
39    * Get the keys used to save component instances into map.
40    */

41   public Object JavaDoc[] getKeys() {
42     return keys;
43   }
44
45   /**
46    * Get the map storing the component instances.
47    */

48   public java.util.Map JavaDoc<K,T> getMap() {
49     return store;
50   }
51
52   public boolean equals(Object JavaDoc obj) {
53     if(obj instanceof MapStore){
54       final MapStore other = (MapStore)obj;
55       return store==other.store && Arrays.equals(keys, other.keys);
56     }
57     else return false;
58   }
59
60   public int hashCode() {
61     int hcode = System.identityHashCode(store);
62     for(int i=0; i<keys.length; i++){
63       hcode = hcode*31+keys[i].hashCode();
64     }
65     return hcode;
66   }
67
68   public String JavaDoc toString() {
69     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
70     buf.append(Misc.getTypeName(store.getClass()));
71     StringUtils.listArray(buf, "{", ",", "}", keys);
72     return buf.toString();
73   }
74
75 }
76
Popular Tags