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.CharCollection;
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: * char 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 CharCollection
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 CharCollectionToCollectionAdapter extends
042: AbstractCollection {
043:
044: /** The underlying primitive collection. */
045: protected CharCollection collection;
046:
047: /**
048: * Creates a new adaption of a collection of char
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 CharCollectionToCollectionAdapter(CharCollection collection) {
058: super ();
059: if (collection == null)
060: Exceptions.nullArgument("collection");
061: this .collection = collection;
062: }
063:
064: /**
065: * Adds an element to this collection. The element is added
066: * to the underlying collection.
067: *
068: * @param o
069: * the element to add to this collection.
070: *
071: * @return <tt>true</tt> if this collection 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 Character Character}.
080: *
081: * @throws UnsupportedOperationException
082: * if the operation is not supported by the
083: * underlying collection.
084: */
085: public boolean add(Object o) {
086: if (o == null)
087: Exceptions.nullElementNotAllowed();
088: return collection.add(((Character) o).charValue());
089: }
090:
091: /**
092: * Clears this collection. The underlying collection is
093: * cleared.
094: *
095: * @throws UnsupportedOperationException
096: * if the operation is not supported by the
097: * underlying collection.
098: */
099: public void clear() {
100: collection.clear();
101: }
102:
103: /**
104: * Indicates whether this collection contains a specified
105: * element. For this collection to contain an object, the
106: * underlying collection must contain its unwrapped value.
107: * <p>Note that this collection can never contain <tt>null</tt>
108: * values or values of other classes than {@link Character Character}.
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: * collection; returns <tt>false</tt> otherwise.
116: */
117: public boolean contains(Object o) {
118: try {
119: return collection.contains(((Character) o).charValue());
120: } catch (ClassCastException cce) {
121: } catch (NullPointerException npe) {
122: }
123: return false;
124: }
125:
126: /**
127: * Returns an iterator over this collection.
128: *
129: * @return an iterator over this collection.
130: */
131: public Iterator iterator() {
132: return new CharIteratorToIteratorAdapter(collection.iterator());
133: }
134:
135: /**
136: * Removes a specified element from this collection.
137: * The unwrapped element is removed from the underlying collection.
138: * <p>Note that this collection can never contain <tt>null</tt>
139: * values or values of other classes than {@link Character Character}.
140: * In those cases, this method will return <tt>false</tt>.
141: *
142: * @param o
143: * the Character value to remove from this collection.
144: *
145: * @return <tt>true</tt> if this collection was modified
146: * as a result of removing <tt>o</tt>; returns
147: * <tt>false</tt> otherwise.
148: *
149: * @throws UnsupportedOperationException
150: * if the operation is not supported by the
151: * underlying collection.
152: */
153: public boolean remove(Object o) {
154: try {
155: return collection.remove(((Character) o).charValue());
156: } catch (ClassCastException cce) {
157: } catch (NullPointerException npe) {
158: }
159: return false;
160: }
161:
162: /**
163: * Removes all the elements of a specified collection from
164: * this collection. The unwrapped elements are removed from
165: * the underlying collection.
166: * <p>This method is only overridden to work
167: * around a bug in {@link AbstractCollection AbstractCollection},
168: * which does not throw a
169: * {@link NullPointerException NullPointerException} when the
170: * argument is <tt>null</tt> and the collection is empty.
171: *
172: * @param c
173: * the collection whose elements to remove from this
174: * collection.
175: *
176: * @return <tt>true</tt> if this collection was modified
177: * as a result of removing the elements of <tt>c</tt>;
178: * returns <tt>false</tt> otherwise.
179: *
180: * @throws UnsupportedOperationException
181: * if the operation is not supported by the underlying
182: * collection.
183: *
184: * @throws NullPointerException
185: * if <tt>c</tt> is <tt>null</tt>.
186: */
187: public boolean removeAll(Collection c) {
188: if (c == null)
189: Exceptions.nullArgument("collection");
190: return super .removeAll(c);
191: }
192:
193: /**
194: * Retains only the elements of a specified collection in
195: * this collection. The unwrapped elements are removed from
196: * the underlying collection.
197: * <p>This method is only overridden to work
198: * around a bug in {@link AbstractCollection AbstractCollection},
199: * which does not throw a
200: * {@link NullPointerException NullPointerException} when the
201: * argument is <tt>null</tt> and the collection is empty.
202: *
203: * @param c
204: * the collection whose elements to retain in this
205: * collection.
206: *
207: * @return <tt>true</tt> if this collection was modified
208: * as a result of removing the elements not contained
209: * in <tt>c</tt>;
210: * returns <tt>false</tt> otherwise.
211: *
212: * @throws UnsupportedOperationException
213: * if the operation is not supported by the underlying
214: * collection.
215: *
216: * @throws NullPointerException
217: * if <tt>c</tt> is <tt>null</tt>.
218: */
219: public boolean retainAll(Collection c) {
220: if (c == null)
221: Exceptions.nullArgument("collection");
222: return super .retainAll(c);
223: }
224:
225: /**
226: * Returns the number of elements in this collection. The
227: * number of elements is the same as that of the underlying
228: * collection.
229: *
230: * @return the number of elements in this collection.
231: */
232: public int size() {
233: return collection.size();
234: }
235:
236: /**
237: * Returns a hash code value for this collection. The hash code
238: * returned is that of the underlying collection.
239: *
240: * @return a hash code value for this collection.
241: */
242: public int hashCode() {
243: return collection.hashCode();
244: }
245:
246: }
|