KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.runtime.Assert;
16
17 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType;
18 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
19 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
20
21 public class SubTypesOfSingleton extends TypeSet {
22     /**
23      * The "base type" defining the upper bound of this set.
24      */

25     private final TType fUpperBound;
26
27     protected SubTypesOfSingleton(TType t, TypeSetEnvironment typeSetEnvironment) {
28         super(typeSetEnvironment);
29         Assert.isNotNull(t);
30         fUpperBound= t;
31     }
32
33     /* (non-Javadoc)
34      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
35      */

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

43     public TypeSet makeClone() {
44         return this; // new SubTypesOfSingleton(fUpperBound);
45
}
46
47     /* (non-Javadoc)
48      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#subTypes()
49      */

50     public TypeSet subTypes() {
51         return this; // makeClone();
52
}
53
54     /* (non-Javadoc)
55      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
56      */

57     public TypeSet specialCasesIntersectedWith(TypeSet other) {
58         if (other.isSingleton() && other.anyMember().equals(fUpperBound))
59             return other; // xsect(subTypes(A),A) = A
60

61         if (other instanceof SubTypesOfSingleton) {
62             SubTypesOfSingleton otherSub= (SubTypesOfSingleton) other;
63
64             if (TTypes.canAssignTo(otherSub.fUpperBound, fUpperBound))
65                 return otherSub; // .makeClone();
66
if (TTypes.canAssignTo(fUpperBound, otherSub.fUpperBound))
67                 return this; // makeClone();
68
} else if (other.hasUniqueLowerBound()) {
69             TType otherLower= other.uniqueLowerBound();
70
71             if (otherLower.equals(fUpperBound))
72                 return new SingletonTypeSet(fUpperBound, getTypeSetEnvironment());
73             if (otherLower != fUpperBound && TTypes.canAssignTo(fUpperBound, otherLower) ||
74                     ! TTypes.canAssignTo(otherLower, fUpperBound))
75                 return getTypeSetEnvironment().getEmptyTypeSet();
76         }
77 // else if (other instanceof SubTypesSet) {
78
// SubTypesSet otherSub= (SubTypesSet) other;
79
// TypeSet otherUppers= otherSub.upperBound();
80
//
81
// for(Iterator iter= otherUppers.iterator(); iter.hasNext(); ) {
82
// TType t= (TType) iter.next();
83
//
84
// if ()
85
// }
86
// }
87
return null;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
92      */

93     public boolean isEmpty() {
94         return false;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
99      */

100     public TypeSet upperBound() {
101         return new SingletonTypeSet(fUpperBound, getTypeSetEnvironment());
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
106      */

107     public TypeSet lowerBound() {
108         EnumeratedTypeSet e= enumerate();
109         return e.lowerBound();
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
114      */

115     public boolean hasUniqueLowerBound() {
116 // TypeSet lowerBound= lowerBound();
117

118 // return lowerBound.isSingleton();
119
return false; // fast, though perhaps inaccurate, but that's ok
120
}
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
124      */

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

132     public TType uniqueLowerBound() {
133         TypeSet lowerBound= lowerBound();
134
135         return lowerBound.anyMember();
136     }
137
138     /* (non-Javadoc)
139      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
140      */

141     public TType uniqueUpperBound() {
142         return fUpperBound;
143     }
144
145     /* (non-Javadoc)
146      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(org.eclipse.jdt.core.TType)
147      */

148     public boolean contains(TType t) {
149         if (t.equals(fUpperBound))
150             return true;
151         return TTypes.canAssignTo(t, fUpperBound);
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
156      */

157     public boolean containsAll(TypeSet other) {
158         if (isUniverse())
159             return true;
160
161         // Optimization: if other is also a SubTypeOfSingleton, just compare bounds
162
if (other instanceof SubTypesOfSingleton) {
163             SubTypesOfSingleton otherSub= (SubTypesOfSingleton) other;
164             return TTypes.canAssignTo(otherSub.fUpperBound, fUpperBound);
165         }
166         // Optimization: if other is a SubTypesSet, just compare each of its bounds to mine
167
if (other instanceof SubTypesSet) {
168             SubTypesSet otherSub= (SubTypesSet) other;
169             TypeSet otherUpperBounds= otherSub.upperBound();
170
171             for(Iterator JavaDoc iter= otherUpperBounds.iterator(); iter.hasNext(); ) {
172                 TType t= (TType) iter.next();
173                 if (! TTypes.canAssignTo(t, fUpperBound))
174                     return false;
175             }
176             return true;
177         }
178         // For now, no more tricks up my sleeve; get an iterator
179
for(Iterator JavaDoc iter= other.iterator(); iter.hasNext(); ) {
180             TType t= (TType) iter.next();
181
182             if (! TTypes.canAssignTo(t, fUpperBound))
183                 return false;
184         }
185         return true;
186     }
187
188     /* (non-Javadoc)
189      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
190      */

191     public Iterator JavaDoc iterator() {
192         return enumerate().iterator();
193 // return new Iterator() {
194
// // First type returned is fUpperBound, then each of the subtypes, in turn
195
// //
196
// // If the upper bound is an array type, return the set of array types
197
// // { Array(subType(elementTypeOf(fUpperBound))) }
198
// private Set/*<TType>*/ subTypes= sTypeHierarchy.getAllSubtypes(getElementTypeOf(fUpperBound));
199
// private Iterator/*<TType>*/ subTypeIter= subTypes.iterator();
200
// private int nDims= getDimsOf(fUpperBound);
201
// private int idx=-1;
202
// public void remove() { /*do nothing*/}
203
// public boolean hasNext() { return idx < subTypes.size(); }
204
// public Object next() {
205
// int i=idx++;
206
// if (i < 0) return fUpperBound;
207
// return makePossiblyArrayTypeFor((TType) subTypeIter.next(), nDims);
208
// }
209
// };
210
}
211
212     /**
213      * Returns the element type of the given TType, if an array type, or the
214      * given TType itself, otherwise.
215      */

216     private TType getElementTypeOf(TType t) {
217         if (t instanceof ArrayType)
218             return ((ArrayType) t).getElementType();
219         return t;
220     }
221
222     /* (non-Javadoc)
223      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
224      */

225     public boolean isSingleton() {
226         return getElementTypeOf(fUpperBound).getSubTypes().length == 0;
227     }
228
229     /* (non-Javadoc)
230      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
231      */

232     public TType anyMember() {
233         return fUpperBound;
234     }
235
236     private EnumeratedTypeSet fEnumCache= null;
237
238     /* (non-Javadoc)
239      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
240      */

241     public EnumeratedTypeSet enumerate() {
242         if (fEnumCache == null) {
243             if (fUpperBound instanceof ArrayType) {
244                 ArrayType at= (ArrayType) fUpperBound;
245                 fEnumCache= EnumeratedTypeSet.makeArrayTypesForElements(TTypes.getAllSubTypesIterator(at.getComponentType()),getTypeSetEnvironment());
246             } else
247                 fEnumCache= new EnumeratedTypeSet(TTypes.getAllSubTypesIterator(fUpperBound), getTypeSetEnvironment());
248
249             fEnumCache.add(fUpperBound);
250             fEnumCache.initComplete();
251         }
252         return fEnumCache;
253     }
254
255     public boolean equals(Object JavaDoc o) {
256         if (!(o instanceof SubTypesOfSingleton))
257             return false;
258         SubTypesOfSingleton other= (SubTypesOfSingleton) o;
259
260         return other.fUpperBound.equals(fUpperBound);
261     }
262
263     public String JavaDoc toString() {
264         return "<" + fID + ": subTypes(" + fUpperBound.getPrettySignature() + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
265
}
266 }
267
Popular Tags