KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > IterableSet


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Sable Research Group
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.util;
28
29 import java.util.*;
30
31 public class IterableSet extends HashChain implements Set
32 {
33     public IterableSet( Collection c)
34     {
35     super();
36     addAll( c);
37     }
38
39     public IterableSet()
40     {
41     super();
42     }
43
44     public boolean add( Object JavaDoc o)
45     {
46     if (o == null)
47         throw new IllegalArgumentException JavaDoc( "Cannot add \"null\" to an IterableSet.");
48
49     if (contains( o))
50         return false;
51
52     return super.add( o);
53     }
54
55     public boolean remove( Object JavaDoc o)
56     {
57     if ((o == null) || (contains( o) == false))
58         return false;
59
60     return super.remove( o);
61     }
62     
63     public boolean equals( Object JavaDoc o)
64     {
65     if (o == null)
66         return false;
67
68     if (this == o)
69         return true;
70
71     if ((o instanceof IterableSet) == false)
72         return false;
73
74     IterableSet other = (IterableSet) o;
75
76     if (size() != other.size())
77         return false;
78     
79     Iterator it = iterator();
80     while (it.hasNext())
81         if (other.contains( it.next()) == false)
82         return false;
83     
84     return true;
85     }
86     
87     public Object JavaDoc clone()
88     {
89     IterableSet s = new IterableSet();
90     s.addAll( this);
91     return s;
92     }
93
94     public boolean isSubsetOf( IterableSet other)
95     {
96     if (other == null)
97         throw new IllegalArgumentException JavaDoc( "Cannot set compare an IterableSet with \"null\".");
98
99     if (size() > other.size())
100         return false;
101
102     Iterator it = iterator();
103     while (it.hasNext())
104         if (other.contains( it.next()) == false)
105         return false;
106
107     return true;
108     }
109     
110     public boolean isSupersetOf( IterableSet other)
111     {
112     if (other == null)
113         throw new IllegalArgumentException JavaDoc( "Cannot set compare an IterableSet with \"null\".");
114
115     if (size() < other.size())
116         return false;
117
118     Iterator it = other.iterator();
119     while (it.hasNext())
120         if (contains( it.next()) == false)
121         return false;
122     
123     return true;
124     }
125
126     public boolean isStrictSubsetOf( IterableSet other)
127     {
128     if (other == null)
129         throw new IllegalArgumentException JavaDoc( "Cannot set compare an IterableSet with \"null\".");
130
131     if (size() >= other.size())
132         return false;
133
134     return isSubsetOf( other);
135     }
136     
137     public boolean isStrictSupersetOf( IterableSet other)
138     {
139     if (other == null)
140         throw new IllegalArgumentException JavaDoc( "Cannot set compare an IterableSet with \"null\".");
141
142     if (size() <= other.size())
143         return false;
144
145     return isSupersetOf( other);
146     }
147
148
149     public boolean intersects( IterableSet other)
150     {
151     if (other == null)
152         throw new IllegalArgumentException JavaDoc( "Cannot set intersect an IterableSet with \"null\".");
153
154     if (other.size() < size()) {
155         Iterator it = other.iterator();
156         while (it.hasNext())
157         if (contains( it.next()))
158             return true;
159     }
160     else {
161         Iterator it = iterator();
162         while (it.hasNext())
163         if (other.contains( it.next()))
164             return true;
165     }
166
167     return false;
168     }
169
170     public IterableSet intersection( IterableSet other)
171     {
172     if (other == null)
173         throw new IllegalArgumentException JavaDoc( "Cannot set intersect an IterableSet with \"null\".");
174
175     IterableSet c = new IterableSet();
176
177     if (other.size() < size()) {
178         Iterator it = other.iterator();
179         while (it.hasNext()) {
180         Object JavaDoc o = it.next();
181         
182         if (contains( o))
183             c.add( o);
184         }
185     }
186     else {
187         Iterator it = iterator();
188         while (it.hasNext()) {
189         Object JavaDoc o = it.next();
190         
191         if (other.contains( o))
192             c.add( o);
193         }
194     }
195     return c;
196     }
197
198     public IterableSet union( IterableSet other)
199     {
200     if (other == null)
201         throw new IllegalArgumentException JavaDoc( "Cannot set union an IterableSet with \"null\".");
202
203     IterableSet c = new IterableSet();
204
205     c.addAll( this);
206     c.addAll( other);
207
208     return c;
209     }
210
211     public String JavaDoc toString()
212     {
213     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
214
215     Iterator it = iterator();
216     while (it.hasNext()) {
217         b.append( it.next().toString());
218         b.append( "\n");
219     }
220
221     return b.toString();
222     }
223 }
224
Popular Tags