001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: // Requires Java2
014: package com.ibm.richtext.textlayout.attributes;
015:
016: import java.util.Hashtable;
017: import java.util.Enumeration;
018: import java.util.Iterator;
019: import java.util.Set;
020: import java.util.Collection;
021:
022: /**
023: * An AttributeSet is an immutable collection of unique Objects.
024: * It has several operations
025: * which return new AttributeSet instances.
026: */
027: public final class AttributeSet implements Set {
028:
029: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
030:
031: /**
032: * An AttributeSet with no members.
033: */
034: public static final AttributeSet EMPTY_SET = new AttributeSet();
035:
036: private Hashtable elements;
037:
038: private static final String errString = "AttributeSet is immutable.";
039:
040: private AttributeSet(Hashtable elements) {
041:
042: this .elements = elements;
043: }
044:
045: /**
046: * Package only. For AttributeMap use.
047: */
048: static AttributeSet createKeySet(Hashtable hashtable) {
049:
050: Hashtable newElements = new Hashtable();
051:
052: Enumeration e = hashtable.keys();
053: while (e.hasMoreElements()) {
054: Object next = e.nextElement();
055: newElements.put(next, next);
056: }
057:
058: return new AttributeSet(newElements);
059: }
060:
061: /**
062: * Create a new, empty AttributeSet. The set is semantically
063: * equivalent to EMPTY_SET.
064: */
065: public AttributeSet() {
066:
067: elements = new Hashtable();
068: }
069:
070: /**
071: * Create a new AttributeSet with the single element elem.
072: */
073: public AttributeSet(Object elem) {
074:
075: elements = new Hashtable(1, 1);
076: elements.put(elem, elem);
077: }
078:
079: /**
080: * Create a new AttributeSet containing the items in the array elems.
081: */
082: public AttributeSet(Object[] elems) {
083:
084: elements = new Hashtable(elems.length, 1);
085: for (int i = 0; i < elems.length; i++) {
086: Object next = elems[i];
087: elements.put(next, next);
088: }
089: }
090:
091: /**
092: * Return true if the number of elements in this set is 0.
093: * @return true if the number of elements in this set is 0
094: */
095: public boolean isEmpty() {
096:
097: return elements.isEmpty();
098: }
099:
100: /**
101: * Return the number of elements in this set.
102: * @return the number of elements in this set
103: */
104: public int size() {
105:
106: return elements.size();
107: }
108:
109: public boolean equals(Object rhs) {
110:
111: try {
112: return equals((AttributeSet) rhs);
113: } catch (ClassCastException e) {
114: return false;
115: }
116: }
117:
118: public boolean equals(AttributeSet rhs) {
119:
120: if (rhs == null) {
121: return false;
122: }
123:
124: return elements.equals(rhs.elements);
125: }
126:
127: /**
128: * Return true if this set contains the given Object
129: * @return true if this set contains <code>o</code>
130: */
131: public boolean contains(Object o) {
132:
133: Object value = elements.get(o);
134: return value != null;
135: }
136:
137: /**
138: * Return true if this set contains all elements in the given
139: * Collection
140: * @param coll the collection to compare with
141: * @return true if this set contains all elements in the given
142: * Collection
143: */
144: public boolean containsAll(Collection coll) {
145:
146: return elements.keySet().containsAll(coll);
147: }
148:
149: /**
150: * Return an Enumeration of the elements in this set.
151: * @return an Enumeration of the elements in this set
152: */
153: public Enumeration elements() {
154:
155: return elements.keys();
156: }
157:
158: /**
159: * Return an Iterator with the elements in this set.
160: * @return an Iterator with the elements in this set.
161: * The Iterator cannot be used to modify this AttributeSet.
162: */
163: public Iterator iterator() {
164:
165: return new EnumerationIterator(elements.keys());
166: }
167:
168: /**
169: * Fill in the given array with the elements in this set.
170: * @param storage an array to fill with this set's elements.
171: * The array cannot be null.
172: * @return the <tt>storage</tt> array.
173: */
174: public Object[] toArray(Object[] storage) {
175:
176: Enumeration keys = elements.keys();
177: int n = 0;
178: while (keys.hasMoreElements()) {
179: storage[n++] = keys.nextElement();
180: }
181: return storage;
182: }
183:
184: /**
185: * Return an array with the elements in this set.
186: * @return an array with the elements in this set
187: */
188: public Object[] toArray() {
189:
190: return toArray(new Object[size()]);
191: }
192:
193: /**
194: * Throws UnsupportedOperationException.
195: * @see #addElement
196: * @throws UnsupportedOperationException
197: */
198: public boolean add(Object o) {
199: throw new UnsupportedOperationException(errString);
200: }
201:
202: /**
203: * Throws UnsupportedOperationException.
204: * @throws UnsupportedOperationException
205: */
206: public boolean remove(Object o) {
207: throw new UnsupportedOperationException(errString);
208: }
209:
210: /**
211: * Throws UnsupportedOperationException.
212: * @see #unionWith
213: * @throws UnsupportedOperationException
214: */
215: public boolean addAll(Collection coll) {
216: throw new UnsupportedOperationException(errString);
217: }
218:
219: /**
220: * Throws UnsupportedOperationException.
221: * @see #subtract
222: * @throws UnsupportedOperationException
223: */
224: public boolean removeAll(Collection coll) {
225: throw new UnsupportedOperationException(errString);
226: }
227:
228: /**
229: * Throws UnsupportedOperationException.
230: * @see #intersectWith
231: * @throws UnsupportedOperationException
232: */
233: public boolean retainAll(Collection coll) {
234: throw new UnsupportedOperationException(errString);
235: }
236:
237: /**
238: * Throws UnsupportedOperationException.
239: * @see #EMPTY_SET
240: * @throws UnsupportedOperationException
241: */
242: public void clear() {
243: throw new UnsupportedOperationException(errString);
244: }
245:
246: /**
247: * Return an AttributeSet containing the elements of this set
248: * and the given element
249: * @param element the element to add
250: * @return an AttributeSet like this one, with <code>element</code>
251: * added
252: */
253: public AttributeSet addElement(Object element) {
254:
255: Hashtable newElements = (Hashtable) elements.clone();
256: newElements.put(element, element);
257: return new AttributeSet(newElements);
258: }
259:
260: /**
261: * Return an AttributeSet which is the union of
262: * this set with the given set.
263: * @param s the set to union with
264: * @return an AttributeSet of the elements in this set or
265: * in <code>s</code>
266: */
267: public AttributeSet unionWith(AttributeSet s) {
268:
269: Hashtable newElements = (Hashtable) elements.clone();
270:
271: Iterator iter = s.iterator();
272: while (iter.hasNext()) {
273: Object next = iter.next();
274: newElements.put(next, next);
275: }
276:
277: return new AttributeSet(newElements);
278: }
279:
280: /**
281: * Return an AttributeSet which is the intersection of
282: * this set with the given set.
283: * @param s the set to intersect with
284: * @return an AttributeSet of the elements in this set which
285: * are in <code>s</code>
286: */
287: public AttributeSet intersectWith(AttributeSet s) {
288:
289: Hashtable newElements = new Hashtable();
290:
291: Iterator iter = s.iterator();
292: while (iter.hasNext()) {
293: Object next = iter.next();
294: if (elements.get(next) != null) {
295: newElements.put(next, next);
296: }
297: }
298:
299: return new AttributeSet(newElements);
300: }
301:
302: /**
303: * Return an AttributeSet with the elements in this set which
304: * are not in the given set.
305: * @param s the set of elements to exclude
306: * @return an AttributeSet of the elements in this set which
307: * are not in <code>s</code>
308: */
309: public AttributeSet subtract(AttributeSet s) {
310:
311: Hashtable newElements = (Hashtable) elements.clone();
312:
313: Iterator iter = s.iterator();
314: while (iter.hasNext()) {
315: newElements.remove(iter.next());
316: }
317:
318: return new AttributeSet(newElements);
319: }
320:
321: private static final class EnumerationIterator implements Iterator {
322:
323: private Enumeration e;
324:
325: EnumerationIterator(Enumeration e) {
326: this .e = e;
327: }
328:
329: public boolean hasNext() {
330: return e.hasMoreElements();
331: }
332:
333: public Object next() {
334: return e.nextElement();
335: }
336:
337: public void remove() {
338: throw new UnsupportedOperationException(errString);
339: }
340: }
341: }
|