]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/typeconstraints/typesets/SubTypesOfSingleton.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / typeconstraints / typesets / SubTypesOfSingleton.java
CommitLineData
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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets;
12
13import java.util.Iterator;
14
15import org.eclipse.core.runtime.Assert;
16
17import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType;
18import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
19import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
20
21public class SubTypesOfSingleton extends TypeSet {
22 /**
23 * The "base type" defining the upper bound of this set.
24 */
25 public 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 @Override
37 public boolean isUniverse() {
38 return fUpperBound.equals(getJavaLangObject());
39 }
40
41 /* (non-Javadoc)
42 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
43 */
44 @Override
45 public TypeSet makeClone() {
46 return this; // new SubTypesOfSingleton(fUpperBound);
47 }
48
49 /* (non-Javadoc)
50 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#subTypes()
51 */
52 @Override
53 public TypeSet subTypes() {
54 return this; // makeClone();
55 }
56
57 /* (non-Javadoc)
58 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
59 */
60 @Override
61 public TypeSet specialCasesIntersectedWith(TypeSet other) {
62 if (other.isSingleton() && other.anyMember().equals(fUpperBound))
63 return other; // xsect(subTypes(A),A) = A
64
65 if (other instanceof SubTypesOfSingleton) {
66 SubTypesOfSingleton otherSub= (SubTypesOfSingleton) other;
67
68 if (TTypes.canAssignTo(otherSub.fUpperBound, fUpperBound))
69 return otherSub; // .makeClone();
70 if (TTypes.canAssignTo(fUpperBound, otherSub.fUpperBound))
71 return this; // makeClone();
72 } else if (other.hasUniqueLowerBound()) {
73 TType otherLower= other.uniqueLowerBound();
74
75 if (otherLower.equals(fUpperBound))
76 return new SingletonTypeSet(fUpperBound, getTypeSetEnvironment());
77 if (otherLower != fUpperBound && TTypes.canAssignTo(fUpperBound, otherLower) ||
78 ! TTypes.canAssignTo(otherLower, fUpperBound))
79 return getTypeSetEnvironment().getEmptyTypeSet();
80 }
81// else if (other instanceof SubTypesSet) {
82// SubTypesSet otherSub= (SubTypesSet) other;
83// TypeSet otherUppers= otherSub.upperBound();
84//
85// for(Iterator iter= otherUppers.iterator(); iter.hasNext(); ) {
86// TType t= (TType) iter.next();
87//
88// if ()
89// }
90// }
91 return null;
92 }
93
94 /* (non-Javadoc)
95 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
96 */
97 @Override
98 public boolean isEmpty() {
99 return false;
100 }
101
102 /* (non-Javadoc)
103 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
104 */
105 @Override
106 public TypeSet upperBound() {
107 return new SingletonTypeSet(fUpperBound, getTypeSetEnvironment());
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
112 */
113 @Override
114 public TypeSet lowerBound() {
115 EnumeratedTypeSet e= enumerate();
116 return e.lowerBound();
117 }
118
119 /* (non-Javadoc)
120 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
121 */
122 @Override
123 public boolean hasUniqueLowerBound() {
124// TypeSet lowerBound= lowerBound();
125
126// return lowerBound.isSingleton();
127 return false; // fast, though perhaps inaccurate, but that's ok
128 }
129
130 /* (non-Javadoc)
131 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
132 */
133 @Override
134 public boolean hasUniqueUpperBound() {
135 return true;
136 }
137
138 /* (non-Javadoc)
139 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
140 */
141 @Override
142 public TType uniqueLowerBound() {
143 TypeSet lowerBound= lowerBound();
144
145 return lowerBound.anyMember();
146 }
147
148 /* (non-Javadoc)
149 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
150 */
151 @Override
152 public TType uniqueUpperBound() {
153 return fUpperBound;
154 }
155
156 /* (non-Javadoc)
157 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(org.eclipse.jdt.core.TType)
158 */
159 @Override
160 public boolean contains(TType t) {
161 return fUpperBound.generated_5001381593117913951(t);
162 }
163
164 /* (non-Javadoc)
165 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
166 */
167 @Override
168 public boolean containsAll(TypeSet other) {
169 if (isUniverse())
170 return true;
171
172 // Optimization: if other is also a SubTypeOfSingleton, just compare bounds
173 if (other instanceof SubTypesOfSingleton) {
174 SubTypesOfSingleton otherSub= (SubTypesOfSingleton) other;
175 return otherSub.generated_3961511123341647214(this);
176 }
177 // Optimization: if other is a SubTypesSet, just compare each of its bounds to mine
178 if (other instanceof SubTypesSet) {
179 SubTypesSet otherSub= (SubTypesSet) other;
180 TypeSet otherUpperBounds= otherSub.upperBound();
181
182 for(Iterator<TType> iter= otherUpperBounds.iterator(); iter.hasNext(); ) {
183 TType t= iter.next();
184 if (! TTypes.canAssignTo(t, fUpperBound))
185 return false;
186 }
187 return true;
188 }
189 // For now, no more tricks up my sleeve; get an iterator
190 for(Iterator<TType> iter= other.iterator(); iter.hasNext(); ) {
191 TType t= iter.next();
192
193 if (! TTypes.canAssignTo(t, fUpperBound))
194 return false;
195 }
196 return true;
197 }
198
199 /* (non-Javadoc)
200 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
201 */
202 @Override
203 public Iterator<TType> iterator() {
204 return enumerate().iterator();
205// return new Iterator() {
206// // First type returned is fUpperBound, then each of the subtypes, in turn
207// //
208// // If the upper bound is an array type, return the set of array types
209// // { Array(subType(elementTypeOf(fUpperBound))) }
210// private Set/*<TType>*/ subTypes= sTypeHierarchy.getAllSubtypes(getElementTypeOf(fUpperBound));
211// private Iterator/*<TType>*/ subTypeIter= subTypes.iterator();
212// private int nDims= getDimsOf(fUpperBound);
213// private int idx=-1;
214// public void remove() { /*do nothing*/}
215// public boolean hasNext() { return idx < subTypes.size(); }
216// public Object next() {
217// int i=idx++;
218// if (i < 0) return fUpperBound;
219// return makePossiblyArrayTypeFor((TType) subTypeIter.next(), nDims);
220// }
221// };
222 }
223
224 /**
225 * Returns the element type of the given TType, if an array type, or the
226 * given TType itself, otherwise.
227 *
228 * @param t a type
229 * @return the element type
230 */
231 private TType getElementTypeOf(TType t) {
232 return t.generated_6755876168370827577();
233 }
234
235 /* (non-Javadoc)
236 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
237 */
238 @Override
239 public boolean isSingleton() {
240 return getElementTypeOf(fUpperBound).getSubTypes().length == 0;
241 }
242
243 /* (non-Javadoc)
244 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
245 */
246 @Override
247 public TType anyMember() {
248 return fUpperBound;
249 }
250
251 EnumeratedTypeSet fEnumCache= null;
252
253 /* (non-Javadoc)
254 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
255 */
256 @Override
257 public EnumeratedTypeSet enumerate() {
258 if (fEnumCache == null) {
259 if (fUpperBound instanceof ArrayType) {
260 ArrayType at= (ArrayType) fUpperBound;
261 fEnumCache= EnumeratedTypeSet.makeArrayTypesForElements(TTypes.getAllSubTypesIterator(at.getComponentType()),getTypeSetEnvironment());
262 } else
263 fEnumCache= new EnumeratedTypeSet(TTypes.getAllSubTypesIterator(fUpperBound), getTypeSetEnvironment());
264
265 fEnumCache.generated_2584938505133572049(this);
266 }
267 return fEnumCache;
268 }
269
270 @Override
271 public boolean equals(Object o) {
272 if (!(o instanceof SubTypesOfSingleton))
273 return false;
274 SubTypesOfSingleton other= (SubTypesOfSingleton) o;
275
276 return other.generated_1183322834701594823(this);
277 }
278
279 @Override
280 public int hashCode() {
281 return fUpperBound.hashCode();
282 }
283
284 @Override
285 public String toString() {
286 return "<" + fID + ": subTypes(" + fUpperBound.getPrettySignature() + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
287 }
288
289 public boolean generated_3961511123341647214(SubTypesOfSingleton subtypesofsingleton) {
290 return TTypes.canAssignTo(fUpperBound, subtypesofsingleton.fUpperBound);
291 }
292
293 public boolean generated_1183322834701594823(SubTypesOfSingleton subtypesofsingleton) {
294 return fUpperBound.equals(subtypesofsingleton.fUpperBound);
295 }
296}