| 1 19 20 package soot.util; 21 import java.util.*; 22 23 27 28 public class HashMultiMap implements MultiMap { 29 Map m = new HashMap(0); 30 31 public HashMultiMap() {} 32 public HashMultiMap( MultiMap m ) { 33 putAll( m ); 34 } 35 public void putAll( MultiMap m ) { 36 Iterator it = m.keySet().iterator(); 37 while( it.hasNext() ) { 38 Object o = it.next(); 39 putAll( o, m.get( o ) ); 40 } 41 } 42 public boolean isEmpty() { 43 return numKeys() == 0; 44 } 45 public int numKeys() { 46 return m.size(); 47 } 48 public boolean containsKey( Object key ) { 49 return m.containsKey( key ); 50 } 51 public boolean containsValue( Object value ) { 52 Iterator it = m.values().iterator(); 53 while( it.hasNext() ) { 54 Set s = (Set) it.next(); 55 if( s.contains( value ) ) return true; 56 } 57 return false; 58 } 59 protected Set newSet() { 60 return new HashSet(4); 61 } 62 private Set findSet( Object key ) { 63 Set s = (Set) m.get( key ); 64 if( s == null ) { 65 s = newSet(); 66 m.put( key, s ); 67 } 68 return s; 69 } 70 public boolean put( Object key, Object value ) { 71 return findSet( key ).add( value ); 72 } 73 public boolean putAll( Object key, Set values ) { 74 if (values.isEmpty()) return false; 75 return findSet( key ).addAll( values ); 76 } 77 public boolean remove( Object key, Object value ) { 78 Set s = (Set) m.get( key ); 79 if( s == null ) return false; 80 boolean ret = s.remove( value ); 81 if( s.isEmpty() ) { 82 m.remove( key ); 83 } 84 return ret; 85 } 86 public boolean remove( Object key ) { 87 return null != m.remove( key ); 88 } 89 public boolean removeAll( Object key, Set values ) { 90 Set s = (Set) m.get( key ); 91 if( s == null ) return false; 92 boolean ret = s.removeAll( values ); 93 if( s.isEmpty() ) { 94 m.remove( key ); 95 } 96 return ret; 97 } 98 public Set get( Object o ) { 99 Set ret = (Set) m.get( o ); 100 if( ret == null ) return Collections.EMPTY_SET; 101 return Collections.unmodifiableSet(ret); 102 } 103 public Set keySet() { 104 return m.keySet(); 105 } 106 public Set values() { 107 Set ret = new HashSet(0); 108 Iterator it = m.values().iterator(); 109 while( it.hasNext() ) { 110 Set s = (Set) it.next(); 111 ret.addAll( s ); 112 } 113 return ret; 114 } 115 public boolean equals( Object o ) { 116 if( ! (o instanceof MultiMap) ) return false; 117 MultiMap mm = (MultiMap) o; 118 if( !keySet().equals( mm.keySet() ) ) return false; 119 Iterator it = m.entrySet().iterator(); 120 while( it.hasNext() ) { 121 Map.Entry e = (Map.Entry) it.next(); 122 Set s = (Set) e.getValue(); 123 if( !s.equals( mm.get( e.getKey() ) ) ) return false; 124 } 125 return true; 126 } 127 public int hashCode() { 128 return m.hashCode(); 129 } 130 } 131 | Popular Tags |