Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/common/util/MultiHashMap.java


1   /*
2    * CommonUtil.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.common.util;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  import java.util.Set;
15  import java.util.Collection;
16  import java.util.ArrayList;
17  import java.util.Iterator;
18  
19  import java.lang.UnsupportedOperationException;
20  
21  /**
22   * <p>Implements {@link java.util.Map} using a {@link java.util.HashMap}.
23   * This Map class allows for multiple values for a single key.</p>
24   *
25   * <p>Below is how the multi map is setup:</p>
26   *
27   * <pre>
28   *  HASH_MAP
29   *  {
30   *    HASH_KEY => (
31   *        new Collection(
32   *          Object, Object, etc...
33   *        )
34   *    ),
35   *
36   *    HASH_KEY => (
37   *        new Collection(
38   *          Object, Object, etc...
39   *        )
40   *    ),
41   *
42   *    etc...
43   *  }
44   * </pre>
45   *
46   * <p>NOTE: This class DOES NOT implement the following:
47   *  <ul>
48   *    <li>public void putAll(Map t)</li>
49   *  </ul>
50   *
51   * @author Scott Milne
52   *
53   */
54  
55  public class MultiHashMap implements Map
56  {
57    /** The {@link java.util.HashMap} that this class uses */
58    private HashMap map;
59  
60  
61    /**
62     * <p>Constructs a new, empty map with a default capacity and load factor, which is 0.75.</p>
63     *
64     */
65  
66    public MultiHashMap()
67    {
68      map = new HashMap();
69    }
70  
71    /**
72     * <p>Constructs a new, empty map with the specified initial capacity and default load factor, which is 0.75.</p>
73     *
74     */
75  
76    public MultiHashMap(int initialCapacity)
77    {
78      map = new HashMap(initialCapacity);
79    }
80  
81    /**
82     * <p>Constructs a new, empty map with the specified initial capacity and the specified load factor.</p>
83     *
84     */
85  
86    public MultiHashMap(int initialCapacity, float loadFactor)
87    {
88      map = new HashMap(initialCapacity, loadFactor);
89    }
90  
91    /**
92     * <p>Determines if the given value matches any of the "collision" values
93     *
94     */
95  
96    public boolean containsCollisionValue(Object value)
97    {
98      // go through each value array checking for the value
99      Iterator valuesIterator = values().iterator();
100     while (valuesIterator.hasNext())
101     {
102       Collection keyList = (Collection)valuesIterator.next();
103       if (keyList.contains(value) )
104       {
105         return true;
106       }
107     }
108 
109     return false;
110   }
111 
112   /**
113    * See {@link java.util.HashMap#put(Object, Object)}.
114    * Places collided values into a Collection instead of replacing previous value.
115    *
116    * @return  Returns the previous Collection.
117    *
118    */
119 
120   public Object put(Object key, Object value)
121   {
122     Object previousValue = null;
123 
124     // check to see if there is already a key for this type
125     if( map.containsKey(key) )
126     {
127       // use the ArrayList and add this item to it
128       Collection keyList = (Collection)map.get(key);
129       keyList.add(value);
130       previousValue = map.put(key, keyList);
131     }
132     else
133     {
134       // create an ArrayList to place into this key
135       map.put( key, new ArrayList() );
136 
137       // add the service to the list
138       Collection keyList = (Collection)map.get(key);
139       keyList.add(value);
140       previousValue = map.put(key, keyList);
141     }
142 
143     return previousValue;
144   }
145 
146   /**
147    * Get a Collection of the collided values at a give key.
148    *
149    * @return                  The Collection at the given key, or null if not found.
150    *
151    */
152 
153   public Collection get(String key)
154   {
155     return (Collection)map.get(key);
156   }
157 
158   /**
159    * Returns the Collection of "collided" values.
160    */
161 
162   public Collection getCollisionList(Object key)
163   {
164     return (Collection)map.get(key);
165   }
166 
167   /**
168    * <p>This operation is not supported.</p>
169    *
170    */
171 
172   public void putAll(Map t)
173     throws UnsupportedOperationException
174   {
175     throw new UnsupportedOperationException();
176   }
177 
178   /** See {@link java.util.HashMap#size()} */
179   public int size()
180   {
181     int total = 0;
182     Iterator keys = map.keySet().iterator();
183 
184     while (keys.hasNext())
185     {
186       String key = (String) keys.next();
187       ArrayList list = (ArrayList) map.get(key);
188       total += list.size();
189     }
190 
191     return total;
192   }
193 
194   /**
195    * Get the size of the collided values for a given key.
196    *
197    */
198 
199   public int size(String key)
200   {
201     ArrayList list = (ArrayList) map.get(key);
202 
203     if (list == null)
204     {
205       return 0;
206     }
207     else
208     {
209       return list.size();
210     }
211   }
212 
213   /** See {@link java.util.HashMap#isEmpty()} */
214   public boolean isEmpty()
215   {
216     return map.isEmpty();
217   }
218 
219   /** See {@link java.util.HashMap#containsValue(Object)} */
220   public boolean containsValue(Object value)
221   {
222     return map.containsValue(value);
223   }
224 
225   /** See {@link java.util.HashMap#containsKey(Object)} */
226   public boolean containsKey(Object key)
227   {
228     return map.containsKey(key);
229   }
230 
231   /** See {@link java.util.HashMap#get(Object)} */
232   public Object get(Object key)
233   {
234     return map.get(key);
235   }
236 
237   /** See {@link java.util.HashMap#remove(Object)} */
238   public Object remove(Object key)
239   {
240     return map.remove(key);
241   }
242 
243   /** See {@link java.util.HashMap#clear()} */
244   public void clear()
245   {
246     map.clear();
247   }
248 
249   /** See {@link java.util.HashMap#clone()} */
250   public Object clone()
251   {
252     return map.clone();
253   }
254 
255   /** See {@link java.util.HashMap#keySet()} */
256   public Set keySet()
257   {
258     return map.keySet();
259   }
260 
261   /** See {@link java.util.HashMap#values()} */
262   public Collection values()
263   {
264     return map.values();
265   }
266 
267   /** See {@link java.util.HashMap#entrySet()} */
268   public Set entrySet()
269   {
270     return map.entrySet();
271   }
272 }
273