1 11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType; 16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes; 17 18 public class TypeSetIntersection extends TypeSet { 19 private TypeSet fLHS; 20 private TypeSet fRHS; 21 22 public TypeSetIntersection(TypeSet lhs, TypeSet rhs) { 23 super(lhs.getTypeSetEnvironment()); 24 fLHS= lhs; 25 fRHS= rhs; 26 } 27 28 31 public TypeSet getLHS() { 32 return fLHS; 33 } 34 35 38 public TypeSet getRHS() { 39 return fRHS; 40 } 41 42 45 public boolean isUniverse() { 46 return fLHS.isUniverse() && fRHS.isUniverse(); 47 } 48 49 52 public TypeSet makeClone() { 53 return this; } 55 56 59 public boolean isEmpty() { 60 if (fLHS.isEmpty() || fRHS.isEmpty()) 61 return true; 62 if (fLHS.isUniverse() || fRHS.isUniverse()) 63 return false; 64 if (fLHS.contains(getJavaLangObject()) && fRHS.contains(getJavaLangObject())) 66 return false; 67 68 88 return false; 89 } 90 91 94 public boolean contains(TType t) { 95 return fLHS.contains(t) && fRHS.contains(t); 96 } 97 98 101 public boolean containsAll(TypeSet s) { 102 return fLHS.containsAll(s) && fRHS.containsAll(s); 103 } 104 105 108 public TypeSet subTypes() { 109 if (isUniverse() || contains(getJavaLangObject())) 110 return getTypeSetEnvironment().getUniverseTypeSet(); 111 if ((fLHS instanceof SubTypesSet || fLHS instanceof SubTypesOfSingleton) && 113 (fRHS instanceof SubTypesSet || fRHS instanceof SubTypesOfSingleton)) 114 return this; 115 return getTypeSetEnvironment().createSubTypesSet(this); 116 } 117 118 121 public TypeSet superTypes() { 122 if ((fLHS instanceof SuperTypesSet || fLHS instanceof SuperTypesOfSingleton) && 124 (fRHS instanceof SuperTypesSet || fRHS instanceof SuperTypesOfSingleton)) 125 return this; 126 return getTypeSetEnvironment().createSuperTypesSet(this); 127 } 128 129 132 public TypeSet upperBound() { 133 if (fLHS.contains(getJavaLangObject()) && fRHS.contains(getJavaLangObject())) 134 return new SingletonTypeSet(getTypeSetEnvironment().getJavaLangObject(), getTypeSetEnvironment()); 135 136 if (fEnumCache != null) return fEnumCache.upperBound(); 137 138 EnumeratedTypeSet lhsSet= fLHS.enumerate(); 139 EnumeratedTypeSet rhsSet= fRHS.enumerate(); 140 TypeSet xsect= lhsSet.intersectedWith(rhsSet); 141 142 return xsect.upperBound(); 143 } 144 145 148 public TypeSet lowerBound() { 149 if (fLHS.hasUniqueLowerBound() && fRHS.hasUniqueLowerBound()) { 150 TType lhsBound= fLHS.uniqueLowerBound(); 151 TType rhsBound= fRHS.uniqueLowerBound(); 152 153 if (lhsBound.equals(rhsBound)) 154 return new SingletonTypeSet(lhsBound, getTypeSetEnvironment()); 155 else if (TTypes.canAssignTo(lhsBound, rhsBound)) 156 return new SingletonTypeSet(rhsBound, getTypeSetEnvironment()); 157 else if (TTypes.canAssignTo(rhsBound, lhsBound)) 158 return new SingletonTypeSet(lhsBound, getTypeSetEnvironment()); 159 } 160 if (fEnumCache != null) return fEnumCache.lowerBound(); 161 162 EnumeratedTypeSet lhsSet= fLHS.enumerate(); 163 EnumeratedTypeSet rhsSet= fRHS.enumerate(); 164 TypeSet xsect= lhsSet.intersectedWith(rhsSet); 165 166 return xsect.lowerBound(); 167 } 168 169 protected TypeSet specialCasesIntersectedWith(TypeSet s2) { 170 if (s2.equals(fLHS)) return this; 172 if (s2.equals(fRHS)) return this; 174 if (s2 instanceof TypeSetIntersection) { 175 TypeSetIntersection x2= (TypeSetIntersection) s2; 176 if (fLHS.equals(x2.fLHS)) 181 return new TypeSetIntersection(this, x2.fRHS); 182 if (fLHS.equals(x2.fRHS)) 184 return new TypeSetIntersection(this, x2.fLHS); 185 if (fRHS.equals(x2.fLHS)) 187 return new TypeSetIntersection(this, x2.fRHS); 188 if (fRHS.equals(x2.fRHS)) 190 return new TypeSetIntersection(this, x2.fLHS); 191 } 192 return null; 193 } 194 195 198 public boolean isSingleton() { 199 if (fEnumCache != null) return fEnumCache.isSingleton(); 200 201 int count= 0; 202 for(Iterator lhsIter= fLHS.iterator(); lhsIter.hasNext(); ) { 203 TType t= (TType) lhsIter.next(); 204 if (fRHS.contains(t)) 205 count++; 206 if (count > 1) 207 return false; 208 } 209 return (count == 1); 210 } 211 212 215 public TType anyMember() { 216 if (fEnumCache != null) return fEnumCache.anyMember(); 217 218 for(Iterator lhsIter= fLHS.iterator(); lhsIter.hasNext(); ) { 219 TType t= (TType) lhsIter.next(); 220 if (fRHS.contains(t)) 221 return t; 222 } 223 return null; 224 } 225 226 229 public Iterator iterator() { 230 return enumerate().iterator(); 231 232 } 263 264 267 public boolean equals(Object o) { 268 if (o instanceof TypeSetIntersection) { 269 TypeSetIntersection other= (TypeSetIntersection) o; 270 return other.fLHS.equals(fLHS) && other.fRHS.equals(fRHS); 271 } else 272 return false; 273 } 274 275 public String toString() { 276 return "<" + fID + ": intersect(" + fLHS + "," + fRHS + ")>"; } 278 279 282 public boolean hasUniqueLowerBound() { 283 return false; 284 } 285 286 289 public boolean hasUniqueUpperBound() { 290 return false; 291 } 292 293 296 public TType uniqueLowerBound() { 297 return null; 298 } 299 300 303 public TType uniqueUpperBound() { 304 return null; 305 } 306 307 private EnumeratedTypeSet fEnumCache= null; 308 309 312 public EnumeratedTypeSet enumerate() { 313 if (fEnumCache == null) { 314 EnumeratedTypeSet lhsSet= fLHS.enumerate(); 315 EnumeratedTypeSet rhsSet= fRHS.enumerate(); 316 fEnumCache= lhsSet.intersectedWith(rhsSet).enumerate(); 317 } 318 319 return fEnumCache; 320 } 321 } 322 | Popular Tags |