KickJava   Java API By Example, From Geeks To Geeks.

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


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 SuperTypesOfSingleton extends TypeSet {
20     /**
21      * The "base type" defining the lower bound of this set.
22      */

23     private TType fLowerBound;
24
25     SuperTypesOfSingleton(TType t, TypeSetEnvironment typeSetEnvironment) {
26         super(typeSetEnvironment);
27         fLowerBound= t;
28     }
29
30     /* (non-Javadoc)
31      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
32      */

33     public boolean isUniverse() {
34         return false;
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 SuperTypesOfSingleton(fLowerBound, getTypeSetEnvironment());
42
}
43
44     /* (non-Javadoc)
45      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
46      */

47     protected TypeSet specialCasesIntersectedWith(TypeSet other) {
48         if (other.isSingleton() && other.anyMember().equals(fLowerBound))
49             return other; // xsect(superTypes(A),A) = A
50

51         if (other instanceof SuperTypesOfSingleton) {
52             SuperTypesOfSingleton otherSuper= (SuperTypesOfSingleton) other;
53
54             if (TTypes.canAssignTo(otherSuper.fLowerBound, fLowerBound))
55                 return this;
56             if (TTypes.canAssignTo(fLowerBound, otherSuper.fLowerBound))
57                 return otherSuper;
58         } else if (other.hasUniqueUpperBound()) {
59             TType otherUpper= other.uniqueUpperBound();
60
61             if (otherUpper.equals(fLowerBound))
62                 return new SingletonTypeSet(fLowerBound, getTypeSetEnvironment());
63             if ((otherUpper != fLowerBound && TTypes.canAssignTo(otherUpper, fLowerBound)) ||
64                     ! TTypes.canAssignTo(fLowerBound, otherUpper))
65                 return getTypeSetEnvironment().getEmptyTypeSet();
66         }
67 // else if (other instanceof SuperTypesSet) {
68
// SuperTypesSet otherSub= (SuperTypesSet) other;
69
// TypeSet otherLowers= otherSub.lowerBound();
70
//
71
// for(Iterator iter= otherLowers.iterator(); iter.hasNext(); ) {
72
// TType t= (TType) iter.next();
73
//
74
// if ()
75
// }
76
// }
77
return null;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
82      */

83     public boolean isEmpty() {
84         return false;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
89      */

90     public TypeSet upperBound() {
91         return new SingletonTypeSet(getJavaLangObject(), getTypeSetEnvironment());
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
96      */

97     public TypeSet lowerBound() {
98         return new SingletonTypeSet(fLowerBound, getTypeSetEnvironment());
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
103      */

104     public boolean hasUniqueLowerBound() {
105         return true;
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
110      */

111     public boolean hasUniqueUpperBound() {
112         return true;
113     }
114
115     /* (non-Javadoc)
116      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
117      */

118     public TType uniqueLowerBound() {
119         return fLowerBound;
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
124      */

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

132     public TypeSet superTypes() {
133         return this; // makeClone();
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 (t.equals(fLowerBound))
141             return true;
142         if (t.equals(getJavaLangObject()))
143             return true;
144         return TTypes.canAssignTo(fLowerBound, t);
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
149      */

150     public boolean containsAll(TypeSet other) {
151         // Optimization: if other is also a SubTypeOfSingleton, just compare bounds
152
if (other instanceof SuperTypesOfSingleton) {
153             SuperTypesOfSingleton otherSuper= (SuperTypesOfSingleton) other;
154             return TTypes.canAssignTo(fLowerBound, otherSuper.fLowerBound);
155         }
156         // Optimization: if other is a SubTypesSet, just compare all its bounds to mine
157
if (other instanceof SuperTypesSet) {
158             SuperTypesSet otherSuper= (SuperTypesSet) other;
159             TypeSet otherLowerBounds= otherSuper.lowerBound();
160
161             for(Iterator JavaDoc iter= otherLowerBounds.iterator(); iter.hasNext(); ) {
162                 TType t= (TType) iter.next();
163                 if (! TTypes.canAssignTo(fLowerBound, t))
164                     return false;
165             }
166             return true;
167         }
168         if (other.isUniverse()) {
169             return false;
170         }
171         // For now, no more tricks up my sleeve; get an iterator
172
for(Iterator JavaDoc iter= other.iterator(); iter.hasNext(); ) {
173             TType t= (TType) iter.next();
174
175             if (! TTypes.canAssignTo(fLowerBound, t))
176                 return false;
177         }
178         return true;
179     }
180
181     /* (non-Javadoc)
182      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
183      */

184     public Iterator JavaDoc iterator() {
185         return enumerate().iterator();
186 // return new Iterator() {
187
// // First type returned is fLowerBound, then each of the supertypes, in turn
188
// //
189
// // If the lower bound is an array type, return the set of array types
190
// // { Array(superType(elementTypeOf(fUpperBound))) }
191
// boolean isArray= (fLowerBound instanceof ArrayType);
192
// private Set/*<TType>*/ superTypes= sTypeHierarchy.getAllSupertypes(getElementTypeOf(fLowerBound));
193
// private Iterator/*<TType>*/ superTypeIter= superTypes.iterator();
194
// private int nDims= getDimsOf(fLowerBound);
195
// private int idx= (isArray ? -2 : -1);
196
// public void remove() { /*do nothing*/ }
197
// public boolean hasNext() { return idx < superTypes.size(); }
198
// public Object next() {
199
// int i=idx++;
200
// if (i < -1) return sJavaLangObject;
201
// if (i < 0) return fLowerBound;
202
// return makePossiblyArrayTypeFor((TType) superTypeIter.next(), nDims);
203
// }
204
// };
205
}
206
207     /* (non-Javadoc)
208      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
209      */

210     public boolean isSingleton() {
211         // The only thing that doesn't have at least 1 proper supertype is java.lang.Object
212
return fLowerBound.equals(getJavaLangObject());
213     }
214
215     /* (non-Javadoc)
216      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
217      */

218     public TType anyMember() {
219         return fLowerBound;
220     }
221
222     private EnumeratedTypeSet fEnumCache= null;
223
224     /* (non-Javadoc)
225      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
226      */

227     public EnumeratedTypeSet enumerate() {
228         if (fEnumCache == null) {
229             if (fLowerBound instanceof ArrayType) {
230                 ArrayType at= (ArrayType) fLowerBound;
231                 fEnumCache= EnumeratedTypeSet.makeArrayTypesForElements(TTypes.getAllSuperTypesIterator(at.getComponentType()), getTypeSetEnvironment());
232                 fEnumCache.add(getJavaLangObject());
233             } else
234                 fEnumCache= new EnumeratedTypeSet(TTypes.getAllSuperTypesIterator(fLowerBound), getTypeSetEnvironment());
235
236             fEnumCache.add(fLowerBound);
237             fEnumCache.initComplete();
238         }
239         return fEnumCache;
240     }
241
242     public boolean equals(Object JavaDoc o) {
243         if (!(o instanceof SuperTypesOfSingleton))
244             return false;
245         SuperTypesOfSingleton other= (SuperTypesOfSingleton) o;
246
247         return other.fLowerBound.equals(fLowerBound);
248     }
249
250     public String JavaDoc toString() {
251         return "<" + fID + ": superTypes(" + fLowerBound.getPrettySignature() + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
252
}
253 }
254
Popular Tags