1 19 20 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 o) 45 { 46 if (o == null) 47 throw new IllegalArgumentException ( "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 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 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 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 ( "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 ( "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 ( "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 ( "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 ( "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 ( "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 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 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 ( "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 toString() 212 { 213 StringBuffer b = new StringBuffer (); 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 |