KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > constraints > SuperTypeSet


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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.structure.constraints;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.HierarchyType;
16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
17 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet;
18
19 /**
20  * Optimized type sets for supertype constraint problems.
21  */

22 public abstract class SuperTypeSet implements ITypeSet {
23
24     /** Implementation of an empty set */
25     private static class SuperTypeEmptySet extends SuperTypeSet {
26
27         /*
28          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#chooseSingleType()
29          */

30         public final TType chooseSingleType() {
31             return null;
32         }
33
34         /*
35          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#isEmpty()
36          */

37         public final boolean isEmpty() {
38             return true;
39         }
40
41         /*
42          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#restrictedTo(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet)
43          */

44         public final ITypeSet restrictedTo(final ITypeSet set) {
45             return this;
46         }
47
48         /*
49          * @see java.lang.Object#toString()
50          */

51         public final String JavaDoc toString() {
52             return "EMPTY"; //$NON-NLS-1$
53
}
54     }
55
56     /** Implementation of a singleton */
57     private static class SuperTypeSingletonSet extends SuperTypeSet {
58
59         /** The type */
60         private final TType fType;
61
62         /**
63          * Creates a new super type singleton set.
64          *
65          * @param type the type
66          */

67         private SuperTypeSingletonSet(final TType type) {
68             fType= type;
69         }
70
71         /*
72          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#chooseSingleType()
73          */

74         public final TType chooseSingleType() {
75             return fType;
76         }
77
78         /*
79          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#isEmpty()
80          */

81         public final boolean isEmpty() {
82             return false;
83         }
84
85         /*
86          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#restrictedTo(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet)
87          */

88         public final ITypeSet restrictedTo(final ITypeSet set) {
89             final TType leftErasure= fType.getErasure();
90             if (set instanceof SuperTypeUniverse) {
91                 return this;
92             } else if (set instanceof SuperTypeSingletonSet) {
93                 if (this == set)
94                     return this;
95                 if (fType.isNullType())
96                     return this;
97                 final SuperTypeSingletonSet singleton= (SuperTypeSingletonSet) set;
98                 final TType rightErasure= singleton.fType.getErasure();
99                 if (leftErasure.isGenericType() || rightErasure.isGenericType()) {
100                     if (rightErasure.equals(leftErasure) || ((HierarchyType) leftErasure).isSubType((HierarchyType) rightErasure))
101                         return this;
102                 }
103                 if (rightErasure.isJavaLangObject())
104                     return this;
105                 if (leftErasure.canAssignTo(rightErasure))
106                     return this;
107                 return SuperTypeSet.getEmpty();
108             } else if (set instanceof SuperTypeTuple) {
109                 if (fType.isNullType())
110                     return this;
111                 final SuperTypeTuple tuple= (SuperTypeTuple) set;
112                 final TType rightErasure= tuple.fSuperType.getErasure();
113                 if (leftErasure.isGenericType() || rightErasure.isGenericType()) {
114                     if (rightErasure.equals(leftErasure) || ((HierarchyType) leftErasure).isSubType((HierarchyType) rightErasure))
115                         return this;
116                 }
117                 if (rightErasure.isJavaLangObject())
118                     return this;
119                 if (leftErasure.canAssignTo(rightErasure))
120                     return this;
121                 return SuperTypeSet.createTypeSet(tuple.fSubType);
122             } else if (set instanceof SuperTypeEmptySet) {
123                 return set;
124             } else
125                 Assert.isTrue(false);
126             return null;
127         }
128
129         /*
130          * @see java.lang.Object#toString()
131          */

132         public final String JavaDoc toString() {
133             return fType.getPrettySignature();
134         }
135     }
136
137     /** Implementation of a tuple */
138     private static class SuperTypeTuple extends SuperTypeSet {
139
140         /** The other type */
141         private final TType fSubType;
142
143         /** The super type */
144         private final TType fSuperType;
145
146         /**
147          * Creates a new super type tuple.
148          *
149          * @param subType the sub type
150          * @param superType the super type
151          */

152         private SuperTypeTuple(final TType subType, final TType superType) {
153             fSubType= subType;
154             fSuperType= superType;
155         }
156
157         /*
158          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#chooseSingleType()
159          */

160         public final TType chooseSingleType() {
161             return fSuperType;
162         }
163
164         /*
165          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#isEmpty()
166          */

167         public final boolean isEmpty() {
168             return false;
169         }
170
171         /*
172          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#restrictedTo(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet)
173          */

174         public final ITypeSet restrictedTo(final ITypeSet set) {
175             if (set instanceof SuperTypeUniverse) {
176                 return this;
177             } else if (set instanceof SuperTypeSingletonSet) {
178                 final SuperTypeSingletonSet singleton= (SuperTypeSingletonSet) set;
179                 final TType rightErasure= singleton.fType.getErasure();
180                 final TType subErasure= fSubType.getErasure();
181                 final TType superErasure= fSuperType.getErasure();
182                 if (subErasure.isGenericType() || superErasure.isGenericType() || rightErasure.isGenericType()) {
183                     if ((rightErasure.equals(subErasure) || ((HierarchyType) subErasure).isSubType((HierarchyType) rightErasure)) && (rightErasure.equals(superErasure) || ((HierarchyType) superErasure).isSubType((HierarchyType) rightErasure)))
184                         return this;
185                 }
186                 if (rightErasure.isJavaLangObject())
187                     return this;
188                 if (subErasure.canAssignTo(rightErasure) && superErasure.canAssignTo(rightErasure))
189                     return this;
190                 return SuperTypeSet.createTypeSet(fSubType);
191             } else if (set instanceof SuperTypeTuple) {
192                 return this;
193             } else if (set instanceof SuperTypeEmptySet) {
194                 return set;
195             } else
196                 Assert.isTrue(false);
197             return null;
198         }
199
200         /*
201          * @see java.lang.Object#toString()
202          */

203         public final String JavaDoc toString() {
204             return "[" + fSubType.getPrettySignature() + ", " + fSuperType.getPrettySignature() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
205
}
206     }
207
208     /** Implementation of the type universe */
209     private static class SuperTypeUniverse extends SuperTypeSet {
210
211         /*
212          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#chooseSingleType()
213          */

214         public final TType chooseSingleType() {
215             return null;
216         }
217
218         /*
219          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#isEmpty()
220          */

221         public final boolean isEmpty() {
222             return false;
223         }
224
225         /*
226          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet#restrictedTo(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet)
227          */

228         public final ITypeSet restrictedTo(final ITypeSet set) {
229             return set;
230         }
231
232         /*
233          * @see java.lang.Object#toString()
234          */

235         public final String JavaDoc toString() {
236             return "UNIVERSE"; //$NON-NLS-1$
237
}
238     }
239
240     /** The empty set */
241     private static final ITypeSet fgEmpty= new SuperTypeEmptySet();
242
243     /** The universe */
244     private static final ITypeSet fgUniverse= new SuperTypeUniverse();
245
246     /**
247      * Creates a new type set.
248      *
249      * @param type the type to contain, or <code>null</code>
250      * @return the type set, or the universe if <code>type</code> is <code>null</code>
251      */

252     public static ITypeSet createTypeSet(final TType type) {
253         if (type == null)
254             return fgUniverse;
255         return new SuperTypeSingletonSet(type);
256     }
257
258     /**
259      * Creates a new type set.
260      *
261      * @param subType the sub type
262      * @param superType the super type
263      * @return the type set, or the universe if <code>type</code> is <code>null</code>
264      */

265     public static ITypeSet createTypeSet(final TType subType, final TType superType) {
266         if (subType == null || superType == null)
267             return fgUniverse;
268         return new SuperTypeTuple(subType, superType);
269     }
270
271     /**
272      * Returns the empty set.
273      *
274      * @return the empty set
275      */

276     public static ITypeSet getEmpty() {
277         return fgEmpty;
278     }
279
280     /**
281      * Returns the universe set.
282      *
283      * @return the universe set
284      */

285     public static ITypeSet getUniverse() {
286         return fgUniverse;
287     }
288 }
289
Popular Tags