KickJava   Java API By Example, From Geeks To Geeks.

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


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 SubTypesSet extends TypeSet {
20     /**
21      * The set of "base types" defining the upper bounds of this set.
22      */

23     private TypeSet fUpperBounds;
24
25     SubTypesSet(TypeSet superTypes) {
26         super(superTypes.getTypeSetEnvironment());
27         fUpperBounds= superTypes;
28     }
29
30     /* (non-Javadoc)
31      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
32      */

33     public boolean isUniverse() {
34         return fUpperBounds.isUniverse() || fUpperBounds.contains(getJavaLangObject());
35     }
36
37     /* (non-Javadoc)
38      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
39      */

40     public TypeSet makeClone() {
41         return this; //new SubTypesSet(fUpperBounds.makeClone());
42
}
43
44     /* (non-Javadoc)
45      * @see java.lang.Object#equals(java.lang.Object)
46      */

47     public boolean equals(Object JavaDoc o) {
48         if (o instanceof SubTypesSet) {
49             SubTypesSet other= (SubTypesSet) o;
50             return other.fUpperBounds.equals(fUpperBounds);
51 // } else if (o instanceof TypeSet) {
52
// TypeSet other= (TypeSet) o;
53
// if (other.isUniverse() && isUniverse())
54
// return true;
55
// return enumerate().equals(other.enumerate());
56
} else
57             return false;
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet)
62      */

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

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

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

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

161     public boolean containsAll(TypeSet s) {
162         if (fEnumCache != null) return fEnumCache.containsAll(s);
163
164         if (fUpperBounds.containsAll(s))
165             return true;
166
167         // Make sure all elements of s are contained in this set
168
for(Iterator JavaDoc sIter= s.iterator(); sIter.hasNext(); ) {
169             TType t= (TType) sIter.next();
170             boolean found= false;
171
172             // Scan the "upper frontier", i.e. the upper bound set, and see whether
173
// 't' is a subtype of any of those.
174
for(Iterator JavaDoc ubIter= fUpperBounds /*.upperBound() */.iterator(); ubIter.hasNext(); ) {
175                 TType ub= (TType) ubIter.next();
176
177                 if (TTypes.canAssignTo(t, ub)) {
178                     found= true;
179                     break;
180                 }
181             }
182             if (!found) return false;
183         }
184         return true;
185     }
186
187     /**
188      * Returns the element type of the given TType, if an array type, or the
189      * given TType itself, otherwise.
190      */

191     private TType getElementTypeOf(TType t) {
192         if (t instanceof ArrayType)
193             return ((ArrayType) t).getElementType();
194         return t;
195     }
196
197     /* (non-Javadoc)
198      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
199      */

200     public boolean isSingleton() {
201         if (!fUpperBounds.isSingleton())
202             return false;
203
204         TType t= fUpperBounds.anyMember();
205
206         return getElementTypeOf(t).getSubTypes().length == 0;
207     }
208
209     /* (non-Javadoc)
210      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
211      */

212     public TType anyMember() {
213         return fUpperBounds.anyMember();
214     }
215
216     /* (non-Javadoc)
217      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
218      */

219     public TypeSet upperBound() {
220         return fUpperBounds; // perhaps should be unmodifiable?
221
}
222
223     /* (non-Javadoc)
224      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
225      */

226     public TypeSet lowerBound() {
227         return enumerate().lowerBound();
228     }
229
230     /* (non-Javadoc)
231      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
232      */

233     public Iterator JavaDoc iterator() {
234         return enumerate().iterator();
235     }
236
237     /* (non-Javadoc)
238      * @see java.lang.Object#toString()
239      */

240     public String JavaDoc toString() {
241         return "<" + fID + ": subTypes(" + fUpperBounds + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
242
}
243
244     /* (non-Javadoc)
245      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
246      */

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

254     public boolean hasUniqueUpperBound() {
255         return fUpperBounds.isSingleton();
256     }
257
258     /* (non-Javadoc)
259      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
260      */

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

268     public TType uniqueUpperBound() {
269         return fUpperBounds.isSingleton() ? fUpperBounds.anyMember() : null;
270     }
271
272     private EnumeratedTypeSet fEnumCache= null;
273
274     /* (non-Javadoc)
275      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
276      */

277     public EnumeratedTypeSet enumerate() {
278         if (fEnumCache == null) {
279             fEnumCache= new EnumeratedTypeSet(getTypeSetEnvironment());
280
281             for(Iterator JavaDoc iter= fUpperBounds.iterator(); iter.hasNext(); ) {
282                 TType ub= (TType) iter.next();
283
284                 if (ub instanceof ArrayType) {
285                     ArrayType at= (ArrayType) ub;
286                     int numDims= at.getDimensions();
287                     for(Iterator JavaDoc elemSubIter=TTypes.getAllSubTypesIterator(at.getElementType()); elemSubIter.hasNext(); )
288                         fEnumCache.add(TTypes.createArrayType((TType) elemSubIter.next(), numDims));
289                 } else {
290                     for (Iterator JavaDoc iterator= TTypes.getAllSubTypesIterator(ub); iterator.hasNext();) {
291                         fEnumCache.fMembers.add(iterator.next());
292                     }
293                 }
294                 fEnumCache.add(ub);
295             }
296 // fEnumCache.initComplete();
297
}
298         return fEnumCache;
299     }
300 }
301
Popular Tags