KickJava   Java API By Example, From Geeks To Geeks.

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


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.ITypeSet;
17
18 public abstract class TypeSet implements ITypeSet {
19     
20     public TType chooseSingleType() {
21         return null;
22     }
23     
24     public ITypeSet restrictedTo(ITypeSet restrictionSet) {
25         throw new UnsupportedOperationException JavaDoc();
26     }
27     
28     protected TType getJavaLangObject() {
29         return fTypeSetEnvironment.getJavaLangObject();
30     }
31
32     protected TypeSetEnvironment getTypeSetEnvironment() {
33         return fTypeSetEnvironment;
34     }
35
36     static private int sID= 0;
37
38     static public int getCount() { return sID; }
39     static public void resetCount() { sID= 0; }
40
41     /**
42      * An ID unique to this EnumeratedTypeSet instance, to aid in debugging the sharing
43      * of TypeSets across ConstraintVariables in a TypeEstimateEnvironment.
44      */

45     protected final int fID;
46     private final TypeSetEnvironment fTypeSetEnvironment;
47     
48     protected TypeSet(TypeSetEnvironment typeSetEnvironment) {
49         fTypeSetEnvironment= typeSetEnvironment;
50         fID= sID++;
51     }
52
53     /**
54      * @return <code>true</code> iff this set represents the universe of TTypes
55      */

56     abstract public boolean isUniverse();
57
58     abstract public TypeSet makeClone();
59
60     protected TypeSet specialCasesIntersectedWith(TypeSet s2) {
61         return null;
62     }
63
64     /**
65      * Computes and returns a <em>new</em> TypeSet representing the intersection of the
66      * receiver with s2. Does not modify the receiver or argument sets.
67      * @param s2
68      * @return the new TypeSet
69      */

70     public TypeSet intersectedWith(TypeSet s2) {
71         if (s2.isUniverse())
72             return makeClone();
73         else if (isUniverse())
74             return s2.makeClone();
75         else if (isEmpty() || s2.isEmpty())
76             return getTypeSetEnvironment().getEmptyTypeSet();
77         else if (isSingleton()) {
78             if (s2.contains(anyMember()))
79                 return makeClone();
80             else
81                 return getTypeSetEnvironment().getEmptyTypeSet();
82         } else if (s2.isSingleton()) {
83                 if (contains(s2.anyMember()))
84                     return s2.makeClone();
85                 else
86                     return getTypeSetEnvironment().getEmptyTypeSet();
87         } else if (s2 instanceof TypeSetIntersection) {
88             TypeSetIntersection x= (TypeSetIntersection) s2;
89             // xsect(A,xsect(A,B)) = xsect(A,B) and
90
// xsect(B,xsect(A,B)) = xsect(A,B)
91
if (x.getLHS().equals(this) || x.getRHS().equals(this))
92                 return x;
93         }
94
95         TypeSet result= specialCasesIntersectedWith(s2);
96
97         if (result != null)
98             return result;
99         else
100             return new TypeSetIntersection(this, s2);
101     }
102
103     /**
104      * Returns the TypeSet resulting from union'ing the receiver with the argument.
105      * Does not modify the receiver or the argument sets.
106      */

107     public TypeSet addedTo(TypeSet that) {
108         if (isUniverse() || that.isUniverse())
109             return getTypeSetEnvironment().getUniverseTypeSet();
110         if ((this instanceof EnumeratedTypeSet || this instanceof SingletonTypeSet) &&
111             (that instanceof EnumeratedTypeSet || that instanceof SingletonTypeSet)) {
112             EnumeratedTypeSet result= enumerate();
113
114             result.addAll(that);
115             return result;
116         }
117         return new TypeSetUnion(this, that);
118     }
119
120     /**
121      * Returns a new TypeSet representing the set of all sub-types of the
122      * types in the receiver.
123      */

124     public TypeSet subTypes() {
125         if (isUniverse() || contains(getJavaLangObject()))
126             return getTypeSetEnvironment().getUniverseTypeSet();
127
128         if (isSingleton())
129             return possiblyArraySubTypeSetFor(anyMember());
130
131         return getTypeSetEnvironment().createSubTypesSet(this);
132     }
133
134     private TypeSet possiblyArraySubTypeSetFor(TType t) {
135         // In Java, subTypes(x[]) == (subTypes(x))[]
136
// if (t.isArrayType()) {
137
// ArrayType at= (ArrayType) t;
138
//
139
// return new ArrayTypeSet(possiblyArraySubTypeSetFor(at.getArrayElementType()));
140
// } else
141

142         return getTypeSetEnvironment().createSubTypesOfSingleton(t);
143     }
144
145     private TypeSet possiblyArraySuperTypeSetFor(TType t) {
146         // In Java, superTypes(x[]) == (superTypes(x))[] union {Object}
147
// if (t.isArrayType()) {
148
// ArrayType at= (ArrayType) t;
149
//
150
// return new ArraySuperTypeSet(possiblyArraySuperTypeSetFor(at.getArrayElementType()));
151
// } else
152
return getTypeSetEnvironment().createSuperTypesOfSingleton(t);
153     }
154
155     /**
156      * Returns a new TypeSet representing the set of all super-types of the
157      * types in the receiver.
158      */

159     public TypeSet superTypes() {
160         if (isUniverse())
161             return getTypeSetEnvironment().getUniverseTypeSet();
162
163         if (isSingleton())
164             return possiblyArraySuperTypeSetFor(anyMember());
165
166         return getTypeSetEnvironment().createSuperTypesSet(this);
167     }
168
169     /**
170      * Return true iff the type set contains no types.
171      */

172     abstract public boolean isEmpty();
173
174     /**
175      * Returns the types in the upper bound of this set.
176      */

177     abstract public TypeSet upperBound();
178
179     /**
180      * Returns the types in the lower bound of this set.
181      */

182     abstract public TypeSet lowerBound();
183
184     /**
185      * Returns true iff this TypeSet has a unique lower bound.
186      */

187     abstract public boolean hasUniqueLowerBound();
188
189     /**
190      * Returns true iff this TypeSet has a unique upper bound other than
191      * java.lang.Object.
192      */

193     abstract public boolean hasUniqueUpperBound();
194
195     /**
196      * Returns the unique lower bound of this set of types, if it has one,
197      * or null otherwise.
198      */

199     abstract public TType uniqueLowerBound();
200
201     /**
202      * Returns the unique upper bound of this set of types, if it has one,
203      * or null otherwise.
204      */

205     abstract public TType uniqueUpperBound();
206
207     /**
208      * Returns true iff the type set contains the given type.
209      */

210     abstract public boolean contains(TType t);
211
212     /**
213      * Returns true iff the type set contains all of the types in the given TypeSet.
214      */

215     abstract public boolean containsAll(TypeSet s);
216
217     /**
218      * Returns an iterator over the types in the receiver.
219      */

220     abstract public Iterator JavaDoc iterator();
221
222     /**
223      * Returns a new TypeSet enumerating the receiver's contents.
224      */

225     abstract public EnumeratedTypeSet enumerate();
226
227     /**
228      * Returns true iff the given set has precisely one element
229      */

230     abstract public boolean isSingleton();
231
232     /**
233      * Returns an arbitrary member of the given Typeset.
234      */

235     abstract public TType anyMember();
236 }
237
Popular Tags