001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Robert M. Fuhrer (rfuhrer@watson.ibm.com), IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType;
015: import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
016: import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
017:
018: public class SuperTypesOfSingleton extends TypeSet {
019: /**
020: * The "base type" defining the lower bound of this set.
021: */
022: private TType fLowerBound;
023:
024: SuperTypesOfSingleton(TType t, TypeSetEnvironment typeSetEnvironment) {
025: super (typeSetEnvironment);
026: fLowerBound = t;
027: }
028:
029: /* (non-Javadoc)
030: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
031: */
032: public boolean isUniverse() {
033: return false;
034: }
035:
036: /* (non-Javadoc)
037: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
038: */
039: public TypeSet makeClone() {
040: return this ; //new SuperTypesOfSingleton(fLowerBound, getTypeSetEnvironment());
041: }
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
045: */
046: protected TypeSet specialCasesIntersectedWith(TypeSet other) {
047: if (other.isSingleton()
048: && other.anyMember().equals(fLowerBound))
049: return other; // xsect(superTypes(A),A) = A
050:
051: if (other instanceof SuperTypesOfSingleton) {
052: SuperTypesOfSingleton otherSuper = (SuperTypesOfSingleton) other;
053:
054: if (TTypes.canAssignTo(otherSuper.fLowerBound, fLowerBound))
055: return this ;
056: if (TTypes.canAssignTo(fLowerBound, otherSuper.fLowerBound))
057: return otherSuper;
058: } else if (other.hasUniqueUpperBound()) {
059: TType otherUpper = other.uniqueUpperBound();
060:
061: if (otherUpper.equals(fLowerBound))
062: return new SingletonTypeSet(fLowerBound,
063: getTypeSetEnvironment());
064: if ((otherUpper != fLowerBound && TTypes.canAssignTo(
065: otherUpper, fLowerBound))
066: || !TTypes.canAssignTo(fLowerBound, otherUpper))
067: return getTypeSetEnvironment().getEmptyTypeSet();
068: }
069: // else if (other instanceof SuperTypesSet) {
070: // SuperTypesSet otherSub= (SuperTypesSet) other;
071: // TypeSet otherLowers= otherSub.lowerBound();
072: //
073: // for(Iterator iter= otherLowers.iterator(); iter.hasNext(); ) {
074: // TType t= (TType) iter.next();
075: //
076: // if ()
077: // }
078: // }
079: return null;
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
084: */
085: public boolean isEmpty() {
086: return false;
087: }
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
091: */
092: public TypeSet upperBound() {
093: return new SingletonTypeSet(getJavaLangObject(),
094: getTypeSetEnvironment());
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
099: */
100: public TypeSet lowerBound() {
101: return new SingletonTypeSet(fLowerBound,
102: getTypeSetEnvironment());
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
107: */
108: public boolean hasUniqueLowerBound() {
109: return true;
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
114: */
115: public boolean hasUniqueUpperBound() {
116: return true;
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
121: */
122: public TType uniqueLowerBound() {
123: return fLowerBound;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
128: */
129: public TType uniqueUpperBound() {
130: return getJavaLangObject();
131: }
132:
133: /* (non-Javadoc)
134: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#superTypes()
135: */
136: public TypeSet super Types() {
137: return this ; // makeClone();
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
142: */
143: public boolean contains(TType t) {
144: if (t.equals(fLowerBound))
145: return true;
146: if (t.equals(getJavaLangObject()))
147: return true;
148: return TTypes.canAssignTo(fLowerBound, t);
149: }
150:
151: /* (non-Javadoc)
152: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
153: */
154: public boolean containsAll(TypeSet other) {
155: // Optimization: if other is also a SubTypeOfSingleton, just compare bounds
156: if (other instanceof SuperTypesOfSingleton) {
157: SuperTypesOfSingleton otherSuper = (SuperTypesOfSingleton) other;
158: return TTypes.canAssignTo(fLowerBound,
159: otherSuper.fLowerBound);
160: }
161: // Optimization: if other is a SubTypesSet, just compare all its bounds to mine
162: if (other instanceof SuperTypesSet) {
163: SuperTypesSet otherSuper = (SuperTypesSet) other;
164: TypeSet otherLowerBounds = otherSuper.lowerBound();
165:
166: for (Iterator iter = otherLowerBounds.iterator(); iter
167: .hasNext();) {
168: TType t = (TType) iter.next();
169: if (!TTypes.canAssignTo(fLowerBound, t))
170: return false;
171: }
172: return true;
173: }
174: if (other.isUniverse()) {
175: return false;
176: }
177: // For now, no more tricks up my sleeve; get an iterator
178: for (Iterator iter = other.iterator(); iter.hasNext();) {
179: TType t = (TType) iter.next();
180:
181: if (!TTypes.canAssignTo(fLowerBound, t))
182: return false;
183: }
184: return true;
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
189: */
190: public Iterator iterator() {
191: return enumerate().iterator();
192: // return new Iterator() {
193: // // First type returned is fLowerBound, then each of the supertypes, in turn
194: // //
195: // // If the lower bound is an array type, return the set of array types
196: // // { Array(superType(elementTypeOf(fUpperBound))) }
197: // boolean isArray= (fLowerBound instanceof ArrayType);
198: // private Set/*<TType>*/ superTypes= sTypeHierarchy.getAllSupertypes(getElementTypeOf(fLowerBound));
199: // private Iterator/*<TType>*/ superTypeIter= superTypes.iterator();
200: // private int nDims= getDimsOf(fLowerBound);
201: // private int idx= (isArray ? -2 : -1);
202: // public void remove() { /*do nothing*/ }
203: // public boolean hasNext() { return idx < superTypes.size(); }
204: // public Object next() {
205: // int i=idx++;
206: // if (i < -1) return sJavaLangObject;
207: // if (i < 0) return fLowerBound;
208: // return makePossiblyArrayTypeFor((TType) superTypeIter.next(), nDims);
209: // }
210: // };
211: }
212:
213: /* (non-Javadoc)
214: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
215: */
216: public boolean isSingleton() {
217: // The only thing that doesn't have at least 1 proper supertype is java.lang.Object
218: return fLowerBound.equals(getJavaLangObject());
219: }
220:
221: /* (non-Javadoc)
222: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
223: */
224: public TType anyMember() {
225: return fLowerBound;
226: }
227:
228: private EnumeratedTypeSet fEnumCache = null;
229:
230: /* (non-Javadoc)
231: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
232: */
233: public EnumeratedTypeSet enumerate() {
234: if (fEnumCache == null) {
235: if (fLowerBound instanceof ArrayType) {
236: ArrayType at = (ArrayType) fLowerBound;
237: fEnumCache = EnumeratedTypeSet
238: .makeArrayTypesForElements(TTypes
239: .getAllSuperTypesIterator(at
240: .getComponentType()),
241: getTypeSetEnvironment());
242: fEnumCache.add(getJavaLangObject());
243: } else
244: fEnumCache = new EnumeratedTypeSet(TTypes
245: .getAllSuperTypesIterator(fLowerBound),
246: getTypeSetEnvironment());
247:
248: fEnumCache.add(fLowerBound);
249: fEnumCache.initComplete();
250: }
251: return fEnumCache;
252: }
253:
254: public boolean equals(Object o) {
255: if (!(o instanceof SuperTypesOfSingleton))
256: return false;
257: SuperTypesOfSingleton other = (SuperTypesOfSingleton) o;
258:
259: return other.fLowerBound.equals(fLowerBound);
260: }
261:
262: public String toString() {
263: return "<" + fID + ": superTypes(" + fLowerBound.getPrettySignature() + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
264: }
265: }
|