View Javadoc
1 2 package jsdsi.util; 3 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.HashSet; 7 import java.util.Map; 8 import java.util.Set; 9 10 /*** 11 * A one-to-many map: each key is associated with a set of values. Note 12 * that get(key) returns the empty set if no value has previously been 13 * put() for that key. 14 * 15 * @author Sameer Ajmani 16 * @author Sean Radford 17 * @version $Revision: 1.1.2.2 $ $Date: 2005/11/08 03:12:52 $ 18 */ 19 public class MultiMap { 20 /*** 21 * Stores the key -> Set bindings. 22 */ 23 Map map = new HashMap(); 24 25 /*** 26 * Returns the <code>Set</code> for a given key. If the key hasn't been 27 * previously inserted with the put-method an empty set is returned. 28 * 29 * @see #put(Object, Object) 30 * 31 * @param key key so return the set for. 32 * @return the set that has been added with key <code>key</code> 33 * previously. 34 */ 35 public Set get(Object key) { 36 Set set = (Set) map.get(key); 37 if (set == null) { 38 set = new HashSet(); 39 map.put(key, set); 40 } 41 return set; 42 } 43 44 /*** 45 * Adds a <code>Set</code> for a given key to this <code>MultiMap</code>. 46 * 47 * @param key key to add the set for. 48 * @param value set to add with key <code>key</code>. 49 */ 50 public void put(Object key, Object value) { 51 get(key).add(value); 52 } 53 54 /*** 55 * Adds the elements of a given collection to the set for a given key in 56 * this <code>MultiMap</code>. 57 * 58 * @param key key to add the collections for. 59 * @param coll collection to add for <code>key</code>. 60 */ 61 public void putAll(Object key, Collection coll) { 62 get(key).addAll(coll); 63 } 64 65 /*** 66 * Remove an object from the set added with the key <code>key</code>. 67 * 68 * @param key key of set to remove <code>value</code> from. 69 * @param value value to remove from the set that has been put with key 70 * <code>key</code>. 71 */ 72 public void remove(Object key, Object value) { 73 get(key).remove(value); 74 } 75 76 /*** 77 * Returns the number of elements in this MultiMap 78 * 79 * @return the number of elements in this MultiMap. 80 */ 81 public int size() { 82 return this.map.size(); 83 } 84 85 /*** 86 * Returns <code>true</code> if this MultiMap contains no elements. 87 * 88 * @return <code>true</code> if this MultiMap contains no elements. 89 */ 90 public boolean isEmpty() { 91 return this.map.isEmpty(); 92 } 93 94 }

This page was automatically generated by Maven