KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints > typesets > SuperTypesSet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Robert M. Fuhrer (rfuhrer@watson.ibm.com), IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType;
16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
17 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
18
19 public class SuperTypesSet extends TypeSet {
20     private TypeSet fLowerBounds;
21
22     SuperTypesSet(TType subType, TypeSetEnvironment typeSetEnvironment) {
23         super(typeSetEnvironment);
24         fLowerBounds= new SingletonTypeSet(subType, typeSetEnvironment);
25     }
26
27     SuperTypesSet(TypeSet subTypes, TypeSetEnvironment typeSetEnvironment) {
28         super(typeSetEnvironment);
29         fLowerBounds= subTypes;
30     }
31
32     /* (non-Javadoc)
33      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
34      */

35     public boolean isUniverse() {
36         return fLowerBounds.isUniverse();
37     }
38
39     /* (non-Javadoc)
40      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
41      */

42     public TypeSet makeClone() {
43         return this; //new SuperTypesSet(fLowerBounds.makeClone(), getTypeSetEnvironment());
44
}
45
46     /* (non-Javadoc)
47      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
48      */

49     public TypeSet upperBound() {
50         return new SingletonTypeSet(getTypeSetEnvironment().getJavaLangObject(), getTypeSetEnvironment());
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
55      */

56     public TypeSet lowerBound() {
57         // Ask the operand for its lower-bound, in case it's something like an
58
// EnumeratedTypeSet, which may have things in it other than the lower
59
// bound...
60
return fLowerBounds.lowerBound();
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet)
65      */

66     protected TypeSet specialCasesIntersectedWith(TypeSet s2) {
67         if (fLowerBounds.equals(s2))
68             return s2; // xsect(superTypes(A),A) = A
69
if (s2 instanceof SuperTypesSet) {
70             SuperTypesSet st2= (SuperTypesSet) s2;
71
72             if (fLowerBounds.isSingleton() && st2.fLowerBounds.isSingleton()) {
73                 TType t1= this.fLowerBounds.anyMember();
74                 TType t2= st2.fLowerBounds.anyMember();
75
76                 if (TTypes.canAssignTo(t1, t2))
77                     return new SuperTypesSet(st2.fLowerBounds, getTypeSetEnvironment());
78             } else if (fLowerBounds instanceof SubTypesSet) {
79                 // xsect(superTypes(subTypes(A)), superTypes(A)) = superTypes(A)
80
SubTypesSet myLowerSubTypes= (SubTypesSet) fLowerBounds;
81
82                 if (myLowerSubTypes.upperBound().equals(st2.upperBound()))
83                     return st2;
84             }
85         }
86         if (s2 instanceof SuperTypesOfSingleton) {
87             SuperTypesOfSingleton st2= (SuperTypesOfSingleton) s2;
88
89             if (fLowerBounds.isSingleton()) {
90                 TType t1= this.fLowerBounds.anyMember();
91                 TType t2= st2.uniqueLowerBound();
92
93                 if (TTypes.canAssignTo(t1, t2))
94                     return getTypeSetEnvironment().createSuperTypesOfSingleton(t2);
95             } else if (fLowerBounds instanceof SubTypesOfSingleton) {
96                 // xsect(superTypes(subTypes(A)), superTypes(A)) = superTypes(A)
97
SubTypesOfSingleton myLowerSubTypes= (SubTypesOfSingleton) fLowerBounds;
98
99                 if (myLowerSubTypes.uniqueUpperBound().equals(st2.uniqueUpperBound()))
100                     return st2;
101             }
102         }
103         if (s2 instanceof SubTypesSet) {
104             SubTypesSet st2= (SubTypesSet) s2;
105             if (fLowerBounds.equals(st2.upperBound()))
106                 return fLowerBounds;
107
108             if (fLowerBounds instanceof TypeSetIntersection) {
109                 // (intersect (superTypes (intersect (subTypes A) B))
110
// (subTypes A)) =>
111
// (intersect (subTypes A) (superTypes B))
112
TypeSetIntersection lbXSect= (TypeSetIntersection) fLowerBounds;
113                 TypeSet xsectLeft= lbXSect.getLHS();
114                 TypeSet xsectRight= lbXSect.getRHS();
115
116                 if (xsectLeft.equals(st2.upperBound()))
117                     return new TypeSetIntersection(s2, new SuperTypesSet(xsectRight, getTypeSetEnvironment()));
118             }
119         }
120         return null;
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#superTypes()
125      */

126     public TypeSet superTypes() {
127         return this; // makeClone();
128
}
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
132      */

133     public boolean isEmpty() {
134         return fLowerBounds.isEmpty();
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
139      */

140     public boolean contains(TType t) {
141         if (fEnumCache != null) return fEnumCache.contains(t);
142
143         if (t.equals(getJavaLangObject()))
144             return true;
145         if (fLowerBounds.contains(t))
146             return true;
147
148         // Find the "lower frontier", i.e. the lower bound, and see whether
149
// the given type is a supertype of any of those.
150
for(Iterator JavaDoc lbIter= fLowerBounds /*.lowerBound() */.iterator(); lbIter.hasNext(); ) {
151             TType lb= (TType) lbIter.next();
152
153             if (TTypes.canAssignTo(lb, t))
154                 return true;
155         }
156         return false;
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet)
161      */

162     public boolean containsAll(TypeSet s) {
163         if (fEnumCache != null) return fEnumCache.containsAll(s);
164
165         if (!isUniverse() && s.isUniverse()) // this is more general than just SuperTypesSet; probably belongs in TypeSet
166
return false;
167         if (equals(s))
168             return true;
169         if (fLowerBounds.containsAll(s))
170             return true;
171
172         // Make sure all elements of s are contained in this set
173
for(Iterator JavaDoc sIter= s.iterator(); sIter.hasNext(); ) {
174             TType t= (TType) sIter.next();
175             boolean found= false;
176
177             // Scan the "lower frontier", i.e. the lower bound set, and see whether
178
// 't' is a supertype of any of those.
179
for(Iterator JavaDoc lbIter= fLowerBounds /*.lowerBound()*/.iterator(); lbIter.hasNext(); ) {
180                 TType lb= (TType) lbIter.next();
181
182                 if (TTypes.canAssignTo(lb, t)) {
183                     found= true;
184                     break;
185                 }
186             }
187             if (!found) return false;
188         }
189         return true;
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
194      */

195     public boolean isSingleton() {
196         if (fEnumCache != null) return fEnumCache.isSingleton();
197
198         return fLowerBounds.isSingleton() && (fLowerBounds.anyMember() == getJavaLangObject());
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
203      */

204     public TType anyMember() {
205         return fLowerBounds.anyMember();
206     }
207
208     /* (non-Javadoc)
209      * @see java.lang.Object#equals(java.lang.Object)
210      */

211     public boolean equals(Object JavaDoc o) {
212         if (o instanceof SuperTypesSet) {
213             SuperTypesSet other= (SuperTypesSet) o;
214             return other.fLowerBounds.equals(fLowerBounds);
215 // } else if (o instanceof TypeSet) {
216
// TypeSet other= (TypeSet) o;
217
// if (other.isUniverse() && isUniverse())
218
// return true;
219
// return enumerate().equals(other.enumerate());
220
} else
221             return false;
222     }
223
224     /* (non-Javadoc)
225      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
226      */

227     public Iterator JavaDoc iterator() {
228         return enumerate().iterator();
229     }
230
231     public String JavaDoc toString() {
232         return "<" + fID + ": superTypes(" + fLowerBounds + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
233
}
234
235     /* (non-Javadoc)
236      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
237      */

238     public boolean hasUniqueLowerBound() {
239         return fLowerBounds.isSingleton();
240     }
241
242     /* (non-Javadoc)
243      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
244      */

245     public boolean hasUniqueUpperBound() {
246         return false;
247     }
248
249     /* (non-Javadoc)
250      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
251      */

252     public TType uniqueLowerBound() {
253         return fLowerBounds.isSingleton() ? fLowerBounds.anyMember() : null;
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
258      */

259     public TType uniqueUpperBound() {
260         return null;
261     }
262
263     private EnumeratedTypeSet fEnumCache= null;
264
265     /* (non-Javadoc)
266      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
267      */

268     public EnumeratedTypeSet enumerate() {
269         if (fEnumCache == null) {
270             fEnumCache= new EnumeratedTypeSet(getTypeSetEnvironment());
271             boolean anyLBIsIntfOrArray= false;
272
273             for(Iterator JavaDoc iter= fLowerBounds.iterator(); iter.hasNext(); ) {
274                 TType lb= (TType) iter.next();
275
276                 if (lb instanceof ArrayType) {
277                     ArrayType at= (ArrayType) lb;
278                     int numDims= at.getDimensions();
279                     for(Iterator JavaDoc elemSuperIter=TTypes.getAllSuperTypesIterator(at.getElementType()); elemSuperIter.hasNext(); )
280                         fEnumCache.add(TTypes.createArrayType((TType) elemSuperIter.next(), numDims));
281                     anyLBIsIntfOrArray= true;
282                 } else {
283                     for (Iterator JavaDoc iterator= TTypes.getAllSuperTypesIterator(lb); iterator.hasNext(); )
284                     fEnumCache.fMembers.add(iterator.next());
285                 }
286                 fEnumCache.add(lb);
287             }
288             if (anyLBIsIntfOrArray) fEnumCache.add(getJavaLangObject());
289             //fEnumCache.initComplete();
290
}
291         return fEnumCache;
292     }
293 }
294
Popular Tags