001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Robert M. Fuhrer (rfuhrer@watson.ibm.com), IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.runtime.Assert;
015:
016: import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
017:
018: public class SingletonTypeSet extends TypeSet {
019: private final TType fType;
020:
021: //TODO: encapsulate in factory method and return the same set for known types
022: public SingletonTypeSet(TType t,
023: TypeSetEnvironment typeSetEnvironment) {
024: super (typeSetEnvironment);
025: Assert.isNotNull(t);
026: fType = t;
027: }
028:
029: /* (non-Javadoc)
030: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
031: */
032: public boolean isUniverse() {
033: return false;
034: }
035:
036: /* (non-Javadoc)
037: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
038: */
039: public TypeSet makeClone() {
040: return this ; //new SingletonTypeSet(fType, getTypeSetEnvironment());
041: }
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#intersectedWith(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
045: */
046: protected TypeSet specialCasesIntersectedWith(TypeSet s2) {
047: if (s2.contains(fType))
048: return this ;
049: else
050: return getTypeSetEnvironment().getEmptyTypeSet();
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
055: */
056: public boolean isEmpty() {
057: return false;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
062: */
063: public TypeSet upperBound() {
064: return this ; // makeClone();
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
069: */
070: public TypeSet lowerBound() {
071: return this ; // makeClone();
072: }
073:
074: /* (non-Javadoc)
075: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
076: */
077: public boolean hasUniqueLowerBound() {
078: return true;
079: }
080:
081: /* (non-Javadoc)
082: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
083: */
084: public boolean hasUniqueUpperBound() {
085: return true;
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
090: */
091: public TType uniqueLowerBound() {
092: return fType;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
097: */
098: public TType uniqueUpperBound() {
099: return fType;
100: }
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
104: */
105: public boolean contains(TType t) {
106: return fType.equals(t);
107: }
108:
109: /* (non-Javadoc)
110: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
111: */
112: public boolean containsAll(TypeSet s) {
113: if (s.isEmpty())
114: return true;
115: if (s.isSingleton())
116: return s.anyMember().equals(fType);
117: return false;
118: }
119:
120: /* (non-Javadoc)
121: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
122: */
123: public Iterator iterator() {
124: return new Iterator() {
125: private boolean done = false;
126:
127: public void remove() {
128: throw new UnsupportedOperationException();
129: }
130:
131: public boolean hasNext() {
132: return !done;
133: }
134:
135: public Object next() {
136: done = true;
137: return fType;
138: }
139: };
140: }
141:
142: /* (non-Javadoc)
143: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
144: */
145: public boolean isSingleton() {
146: return true;
147: }
148:
149: /* (non-Javadoc)
150: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
151: */
152: public TType anyMember() {
153: return fType;
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
158: */
159: public EnumeratedTypeSet enumerate() {
160: EnumeratedTypeSet enumeratedTypeSet = new EnumeratedTypeSet(
161: fType, getTypeSetEnvironment());
162: enumeratedTypeSet.initComplete();
163: return enumeratedTypeSet;
164: }
165:
166: public boolean equals(Object o) {
167: if (o instanceof SingletonTypeSet) {
168: SingletonTypeSet other = (SingletonTypeSet) o;
169:
170: return fType.equals(other.fType);
171: } else if (o instanceof TypeSet) {
172: TypeSet other = (TypeSet) o;
173:
174: return other.isSingleton()
175: && other.anyMember().equals(fType);
176: } else
177: return false;
178: }
179:
180: public String toString() {
181: return "{" + fID + ": " + fType.getPrettySignature() + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
182: }
183: }
|