KickJava   Java API By Example, From Geeks To Geeks.

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


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.TType;
16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
17
18 public class TypeSetIntersection extends TypeSet {
19     private TypeSet fLHS;
20     private TypeSet fRHS;
21
22     public TypeSetIntersection(TypeSet lhs, TypeSet rhs) {
23         super(lhs.getTypeSetEnvironment());
24         fLHS= lhs;
25         fRHS= rhs;
26     }
27
28     /**
29      * @return Returns the LHS.
30      */

31     public TypeSet getLHS() {
32         return fLHS;
33     }
34
35     /**
36      * @return Returns the RHS.
37      */

38     public TypeSet getRHS() {
39         return fRHS;
40     }
41
42     /* (non-Javadoc)
43      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
44      */

45     public boolean isUniverse() {
46         return fLHS.isUniverse() && fRHS.isUniverse();
47     }
48
49     /* (non-Javadoc)
50      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
51      */

52     public TypeSet makeClone() {
53         return this; //new TypeSetIntersection(fLHS.makeClone(), fRHS.makeClone());
54
}
55
56     /* (non-Javadoc)
57      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
58      */

59     public boolean isEmpty() {
60         if (fLHS.isEmpty() || fRHS.isEmpty())
61             return true;
62         if (fLHS.isUniverse() || fRHS.isUniverse())
63             return false;
64         // Another quick check we can make before jumping to the expensive stuff
65
if (fLHS.contains(getJavaLangObject()) && fRHS.contains(getJavaLangObject()))
66             return false;
67
68 // TypeSet lhsLB= fLHS.lowerBound();
69
// TypeSet rhsUB= fRHS.upperBound();
70
//
71
// // Avoid the infinite recursion that will occur if lhsLB == fLHS && rhsUB == fRHS
72
// if ((!lhsLB.equals(fLHS) || !rhsUB.equals(fRHS)) &&
73
// !lhsLB.intersectedWith(rhsUB).isEmpty())
74
// return false;
75
//
76
// if (areAllSuperTypesOf(lhsLB, rhsUB))
77
// return true;
78
//
79
// TypeSet lhsUB= fLHS.upperBound();
80
// TypeSet rhsLB= fRHS.lowerBound();
81
//
82
// if (!lhsUB.intersectedWith(rhsLB).isEmpty())
83
// return false;
84
//
85
// if (areAllSuperTypesOf(rhsLB, lhsUB))
86
// return true;
87

88         return false;
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
93      */

94     public boolean contains(TType t) {
95         return fLHS.contains(t) && fRHS.contains(t);
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
100      */

101     public boolean containsAll(TypeSet s) {
102         return fLHS.containsAll(s) && fRHS.containsAll(s);
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#subTypes()
107      */

108     public TypeSet subTypes() {
109         if (isUniverse() || contains(getJavaLangObject()))
110             return getTypeSetEnvironment().getUniverseTypeSet();
111         // sub(xsect(sub(a),sub(b))) == xsect(sub(a),sub(b))
112
if ((fLHS instanceof SubTypesSet || fLHS instanceof SubTypesOfSingleton) &&
113             (fRHS instanceof SubTypesSet || fRHS instanceof SubTypesOfSingleton))
114             return this;
115         return getTypeSetEnvironment().createSubTypesSet(this);
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#superTypes()
120      */

121     public TypeSet superTypes() {
122         // super(xsect(super(a),super(b))) == xsect(super(a),super(b))
123
if ((fLHS instanceof SuperTypesSet || fLHS instanceof SuperTypesOfSingleton) &&
124             (fRHS instanceof SuperTypesSet || fRHS instanceof SuperTypesOfSingleton))
125             return this;
126         return getTypeSetEnvironment().createSuperTypesSet(this);
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
131      */

132     public TypeSet upperBound() {
133         if (fLHS.contains(getJavaLangObject()) && fRHS.contains(getJavaLangObject()))
134             return new SingletonTypeSet(getTypeSetEnvironment().getJavaLangObject(), getTypeSetEnvironment());
135
136         if (fEnumCache != null) return fEnumCache.upperBound();
137
138         EnumeratedTypeSet lhsSet= fLHS.enumerate();
139         EnumeratedTypeSet rhsSet= fRHS.enumerate();
140         TypeSet xsect= lhsSet.intersectedWith(rhsSet);
141
142         return xsect.upperBound();
143     }
144
145     /* (non-Javadoc)
146      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
147      */

148     public TypeSet lowerBound() {
149         if (fLHS.hasUniqueLowerBound() && fRHS.hasUniqueLowerBound()) {
150             TType lhsBound= fLHS.uniqueLowerBound();
151             TType rhsBound= fRHS.uniqueLowerBound();
152
153             if (lhsBound.equals(rhsBound))
154                 return new SingletonTypeSet(lhsBound, getTypeSetEnvironment());
155             else if (TTypes.canAssignTo(lhsBound, rhsBound))
156                 return new SingletonTypeSet(rhsBound, getTypeSetEnvironment());
157             else if (TTypes.canAssignTo(rhsBound, lhsBound))
158                 return new SingletonTypeSet(lhsBound, getTypeSetEnvironment());
159         }
160         if (fEnumCache != null) return fEnumCache.lowerBound();
161
162         EnumeratedTypeSet lhsSet= fLHS.enumerate();
163         EnumeratedTypeSet rhsSet= fRHS.enumerate();
164         TypeSet xsect= lhsSet.intersectedWith(rhsSet);
165
166         return xsect.lowerBound();
167     }
168
169     protected TypeSet specialCasesIntersectedWith(TypeSet s2) {
170         if (s2.equals(fLHS)) // xsect(s2,xsect(s2,?)) = xsect(s2,?)
171
return this;
172         if (s2.equals(fRHS)) // xsect(s2,xsect(?,s2)) = xsect(?,s2)
173
return this;
174         if (s2 instanceof TypeSetIntersection) {
175             TypeSetIntersection x2= (TypeSetIntersection) s2;
176             //
177
// The following should use a "quick equals()" guaranteed to be constant-time
178
//
179
// xsect(xsect(A,B),xsect(A,C)) = xsect(xsect(A,B),C)
180
if (fLHS.equals(x2.fLHS))
181                 return new TypeSetIntersection(this, x2.fRHS);
182             // xsect(xsect(A,B),xsect(C,A)) = xsect(xsect(A,B),C)
183
if (fLHS.equals(x2.fRHS))
184                 return new TypeSetIntersection(this, x2.fLHS);
185             // xsect(xsect(A,B),xsect(B,C)) = xsect(xsect(A,B),C)
186
if (fRHS.equals(x2.fLHS))
187                 return new TypeSetIntersection(this, x2.fRHS);
188             // xsect(xsect(A,B),xsect(C,B)) = xsect(xsect(A,B),C)
189
if (fRHS.equals(x2.fRHS))
190                 return new TypeSetIntersection(this, x2.fLHS);
191         }
192         return null;
193     }
194
195     /* (non-Javadoc)
196      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
197      */

198     public boolean isSingleton() {
199         if (fEnumCache != null) return fEnumCache.isSingleton();
200
201         int count= 0;
202         for(Iterator JavaDoc lhsIter= fLHS.iterator(); lhsIter.hasNext(); ) {
203             TType t= (TType) lhsIter.next();
204             if (fRHS.contains(t))
205                 count++;
206             if (count > 1)
207                 return false;
208         }
209         return (count == 1);
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
214      */

215     public TType anyMember() {
216         if (fEnumCache != null) return fEnumCache.anyMember();
217
218         for(Iterator JavaDoc lhsIter= fLHS.iterator(); lhsIter.hasNext(); ) {
219             TType t= (TType) lhsIter.next();
220             if (fRHS.contains(t))
221                 return t;
222         }
223         return null;
224     }
225
226     /* (non-Javadoc)
227      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
228      */

229     public Iterator JavaDoc iterator() {
230         return enumerate().iterator();
231
232 // return new Iterator() {
233
// private Iterator fLHSIter= fLHS.iterator();
234
// private TType fNext= null;
235
// public void remove() {
236
// throw new IllegalStateException("Unimplemented");
237
// }
238
// private void advance() {
239
// for(; fLHSIter.hasNext(); ) {
240
// TType t= (TType) fLHSIter.next();
241
// if (fRHS.contains(t)) {
242
// fNext= t;
243
// break;
244
// }
245
// }
246
// }
247
// public boolean hasNext() {
248
// if (fNext == null)
249
// advance();
250
// return fNext != null;
251
// }
252
// public Object next() {
253
// if (fNext == null)
254
// advance();
255
// if (fNext == null)
256
// throw new NoSuchElementException("No more elements in TypeSetIntersection");
257
// TType result= fNext;
258
// fNext= null;
259
// return result;
260
// }
261
// };
262
}
263
264     /* (non-Javadoc)
265      * @see java.lang.Object#equals(java.lang.Object)
266      */

267     public boolean equals(Object JavaDoc o) {
268         if (o instanceof TypeSetIntersection) {
269             TypeSetIntersection other= (TypeSetIntersection) o;
270             return other.fLHS.equals(fLHS) && other.fRHS.equals(fRHS);
271         } else
272             return false;
273     }
274
275     public String JavaDoc toString() {
276         return "<" + fID + ": intersect(" + fLHS + "," + fRHS + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
277
}
278
279     /* (non-Javadoc)
280      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
281      */

282     public boolean hasUniqueLowerBound() {
283         return false;
284     }
285
286     /* (non-Javadoc)
287      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
288      */

289     public boolean hasUniqueUpperBound() {
290         return false;
291     }
292
293     /* (non-Javadoc)
294      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
295      */

296     public TType uniqueLowerBound() {
297         return null;
298     }
299
300     /* (non-Javadoc)
301      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
302      */

303     public TType uniqueUpperBound() {
304         return null;
305     }
306
307     private EnumeratedTypeSet fEnumCache= null;
308
309     /* (non-Javadoc)
310      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
311      */

312     public EnumeratedTypeSet enumerate() {
313         if (fEnumCache == null) {
314             EnumeratedTypeSet lhsSet= fLHS.enumerate();
315             EnumeratedTypeSet rhsSet= fRHS.enumerate();
316             fEnumCache= lhsSet.intersectedWith(rhsSet).enumerate();
317         }
318
319         return fEnumCache;
320     }
321 }
322
Popular Tags