001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2002 Sable Research Group
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.util;
027:
028: import java.util.*;
029:
030: import soot.SootClass;
031:
032: public class IterableSet extends HashChain implements Set {
033: public IterableSet(Collection<SootClass> c) {
034: super ();
035: addAll(c);
036: }
037:
038: public IterableSet() {
039: super ();
040: }
041:
042: public boolean add(Object o) {
043: if (o == null)
044: throw new IllegalArgumentException(
045: "Cannot add \"null\" to an IterableSet.");
046:
047: if (contains(o))
048: return false;
049:
050: return super .add(o);
051: }
052:
053: public boolean remove(Object o) {
054: if ((o == null) || (contains(o) == false))
055: return false;
056:
057: return super .remove(o);
058: }
059:
060: public boolean equals(Object o) {
061: if (o == null)
062: return false;
063:
064: if (this == o)
065: return true;
066:
067: if ((o instanceof IterableSet) == false)
068: return false;
069:
070: IterableSet other = (IterableSet) o;
071:
072: if (size() != other.size())
073: return false;
074:
075: Iterator it = iterator();
076: while (it.hasNext())
077: if (other.contains(it.next()) == false)
078: return false;
079:
080: return true;
081: }
082:
083: public Object clone() {
084: IterableSet s = new IterableSet();
085: s.addAll(this );
086: return s;
087: }
088:
089: public boolean isSubsetOf(IterableSet other) {
090: if (other == null)
091: throw new IllegalArgumentException(
092: "Cannot set compare an IterableSet with \"null\".");
093:
094: if (size() > other.size())
095: return false;
096:
097: Iterator it = iterator();
098: while (it.hasNext())
099: if (other.contains(it.next()) == false)
100: return false;
101:
102: return true;
103: }
104:
105: public boolean isSupersetOf(IterableSet other) {
106: if (other == null)
107: throw new IllegalArgumentException(
108: "Cannot set compare an IterableSet with \"null\".");
109:
110: if (size() < other.size())
111: return false;
112:
113: Iterator it = other.iterator();
114: while (it.hasNext())
115: if (contains(it.next()) == false)
116: return false;
117:
118: return true;
119: }
120:
121: public boolean isStrictSubsetOf(IterableSet other) {
122: if (other == null)
123: throw new IllegalArgumentException(
124: "Cannot set compare an IterableSet with \"null\".");
125:
126: if (size() >= other.size())
127: return false;
128:
129: return isSubsetOf(other);
130: }
131:
132: public boolean isStrictSupersetOf(IterableSet other) {
133: if (other == null)
134: throw new IllegalArgumentException(
135: "Cannot set compare an IterableSet with \"null\".");
136:
137: if (size() <= other.size())
138: return false;
139:
140: return isSupersetOf(other);
141: }
142:
143: public boolean intersects(IterableSet other) {
144: if (other == null)
145: throw new IllegalArgumentException(
146: "Cannot set intersect an IterableSet with \"null\".");
147:
148: if (other.size() < size()) {
149: Iterator it = other.iterator();
150: while (it.hasNext())
151: if (contains(it.next()))
152: return true;
153: } else {
154: Iterator it = iterator();
155: while (it.hasNext())
156: if (other.contains(it.next()))
157: return true;
158: }
159:
160: return false;
161: }
162:
163: public IterableSet intersection(IterableSet other) {
164: if (other == null)
165: throw new IllegalArgumentException(
166: "Cannot set intersect an IterableSet with \"null\".");
167:
168: IterableSet c = new IterableSet();
169:
170: if (other.size() < size()) {
171: Iterator it = other.iterator();
172: while (it.hasNext()) {
173: Object o = it.next();
174:
175: if (contains(o))
176: c.add(o);
177: }
178: } else {
179: Iterator it = iterator();
180: while (it.hasNext()) {
181: Object o = it.next();
182:
183: if (other.contains(o))
184: c.add(o);
185: }
186: }
187: return c;
188: }
189:
190: public IterableSet union(IterableSet other) {
191: if (other == null)
192: throw new IllegalArgumentException(
193: "Cannot set union an IterableSet with \"null\".");
194:
195: IterableSet c = new IterableSet();
196:
197: c.addAll(this );
198: c.addAll(other);
199:
200: return c;
201: }
202:
203: public String toString() {
204: StringBuffer b = new StringBuffer();
205:
206: Iterator it = iterator();
207: while (it.hasNext()) {
208: b.append(it.next().toString());
209: b.append("\n");
210: }
211:
212: return b.toString();
213: }
214: }
|