1 /*
2 * Copyright 2002 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this program for any
5 * purpose and without fee is hereby granted, provided that this
6 * copyright and permission notice appear on all copies and supporting
7 * documentation, the name of M.I.T. not be used in advertising or
8 * publicity pertaining to distribution of the program without specific
9 * prior permission, and notice be given in supporting documentation that
10 * copying and distribution is by permission of M.I.T. M.I.T. makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 */
14 package jsdsi;
15
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21
22 /***
23 * A one-to-many map: each key is associated with a set of values. Note
24 * that get(key) returns the empty set if no value has previously been
25 * put() for that key.
26 *
27 * @author Sameer Ajmani
28 * @version $Revision: 1.1 $ $Date: 2004/02/28 15:49:34 $
29 */
30 class MultiMap {
31 /***
32 * Stores the key -> Set bindings.
33 */
34 Map map = new HashMap();
35
36 /***
37 * Returns the <code>Set</code> for a given key. If the key hasn't been
38 * previously inserted with the put-method an empty set is returned.
39 *
40 * @see #put(Object, Object)
41 *
42 * @param key key so return the set for.
43 * @return the set that has been added with key <code>key</code>
44 * previously.
45 */
46 public Set get(Object key) {
47 Set set = (Set) map.get(key);
48 if (set == null) {
49 set = new HashSet();
50 map.put(key, set);
51 }
52 return set;
53 }
54
55 /***
56 * Adds a <code>Set</code> for a given key to this <code>MultiMap</code>.
57 *
58 * @param key key to add the set for.
59 * @param value set to add with key <code>key</code>.
60 */
61 public void put(Object key, Object value) {
62 get(key).add(value);
63 }
64
65 /***
66 * Adds the elements of a given collection to the set for a given key in
67 * this <code>MultiMap</code>.
68 *
69 * @param key key to add the collections for.
70 * @param coll collection to add for <code>key</code>.
71 */
72 public void putAll(Object key, Collection coll) {
73 get(key).addAll(coll);
74 }
75
76 /***
77 * Remove an object from the set added with the key <code>key</code>.
78 *
79 * @param key key of set to remove <code>value</code> from.
80 * @param value value to remove from the set that has been put with key
81 * <code>key</code>.
82 */
83 public void remove(Object key, Object value) {
84 get(key).remove(value);
85 }
86 }
This page was automatically generated by Maven