KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 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.core.runtime.Assert;
16
17
18 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
19
20 public class SingletonTypeSet extends TypeSet {
21     private final TType fType;
22
23     
24     //TODO: encapsulate in factory method and return the same set for known types
25
public SingletonTypeSet(TType t, TypeSetEnvironment typeSetEnvironment) {
26         super(typeSetEnvironment);
27         Assert.isNotNull(t);
28         fType= t;
29     }
30
31     /* (non-Javadoc)
32      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
33      */

34     public boolean isUniverse() {
35         return false;
36     }
37
38     /* (non-Javadoc)
39      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
40      */

41     public TypeSet makeClone() {
42         return this; //new SingletonTypeSet(fType, getTypeSetEnvironment());
43
}
44
45     /* (non-Javadoc)
46      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
47      */

48     protected TypeSet specialCasesIntersectedWith(TypeSet s2) {
49         if (s2.contains(fType))
50             return this;
51         else
52             return getTypeSetEnvironment().getEmptyTypeSet();
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
57      */

58     public boolean isEmpty() {
59         return false;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
64      */

65     public TypeSet upperBound() {
66         return this; // makeClone();
67
}
68
69     /* (non-Javadoc)
70      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
71      */

72     public TypeSet lowerBound() {
73         return this; // makeClone();
74
}
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
78      */

79     public boolean hasUniqueLowerBound() {
80         return true;
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
85      */

86     public boolean hasUniqueUpperBound() {
87         return true;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
92      */

93     public TType uniqueLowerBound() {
94         return fType;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
99      */

100     public TType uniqueUpperBound() {
101         return fType;
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
106      */

107     public boolean contains(TType t) {
108         return fType.equals(t);
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
113      */

114     public boolean containsAll(TypeSet s) {
115         if (s.isEmpty())
116             return true;
117         if (s.isSingleton())
118             return s.anyMember().equals(fType);
119         return false;
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
124      */

125     public Iterator JavaDoc iterator() {
126         return new Iterator JavaDoc() {
127             private boolean done= false;
128             public void remove() {
129                 throw new UnsupportedOperationException JavaDoc();
130             }
131             public boolean hasNext() {
132                 return !done;
133             }
134             public Object JavaDoc next() {
135                 done= true;
136                 return fType;
137             }
138         };
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
143      */

144     public boolean isSingleton() {
145         return true;
146     }
147
148     /* (non-Javadoc)
149      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
150      */

151     public TType anyMember() {
152         return fType;
153     }
154
155     /* (non-Javadoc)
156      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
157      */

158     public EnumeratedTypeSet enumerate() {
159         EnumeratedTypeSet enumeratedTypeSet= new EnumeratedTypeSet(fType, getTypeSetEnvironment());
160         enumeratedTypeSet.initComplete();
161         return enumeratedTypeSet;
162     }
163
164     public boolean equals(Object JavaDoc o) {
165         if (o instanceof SingletonTypeSet) {
166             SingletonTypeSet other= (SingletonTypeSet) o;
167
168             return fType.equals(other.fType);
169         } else if (o instanceof TypeSet) {
170             TypeSet other= (TypeSet) o;
171
172             return other.isSingleton() && other.anyMember().equals(fType);
173         } else
174             return false;
175     }
176
177     public String JavaDoc toString() {
178         return "{" + fID + ": " + fType.getPrettySignature() + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
179
}
180 }
181
Popular Tags