]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
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 | @Override | |
34 | public boolean isUniverse() { | |
35 | return false; | |
36 | } | |
37 | ||
38 | /* (non-Javadoc) | |
39 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone() | |
40 | */ | |
41 | @Override | |
42 | public TypeSet makeClone() { | |
43 | return this; //new SuperTypesOfSingleton(fLowerBound, getTypeSetEnvironment()); | |
44 | } | |
45 | ||
46 | /* (non-Javadoc) | |
47 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet) | |
48 | */ | |
49 | @Override | |
50 | protected TypeSet specialCasesIntersectedWith(TypeSet other) { | |
51 | if (other.isSingleton() && other.anyMember().equals(fLowerBound)) | |
52 | return other; // xsect(superTypes(A),A) = A | |
53 | ||
54 | if (other instanceof SuperTypesOfSingleton) { | |
55 | SuperTypesOfSingleton otherSuper= (SuperTypesOfSingleton) other; | |
56 | ||
57 | if (TTypes.canAssignTo(otherSuper.fLowerBound, fLowerBound)) | |
58 | return this; | |
59 | if (TTypes.canAssignTo(fLowerBound, otherSuper.fLowerBound)) | |
60 | return otherSuper; | |
61 | } else if (other.hasUniqueUpperBound()) { | |
62 | TType otherUpper= other.uniqueUpperBound(); | |
63 | ||
64 | if (otherUpper.equals(fLowerBound)) | |
65 | return new SingletonTypeSet(fLowerBound, getTypeSetEnvironment()); | |
66 | if ((otherUpper != fLowerBound && TTypes.canAssignTo(otherUpper, fLowerBound)) || | |
67 | ! TTypes.canAssignTo(fLowerBound, otherUpper)) | |
68 | return getTypeSetEnvironment().getEmptyTypeSet(); | |
69 | } | |
70 | // else if (other instanceof SuperTypesSet) { | |
71 | // SuperTypesSet otherSub= (SuperTypesSet) other; | |
72 | // TypeSet otherLowers= otherSub.lowerBound(); | |
73 | // | |
74 | // for(Iterator iter= otherLowers.iterator(); iter.hasNext(); ) { | |
75 | // TType t= (TType) iter.next(); | |
76 | // | |
77 | // if () | |
78 | // } | |
79 | // } | |
80 | return null; | |
81 | } | |
82 | ||
83 | /* (non-Javadoc) | |
84 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty() | |
85 | */ | |
86 | @Override | |
87 | public boolean isEmpty() { | |
88 | return false; | |
89 | } | |
90 | ||
91 | /* (non-Javadoc) | |
92 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound() | |
93 | */ | |
94 | @Override | |
95 | public TypeSet upperBound() { | |
96 | return new SingletonTypeSet(getJavaLangObject(), getTypeSetEnvironment()); | |
97 | } | |
98 | ||
99 | /* (non-Javadoc) | |
100 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound() | |
101 | */ | |
102 | @Override | |
103 | public TypeSet lowerBound() { | |
104 | return new SingletonTypeSet(fLowerBound, getTypeSetEnvironment()); | |
105 | } | |
106 | ||
107 | /* (non-Javadoc) | |
108 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound() | |
109 | */ | |
110 | @Override | |
111 | public boolean hasUniqueLowerBound() { | |
112 | return true; | |
113 | } | |
114 | ||
115 | /* (non-Javadoc) | |
116 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound() | |
117 | */ | |
118 | @Override | |
119 | public boolean hasUniqueUpperBound() { | |
120 | return true; | |
121 | } | |
122 | ||
123 | /* (non-Javadoc) | |
124 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound() | |
125 | */ | |
126 | @Override | |
127 | public TType uniqueLowerBound() { | |
128 | return fLowerBound; | |
129 | } | |
130 | ||
131 | /* (non-Javadoc) | |
132 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound() | |
133 | */ | |
134 | @Override | |
135 | public TType uniqueUpperBound() { | |
136 | return getJavaLangObject(); | |
137 | } | |
138 | ||
139 | /* (non-Javadoc) | |
140 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#superTypes() | |
141 | */ | |
142 | @Override | |
143 | public TypeSet superTypes() { | |
144 | return this; // makeClone(); | |
145 | } | |
146 | ||
147 | /* (non-Javadoc) | |
148 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType) | |
149 | */ | |
150 | @Override | |
151 | public boolean contains(TType t) { | |
152 | if (t.equals(fLowerBound)) | |
153 | return true; | |
154 | if (t.equals(getJavaLangObject())) | |
155 | return true; | |
156 | return TTypes.canAssignTo(fLowerBound, t); | |
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.TypeSet) | |
161 | */ | |
162 | @Override | |
163 | public boolean containsAll(TypeSet other) { | |
164 | // Optimization: if other is also a SubTypeOfSingleton, just compare bounds | |
165 | if (other instanceof SuperTypesOfSingleton) { | |
166 | SuperTypesOfSingleton otherSuper= (SuperTypesOfSingleton) other; | |
167 | return TTypes.canAssignTo(fLowerBound, otherSuper.fLowerBound); | |
168 | } | |
169 | // Optimization: if other is a SubTypesSet, just compare all its bounds to mine | |
170 | if (other instanceof SuperTypesSet) { | |
171 | SuperTypesSet otherSuper= (SuperTypesSet) other; | |
172 | TypeSet otherLowerBounds= otherSuper.lowerBound(); | |
173 | ||
174 | for(Iterator<TType> iter= otherLowerBounds.iterator(); iter.hasNext(); ) { | |
175 | TType t= iter.next(); | |
176 | if (! TTypes.canAssignTo(fLowerBound, t)) | |
177 | return false; | |
178 | } | |
179 | return true; | |
180 | } | |
181 | if (other.isUniverse()) { | |
182 | return false; | |
183 | } | |
184 | // For now, no more tricks up my sleeve; get an iterator | |
185 | for(Iterator<TType> iter= other.iterator(); iter.hasNext(); ) { | |
186 | TType t= iter.next(); | |
187 | ||
188 | if (! TTypes.canAssignTo(fLowerBound, t)) | |
189 | return false; | |
190 | } | |
191 | return true; | |
192 | } | |
193 | ||
194 | /* (non-Javadoc) | |
195 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator() | |
196 | */ | |
197 | @Override | |
198 | public Iterator<TType> iterator() { | |
199 | return enumerate().iterator(); | |
200 | // return new Iterator() { | |
201 | // // First type returned is fLowerBound, then each of the supertypes, in turn | |
202 | // // | |
203 | // // If the lower bound is an array type, return the set of array types | |
204 | // // { Array(superType(elementTypeOf(fUpperBound))) } | |
205 | // boolean isArray= (fLowerBound instanceof ArrayType); | |
206 | // private Set/*<TType>*/ superTypes= sTypeHierarchy.getAllSupertypes(getElementTypeOf(fLowerBound)); | |
207 | // private Iterator/*<TType>*/ superTypeIter= superTypes.iterator(); | |
208 | // private int nDims= getDimsOf(fLowerBound); | |
209 | // private int idx= (isArray ? -2 : -1); | |
210 | // public void remove() { /*do nothing*/ } | |
211 | // public boolean hasNext() { return idx < superTypes.size(); } | |
212 | // public Object next() { | |
213 | // int i=idx++; | |
214 | // if (i < -1) return sJavaLangObject; | |
215 | // if (i < 0) return fLowerBound; | |
216 | // return makePossiblyArrayTypeFor((TType) superTypeIter.next(), nDims); | |
217 | // } | |
218 | // }; | |
219 | } | |
220 | ||
221 | /* (non-Javadoc) | |
222 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton() | |
223 | */ | |
224 | @Override | |
225 | public boolean isSingleton() { | |
226 | // The only thing that doesn't have at least 1 proper supertype is java.lang.Object | |
227 | return fLowerBound.equals(getJavaLangObject()); | |
228 | } | |
229 | ||
230 | /* (non-Javadoc) | |
231 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember() | |
232 | */ | |
233 | @Override | |
234 | public TType anyMember() { | |
235 | return fLowerBound; | |
236 | } | |
237 | ||
238 | private EnumeratedTypeSet fEnumCache= null; | |
239 | ||
240 | /* (non-Javadoc) | |
241 | * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate() | |
242 | */ | |
243 | @Override | |
244 | public EnumeratedTypeSet enumerate() { | |
245 | if (fEnumCache == null) { | |
246 | if (fLowerBound instanceof ArrayType) { | |
247 | ArrayType at= (ArrayType) fLowerBound; | |
248 | fEnumCache= EnumeratedTypeSet.makeArrayTypesForElements(TTypes.getAllSuperTypesIterator(at.getComponentType()), getTypeSetEnvironment()); | |
249 | fEnumCache.add(getJavaLangObject()); | |
250 | } else | |
251 | fEnumCache= new EnumeratedTypeSet(TTypes.getAllSuperTypesIterator(fLowerBound), getTypeSetEnvironment()); | |
252 | ||
253 | fEnumCache.add(fLowerBound); | |
254 | fEnumCache.initComplete(); | |
255 | } | |
256 | return fEnumCache; | |
257 | } | |
258 | ||
259 | @Override | |
260 | public boolean equals(Object o) { | |
261 | if (!(o instanceof SuperTypesOfSingleton)) | |
262 | return false; | |
263 | SuperTypesOfSingleton other= (SuperTypesOfSingleton) o; | |
264 | ||
265 | return other.fLowerBound.equals(fLowerBound); | |
266 | } | |
267 | ||
268 | @Override | |
269 | public int hashCode() { | |
270 | return fLowerBound.hashCode(); | |
271 | } | |
272 | ||
273 | @Override | |
274 | public String toString() { | |
275 | return "<" + fID + ": superTypes(" + fLowerBound.getPrettySignature() + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
276 | } | |
277 | } |