]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/typeconstraints/typesets/TypeSetIntersection.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / typeconstraints / typesets / TypeSetIntersection.java
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.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         @Override
46         public boolean isUniverse() {
47                 return fLHS.isUniverse() && fRHS.isUniverse();
48         }
49
50         /* (non-Javadoc)
51          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
52          */
53         @Override
54         public TypeSet makeClone() {
55                 return this; //new TypeSetIntersection(fLHS.makeClone(), fRHS.makeClone());
56         }
57
58         /* (non-Javadoc)
59          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
60          */
61         @Override
62         public boolean isEmpty() {
63                 if (fLHS.isEmpty() || fRHS.isEmpty())
64                         return true;
65                 if (fLHS.isUniverse() || fRHS.isUniverse())
66                         return false;
67                 // Another quick check we can make before jumping to the expensive stuff
68                 if (fLHS.contains(getJavaLangObject()) && fRHS.contains(getJavaLangObject()))
69                         return false;
70
71 //              TypeSet lhsLB= fLHS.lowerBound();
72 //              TypeSet rhsUB= fRHS.upperBound();
73 //
74 //              // Avoid the infinite recursion that will occur if lhsLB == fLHS && rhsUB == fRHS
75 //              if ((!lhsLB.equals(fLHS) || !rhsUB.equals(fRHS)) &&
76 //                              !lhsLB.intersectedWith(rhsUB).isEmpty())
77 //                      return false;
78 //
79 //              if (areAllSuperTypesOf(lhsLB, rhsUB))
80 //                      return true;
81 //
82 //              TypeSet lhsUB= fLHS.upperBound();
83 //              TypeSet rhsLB= fRHS.lowerBound();
84 //
85 //              if (!lhsUB.intersectedWith(rhsLB).isEmpty())
86 //                      return false;
87 //
88 //              if (areAllSuperTypesOf(rhsLB, lhsUB))
89 //                      return true;
90
91                 return false;
92         }
93
94         /* (non-Javadoc)
95          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
96          */
97         @Override
98         public boolean contains(TType t) {
99                 return fLHS.contains(t) && fRHS.contains(t);
100         }
101
102         /* (non-Javadoc)
103          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
104          */
105         @Override
106         public boolean containsAll(TypeSet s) {
107                 return fLHS.containsAll(s) && fRHS.containsAll(s);
108         }
109
110         /* (non-Javadoc)
111          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#subTypes()
112          */
113         @Override
114         public TypeSet subTypes() {
115                 if (isUniverse() || contains(getJavaLangObject()))
116                         return getTypeSetEnvironment().getUniverseTypeSet();
117                 // sub(xsect(sub(a),sub(b))) == xsect(sub(a),sub(b))
118                 if ((fLHS instanceof SubTypesSet || fLHS instanceof SubTypesOfSingleton) &&
119                         (fRHS instanceof SubTypesSet || fRHS instanceof SubTypesOfSingleton))
120                         return this;
121                 return getTypeSetEnvironment().createSubTypesSet(this);
122         }
123
124         /* (non-Javadoc)
125          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#superTypes()
126          */
127         @Override
128         public TypeSet superTypes() {
129                 // super(xsect(super(a),super(b))) == xsect(super(a),super(b))
130                 if ((fLHS instanceof SuperTypesSet || fLHS instanceof SuperTypesOfSingleton) &&
131                         (fRHS instanceof SuperTypesSet || fRHS instanceof SuperTypesOfSingleton))
132                         return this;
133                 return getTypeSetEnvironment().createSuperTypesSet(this);
134         }
135
136         /* (non-Javadoc)
137          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
138          */
139         @Override
140         public TypeSet upperBound() {
141                 if (fLHS.contains(getJavaLangObject()) && fRHS.contains(getJavaLangObject()))
142                         return new SingletonTypeSet(getTypeSetEnvironment().getJavaLangObject(), getTypeSetEnvironment());
143
144                 if (fEnumCache != null) return fEnumCache.upperBound();
145
146                 EnumeratedTypeSet lhsSet= fLHS.enumerate();
147                 EnumeratedTypeSet rhsSet= fRHS.enumerate();
148                 TypeSet xsect= lhsSet.intersectedWith(rhsSet);
149
150                 return xsect.upperBound();
151         }
152
153         /* (non-Javadoc)
154          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
155          */
156         @Override
157         public TypeSet lowerBound() {
158                 if (fLHS.hasUniqueLowerBound() && fRHS.hasUniqueLowerBound()) {
159                         TType lhsBound= fLHS.uniqueLowerBound();
160                         TType rhsBound= fRHS.uniqueLowerBound();
161
162                         if (lhsBound.equals(rhsBound))
163                                 return new SingletonTypeSet(lhsBound, getTypeSetEnvironment());
164                         else if (TTypes.canAssignTo(lhsBound, rhsBound))
165                                 return new SingletonTypeSet(rhsBound, getTypeSetEnvironment());
166                         else if (TTypes.canAssignTo(rhsBound, lhsBound))
167                                 return new SingletonTypeSet(lhsBound, getTypeSetEnvironment());
168                 }
169                 if (fEnumCache != null) return fEnumCache.lowerBound();
170
171                 EnumeratedTypeSet lhsSet= fLHS.enumerate();
172                 EnumeratedTypeSet rhsSet= fRHS.enumerate();
173                 TypeSet xsect= lhsSet.intersectedWith(rhsSet);
174
175                 return xsect.lowerBound();
176         }
177
178         @Override
179         protected TypeSet specialCasesIntersectedWith(TypeSet s2) {
180                 if (s2.equals(fLHS)) // xsect(s2,xsect(s2,?)) = xsect(s2,?)
181                         return this;
182                 if (s2.equals(fRHS)) // xsect(s2,xsect(?,s2)) = xsect(?,s2)
183                         return this;
184                 if (s2 instanceof TypeSetIntersection) {
185                         TypeSetIntersection x2= (TypeSetIntersection) s2;
186                         //
187                         // The following should use a "quick equals()" guaranteed to be constant-time
188                         //
189                         // xsect(xsect(A,B),xsect(A,C)) = xsect(xsect(A,B),C)
190                         if (fLHS.equals(x2.fLHS))
191                                 return new TypeSetIntersection(this, x2.fRHS);
192                         // xsect(xsect(A,B),xsect(C,A)) = xsect(xsect(A,B),C)
193                         if (fLHS.equals(x2.fRHS))
194                                 return new TypeSetIntersection(this, x2.fLHS);
195                         // xsect(xsect(A,B),xsect(B,C)) = xsect(xsect(A,B),C)
196                         if (fRHS.equals(x2.fLHS))
197                                 return new TypeSetIntersection(this, x2.fRHS);
198                         // xsect(xsect(A,B),xsect(C,B)) = xsect(xsect(A,B),C)
199                         if (fRHS.equals(x2.fRHS))
200                                 return new TypeSetIntersection(this, x2.fLHS);
201                 }
202                 return null;
203         }
204
205         /* (non-Javadoc)
206          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
207          */
208         @Override
209         public boolean isSingleton() {
210                 if (fEnumCache != null) return fEnumCache.isSingleton();
211
212                 int count= 0;
213                 for(Iterator<TType> lhsIter= fLHS.iterator(); lhsIter.hasNext(); ) {
214                         TType t= lhsIter.next();
215                         if (fRHS.contains(t))
216                                 count++;
217                         if (count > 1)
218                                 return false;
219                 }
220                 return (count == 1);
221         }
222
223         /* (non-Javadoc)
224          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
225          */
226         @Override
227         public TType anyMember() {
228                 if (fEnumCache != null) return fEnumCache.anyMember();
229
230                 for(Iterator<TType> lhsIter= fLHS.iterator(); lhsIter.hasNext(); ) {
231                         TType t= lhsIter.next();
232                         if (fRHS.contains(t))
233                                 return t;
234                 }
235                 return null;
236         }
237
238         /* (non-Javadoc)
239          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
240          */
241         @Override
242         public Iterator<TType> iterator() {
243                 return enumerate().iterator();
244
245 //              return new Iterator() {
246 //                      private Iterator fLHSIter= fLHS.iterator();
247 //                      private TType fNext= null;
248 //                      public void remove() {
249 //                              throw new IllegalStateException("Unimplemented");
250 //                      }
251 //                      private void advance() {
252 //                              for(; fLHSIter.hasNext(); ) {
253 //                                      TType t= (TType) fLHSIter.next();
254 //                                      if (fRHS.contains(t)) {
255 //                                              fNext= t;
256 //                                              break;
257 //                                      }
258 //                              }
259 //                      }
260 //                      public boolean hasNext() {
261 //                              if (fNext == null)
262 //                                      advance();
263 //                              return fNext != null;
264 //                      }
265 //                      public Object next() {
266 //                              if (fNext == null)
267 //                                      advance();
268 //                              if (fNext == null)
269 //                                      throw new NoSuchElementException("No more elements in TypeSetIntersection");
270 //                              TType result= fNext;
271 //                              fNext= null;
272 //                              return result;
273 //                      }
274 //              };
275         }
276
277         /* (non-Javadoc)
278          * @see java.lang.Object#equals(java.lang.Object)
279          */
280         @Override
281         public boolean equals(Object o) {
282                 if (o instanceof TypeSetIntersection) {
283                         TypeSetIntersection other= (TypeSetIntersection) o;
284                         return other.fLHS.equals(fLHS) && other.fRHS.equals(fRHS);
285                 } else
286                         return false;
287         }
288
289         @Override
290         public int hashCode() {
291                 return fLHS.hashCode() * 37 + fRHS.hashCode();
292         }
293         
294         @Override
295         public String toString() {
296                 return "<" + fID + ": intersect(" + fLHS + "," + fRHS + ")>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
297         }
298
299         /* (non-Javadoc)
300          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
301          */
302         @Override
303         public boolean hasUniqueLowerBound() {
304                 return false;
305         }
306
307         /* (non-Javadoc)
308          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
309          */
310         @Override
311         public boolean hasUniqueUpperBound() {
312                 return false;
313         }
314
315         /* (non-Javadoc)
316          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
317          */
318         @Override
319         public TType uniqueLowerBound() {
320                 return null;
321         }
322
323         /* (non-Javadoc)
324          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
325          */
326         @Override
327         public TType uniqueUpperBound() {
328                 return null;
329         }
330
331         private EnumeratedTypeSet fEnumCache= null;
332
333         /* (non-Javadoc)
334          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
335          */
336         @Override
337         public EnumeratedTypeSet enumerate() {
338                 if (fEnumCache == null) {
339                         EnumeratedTypeSet lhsSet= fLHS.enumerate();
340                         EnumeratedTypeSet rhsSet= fRHS.enumerate();
341                         fEnumCache= lhsSet.intersectedWith(rhsSet).enumerate();
342                 }
343
344                 return fEnumCache;
345         }
346 }