KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > HashMultiMap


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.util;
21 import java.util.*;
22
23 /** A map with sets as values, HashMap implementation.
24  *
25  * @author Ondrej Lhotak
26  */

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 JavaDoc 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 JavaDoc key ) {
49     return m.containsKey( key );
50     }
51     public boolean containsValue( Object JavaDoc 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 JavaDoc 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 JavaDoc key, Object JavaDoc value ) {
71     return findSet( key ).add( value );
72     }
73     public boolean putAll( Object JavaDoc key, Set values ) {
74     if (values.isEmpty()) return false;
75     return findSet( key ).addAll( values );
76     }
77     public boolean remove( Object JavaDoc key, Object JavaDoc 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 JavaDoc key ) {
87     return null != m.remove( key );
88     }
89     public boolean removeAll( Object JavaDoc 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 JavaDoc 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 JavaDoc 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