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.BooleanCollection;
022: import bak.pcj.util.Exceptions;
023: import java.util.Collection;
024: import java.util.AbstractCollection;
025: import java.util.Iterator;
026:
027: /**
028: * This class represents adaptions of primitive collections of
029: * boolean values to Java Collections Framework collections. The adapter
030: * is implemented as a wrapper around a primitive collection. Thus,
031: * changes to the underlying collection are reflected by this
032: * collection and vice versa.
033: *
034: * @see BooleanCollection
035: * @see java.util.Collection
036: *
037: * @author Søren Bak
038: * @version 1.2 21-08-2003 19:01
039: * @since 1.0
040: */
041: public class BooleanCollectionToCollectionAdapter extends
042: AbstractCollection {
043:
044: /** The underlying primitive collection. */
045: protected BooleanCollection collection;
046:
047: /**
048: * Creates a new adaption of a collection of boolean
049: * values to a Java Collections Framework collection.
050: *
051: * @param collection
052: * the underlying primitive collection.
053: *
054: * @throws NullPointerException
055: * if <tt>collection</tt> is <tt>null</tt>.
056: */
057: public BooleanCollectionToCollectionAdapter(
058: BooleanCollection collection) {
059: super ();
060: if (collection == null)
061: Exceptions.nullArgument("collection");
062: this .collection = collection;
063: }
064:
065: /**
066: * Adds an element to this collection. The element is added
067: * to the underlying collection.
068: *
069: * @param o
070: * the element to add to this collection.
071: *
072: * @return <tt>true</tt> if this collection was modified
073: * as a result of adding <tt>o</tt>; returns
074: * <tt>false</tt> otherwise.
075: *
076: * @throws IllegalArgumentException
077: * if <tt>o</tt> is <tt>null</tt>.
078: *
079: * @throws ClassCastException
080: * if <tt>o</tt> is not of class {@link Boolean Boolean}.
081: *
082: * @throws UnsupportedOperationException
083: * if the operation is not supported by the
084: * underlying collection.
085: */
086: public boolean add(Object o) {
087: if (o == null)
088: Exceptions.nullElementNotAllowed();
089: return collection.add(((Boolean) o).booleanValue());
090: }
091:
092: /**
093: * Clears this collection. The underlying collection is
094: * cleared.
095: *
096: * @throws UnsupportedOperationException
097: * if the operation is not supported by the
098: * underlying collection.
099: */
100: public void clear() {
101: collection.clear();
102: }
103:
104: /**
105: * Indicates whether this collection contains a specified
106: * element. For this collection to contain an object, the
107: * underlying collection must contain its unwrapped value.
108: * <p>Note that this collection can never contain <tt>null</tt>
109: * values or values of other classes than {@link Boolean Boolean}.
110: * In those cases, this method will return <tt>false</tt>.
111: *
112: * @param o
113: * the element to test for containment.
114: *
115: * @return <tt>true</tt> if <tt>o</tt> is contained in this
116: * collection; returns <tt>false</tt> otherwise.
117: */
118: public boolean contains(Object o) {
119: try {
120: return collection.contains(((Boolean) o).booleanValue());
121: } catch (ClassCastException cce) {
122: } catch (NullPointerException npe) {
123: }
124: return false;
125: }
126:
127: /**
128: * Returns an iterator over this collection.
129: *
130: * @return an iterator over this collection.
131: */
132: public Iterator iterator() {
133: return new BooleanIteratorToIteratorAdapter(collection
134: .iterator());
135: }
136:
137: /**
138: * Removes a specified element from this collection.
139: * The unwrapped element is removed from the underlying collection.
140: * <p>Note that this collection can never contain <tt>null</tt>
141: * values or values of other classes than {@link Boolean Boolean}.
142: * In those cases, this method will return <tt>false</tt>.
143: *
144: * @param o
145: * the Boolean value to remove from this collection.
146: *
147: * @return <tt>true</tt> if this collection was modified
148: * as a result of removing <tt>o</tt>; returns
149: * <tt>false</tt> otherwise.
150: *
151: * @throws UnsupportedOperationException
152: * if the operation is not supported by the
153: * underlying collection.
154: */
155: public boolean remove(Object o) {
156: try {
157: return collection.remove(((Boolean) o).booleanValue());
158: } catch (ClassCastException cce) {
159: } catch (NullPointerException npe) {
160: }
161: return false;
162: }
163:
164: /**
165: * Removes all the elements of a specified collection from
166: * this collection. The unwrapped elements are removed from
167: * the underlying collection.
168: * <p>This method is only overridden to work
169: * around a bug in {@link AbstractCollection AbstractCollection},
170: * which does not throw a
171: * {@link NullPointerException NullPointerException} when the
172: * argument is <tt>null</tt> and the collection is empty.
173: *
174: * @param c
175: * the collection whose elements to remove from this
176: * collection.
177: *
178: * @return <tt>true</tt> if this collection was modified
179: * as a result of removing the elements of <tt>c</tt>;
180: * returns <tt>false</tt> otherwise.
181: *
182: * @throws UnsupportedOperationException
183: * if the operation is not supported by the underlying
184: * collection.
185: *
186: * @throws NullPointerException
187: * if <tt>c</tt> is <tt>null</tt>.
188: */
189: public boolean removeAll(Collection c) {
190: if (c == null)
191: Exceptions.nullArgument("collection");
192: return super .removeAll(c);
193: }
194:
195: /**
196: * Retains only the elements of a specified collection in
197: * this collection. The unwrapped elements are removed from
198: * the underlying collection.
199: * <p>This method is only overridden to work
200: * around a bug in {@link AbstractCollection AbstractCollection},
201: * which does not throw a
202: * {@link NullPointerException NullPointerException} when the
203: * argument is <tt>null</tt> and the collection is empty.
204: *
205: * @param c
206: * the collection whose elements to retain in this
207: * collection.
208: *
209: * @return <tt>true</tt> if this collection was modified
210: * as a result of removing the elements not contained
211: * in <tt>c</tt>;
212: * returns <tt>false</tt> otherwise.
213: *
214: * @throws UnsupportedOperationException
215: * if the operation is not supported by the underlying
216: * collection.
217: *
218: * @throws NullPointerException
219: * if <tt>c</tt> is <tt>null</tt>.
220: */
221: public boolean retainAll(Collection c) {
222: if (c == null)
223: Exceptions.nullArgument("collection");
224: return super .retainAll(c);
225: }
226:
227: /**
228: * Returns the number of elements in this collection. The
229: * number of elements is the same as that of the underlying
230: * collection.
231: *
232: * @return the number of elements in this collection.
233: */
234: public int size() {
235: return collection.size();
236: }
237:
238: /**
239: * Returns a hash code value for this collection. The hash code
240: * returned is that of the underlying collection.
241: *
242: * @return a hash code value for this collection.
243: */
244: public int hashCode() {
245: return collection.hashCode();
246: }
247:
248: }
|