001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.adapter;
020:
021: import bak.pcj.set.BooleanSet;
022: import bak.pcj.adapter.BooleanIteratorToIteratorAdapter;
023: import bak.pcj.util.Exceptions;
024:
025: import java.util.AbstractSet;
026: import java.util.Iterator;
027: import java.util.Collection;
028:
029: /**
030: * This class represents adapters of boolean sets to Java Collections
031: * Framework sets. The adapter
032: * is implemented as a wrapper around a primitive set. Thus,
033: * changes to the underlying set are reflected by this
034: * set and vice versa.
035: *
036: * @see BooleanSet
037: * @see java.util.Set
038: *
039: * @author Søren Bak
040: * @version 1.2 20-08-2003 22:56
041: * @since 1.0
042: */
043: public class BooleanSetToSetAdapter extends AbstractSet {
044:
045: /** The underlying primitive set. */
046: protected BooleanSet set;
047:
048: /**
049: * Creates a new adaption of a set of boolean
050: * values to a Java Collections Framework set.
051: *
052: * @param set
053: * the underlying primitive set.
054: *
055: * @throws NullPointerException
056: * if <tt>set</tt> is <tt>null</tt>.
057: */
058: public BooleanSetToSetAdapter(BooleanSet set) {
059: if (set == null)
060: Exceptions.nullArgument("set");
061: this .set = set;
062: }
063:
064: /**
065: * Adds an element to this set. The unwrapped element is added
066: * to the underlying set.
067: *
068: * @param o
069: * the element to add to this set.
070: *
071: * @return <tt>true</tt> if this set was modified
072: * as a result of adding <tt>o</tt>; returns
073: * <tt>false</tt> otherwise.
074: *
075: * @throws IllegalArgumentException
076: * if <tt>o</tt> is <tt>null</tt>.
077: *
078: * @throws ClassCastException
079: * if <tt>o</tt> is not of class {@link Boolean Boolean}.
080: *
081: * @throws UnsupportedOperationException
082: * if the operation is not supported by the
083: * underlying set.
084: */
085: public boolean add(Object o) {
086: if (o == null)
087: Exceptions.nullElementNotAllowed();
088: return set.add(((Boolean) o).booleanValue());
089: }
090:
091: /**
092: * Clears this collection. The underlying set is
093: * cleared.
094: *
095: * @throws UnsupportedOperationException
096: * if the operation is not supported by the
097: * underlying set.
098: */
099: public void clear() {
100: set.clear();
101: }
102:
103: /**
104: * Indicates whether this set contains a specified
105: * element. For this set to contain an object, the
106: * underlying set must contain its unwrapped value.
107: * <p>Note that this set can never contain <tt>null</tt>
108: * values or values of other classes than {@link Boolean Boolean}.
109: * In those cases, this method will return <tt>false</tt>.
110: *
111: * @param o
112: * the element to test for containment.
113: *
114: * @return <tt>true</tt> if <tt>o</tt> is contained in this
115: * set; returns <tt>false</tt> otherwise.
116: */
117: public boolean contains(Object o) {
118: try {
119: return set.contains(((Boolean) o).booleanValue());
120: } catch (ClassCastException cce) {
121: } catch (NullPointerException npe) {
122: }
123: return false;
124: }
125:
126: /**
127: * Returns a hash code value for this set. The hash code
128: * returned is that of the underlying set.
129: *
130: * @return a hash code value for this set.
131: */
132: public int hashCode() {
133: return set.hashCode();
134: }
135:
136: /**
137: * Returns an iterator over this set.
138: *
139: * @return an iterator over this set.
140: */
141: public Iterator iterator() {
142: return new BooleanIteratorToIteratorAdapter(set.iterator());
143: }
144:
145: /**
146: * Removes a specified element from this set.
147: * The unwrapped element is removed from the underlying set.
148: * <p>Note that this set can never contain <tt>null</tt>
149: * values or values of other classes than {@link Boolean Boolean}.
150: * In those cases, this method will return <tt>false</tt>.
151: *
152: * @param o
153: * the Boolean value to remove from this set.
154: *
155: * @return <tt>true</tt> if this set was modified
156: * as a result of removing <tt>o</tt>; returns
157: * <tt>false</tt> otherwise.
158: *
159: * @throws UnsupportedOperationException
160: * if the operation is not supported by the
161: * underlying set.
162: */
163: public boolean remove(Object o) {
164: try {
165: return set.remove(((Boolean) o).booleanValue());
166: } catch (ClassCastException cce) {
167: } catch (NullPointerException npe) {
168: }
169: return false;
170: }
171:
172: /**
173: * Retains only the elements of a specified collection in
174: * this set. The unwrapped elements are removed from
175: * the underlying set.
176: * <p>This method is only overridden to work
177: * around a bug in {@link AbstractSet AbstractSet},
178: * which does not throw a
179: * {@link NullPointerException NullPointerException} when the
180: * argument is <tt>null</tt> and the set is empty. The
181: * bug is inherited from {@link java.util.AbstractCollection java.util.AbstractCollection}.
182: *
183: * @param c
184: * the collection whose elements to retain in this
185: * collection.
186: *
187: * @return <tt>true</tt> if this set was modified
188: * as a result of removing the elements not contained
189: * in <tt>c</tt>;
190: * returns <tt>false</tt> otherwise.
191: *
192: * @throws UnsupportedOperationException
193: * if the operation is not supported by the underlying
194: * set.
195: *
196: * @throws NullPointerException
197: * if <tt>c</tt> is <tt>null</tt>.
198: */
199: public boolean retainAll(Collection c) {
200: if (c == null)
201: Exceptions.nullArgument("collection");
202: return super .retainAll(c);
203: }
204:
205: /**
206: * Returns the number of elements in this set. The
207: * number of elements is the same as that of the underlying
208: * set.
209: *
210: * @return the number of elements in this set.
211: */
212: public int size() {
213: return set.size();
214: }
215:
216: }
|