001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType;
015: import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
016: import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
017:
018: /**
019: * Represents the set of array types whose element types are in a given TypeSet.
020: * I.e., ArrayTypeSet(S) = { x[] | x \in S }
021: */
022: public class ArrayTypeSet extends TypeSet {
023: protected TypeSet fElemTypeSet;
024:
025: protected ArrayTypeSet(TypeSetEnvironment typeSetEnvironment) {
026: super (typeSetEnvironment);
027: }
028:
029: public ArrayTypeSet(TypeSet s) {
030: super (s.getTypeSetEnvironment());
031: fElemTypeSet = s;
032: }
033:
034: /**
035: * @return Returns the element TypeSet.
036: */
037: public TypeSet getElemTypeSet() {
038: return fElemTypeSet;
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isUniverse()
043: */
044: public boolean isUniverse() {
045: return false;
046: }
047:
048: /* (non-Javadoc)
049: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#makeClone()
050: */
051: public TypeSet makeClone() {
052: return new ArrayTypeSet(fElemTypeSet);
053: }
054:
055: /* (non-Javadoc)
056: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isEmpty()
057: */
058: public boolean isEmpty() {
059: return fElemTypeSet.isEmpty();
060: }
061:
062: /* (non-Javadoc)
063: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#upperBound()
064: */
065: public TypeSet upperBound() {
066: return new ArrayTypeSet(fElemTypeSet.upperBound());
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#lowerBound()
071: */
072: public TypeSet lowerBound() {
073: return new ArrayTypeSet(fElemTypeSet.lowerBound());
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueLowerBound()
078: */
079: public boolean hasUniqueLowerBound() {
080: return fElemTypeSet.hasUniqueLowerBound();
081: }
082:
083: /* (non-Javadoc)
084: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#hasUniqueUpperBound()
085: */
086: public boolean hasUniqueUpperBound() {
087: return fElemTypeSet.hasUniqueUpperBound();
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueLowerBound()
092: */
093: public TType uniqueLowerBound() {
094: return TTypes.createArrayType(fElemTypeSet.uniqueLowerBound(),
095: 1);
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#uniqueUpperBound()
100: */
101: public TType uniqueUpperBound() {
102: return TTypes.createArrayType(fElemTypeSet.uniqueUpperBound(),
103: 1);
104: }
105:
106: /* (non-Javadoc)
107: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#contains(TType)
108: */
109: public boolean contains(TType t) {
110: if (!(t instanceof ArrayType))
111: return false;
112: ArrayType at = (ArrayType) t;
113: return fElemTypeSet.contains(at.getComponentType());
114: }
115:
116: /* (non-Javadoc)
117: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#containsAll(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)
118: */
119: public boolean containsAll(TypeSet s) {
120: if (s instanceof ArrayTypeSet
121: && !(s instanceof ArraySuperTypeSet)) {
122: ArrayTypeSet ats = (ArrayTypeSet) s;
123:
124: return fElemTypeSet.containsAll(ats.fElemTypeSet);
125: }
126: for (Iterator iter = s.iterator(); iter.hasNext();) {
127: TType t = (TType) iter.next();
128: if (!contains(t))
129: return false;
130: }
131: return true;
132: }
133:
134: /* (non-Javadoc)
135: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#iterator()
136: */
137: public Iterator iterator() {
138: if (fEnumCache != null)
139: return fEnumCache.iterator();
140:
141: return new Iterator() {
142: Iterator fElemIter = fElemTypeSet.iterator();
143:
144: public boolean hasNext() {
145: return fElemIter.hasNext();
146: }
147:
148: public Object next() {
149: return TTypes.createArrayType(
150: ((TType) fElemIter.next()), 1);
151: }
152:
153: public void remove() {
154: throw new UnsupportedOperationException();
155: }
156: };
157: }
158:
159: private EnumeratedTypeSet fEnumCache = null;
160:
161: /* (non-Javadoc)
162: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#enumerate()
163: */
164: public EnumeratedTypeSet enumerate() {
165: if (fEnumCache == null) {
166: fEnumCache = new EnumeratedTypeSet(getTypeSetEnvironment());
167:
168: for (Iterator iter = fElemTypeSet.iterator(); iter
169: .hasNext();) {
170: TType t = (TType) iter.next();
171: fEnumCache.add(TTypes.createArrayType(t, 1));
172: }
173: fEnumCache.initComplete();
174: }
175: return fEnumCache;
176: }
177:
178: /* (non-Javadoc)
179: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#isSingleton()
180: */
181: public boolean isSingleton() {
182: return fElemTypeSet.isSingleton();
183: }
184:
185: /* (non-Javadoc)
186: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#anyMember()
187: */
188: public TType anyMember() {
189: return TTypes.createArrayType(fElemTypeSet.anyMember(), 1);
190: }
191:
192: /* (non-Javadoc)
193: * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet#superTypes()
194: */
195: public TypeSet super Types() {
196: return new ArraySuperTypeSet(fElemTypeSet);
197: }
198:
199: /* (non-Javadoc)
200: * @see java.lang.Object#equals(java.lang.Object)
201: */
202: public boolean equals(Object obj) {
203: if (obj == this )
204: return true;
205: if (obj instanceof ArrayTypeSet) {
206: ArrayTypeSet other = (ArrayTypeSet) obj;
207:
208: return fElemTypeSet.equals(other.fElemTypeSet);
209: }
210: return false;
211: }
212:
213: public String toString() {
214: return "{" + fID + ": array(" + fElemTypeSet + ")}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
215: }
216: }
|