KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > adapter > ByteCollectionToCollectionAdapter


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.adapter;
20
21 import bak.pcj.ByteCollection;
22 import bak.pcj.util.Exceptions;
23 import java.util.Collection JavaDoc;
24 import java.util.AbstractCollection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * This class represents adaptions of primitive collections of
29  * byte values to Java Collections Framework collections. The adapter
30  * is implemented as a wrapper around a primitive collection. Thus,
31  * changes to the underlying collection are reflected by this
32  * collection and vice versa.
33  *
34  * @see ByteCollection
35  * @see java.util.Collection
36  *
37  * @author Søren Bak
38  * @version 1.2 21-08-2003 19:01
39  * @since 1.0
40  */

41 public class ByteCollectionToCollectionAdapter extends AbstractCollection JavaDoc {
42
43     /** The underlying primitive collection. */
44     protected ByteCollection collection;
45
46     /**
47      * Creates a new adaption of a collection of byte
48      * values to a Java Collections Framework collection.
49      *
50      * @param collection
51      * the underlying primitive collection.
52      *
53      * @throws NullPointerException
54      * if <tt>collection</tt> is <tt>null</tt>.
55      */

56     public ByteCollectionToCollectionAdapter(ByteCollection collection) {
57         super();
58         if (collection == null)
59             Exceptions.nullArgument("collection");
60         this.collection = collection;
61     }
62
63     /**
64      * Adds an element to this collection. The element is added
65      * to the underlying collection.
66      *
67      * @param o
68      * the element to add to this collection.
69      *
70      * @return <tt>true</tt> if this collection was modified
71      * as a result of adding <tt>o</tt>; returns
72      * <tt>false</tt> otherwise.
73      *
74      * @throws IllegalArgumentException
75      * if <tt>o</tt> is <tt>null</tt>.
76      *
77      * @throws ClassCastException
78      * if <tt>o</tt> is not of class {@link Byte Byte}.
79      *
80      * @throws UnsupportedOperationException
81      * if the operation is not supported by the
82      * underlying collection.
83      */

84     public boolean add(Object JavaDoc o) {
85         if (o == null)
86             Exceptions.nullElementNotAllowed();
87         return collection.add(((Byte JavaDoc)o).byteValue());
88     }
89
90     /**
91      * Clears this collection. The underlying collection is
92      * cleared.
93      *
94      * @throws UnsupportedOperationException
95      * if the operation is not supported by the
96      * underlying collection.
97      */

98     public void clear()
99     { collection.clear(); }
100
101     /**
102      * Indicates whether this collection contains a specified
103      * element. For this collection to contain an object, the
104      * underlying collection must contain its unwrapped value.
105      * <p>Note that this collection can never contain <tt>null</tt>
106      * values or values of other classes than {@link Byte Byte}.
107      * In those cases, this method will return <tt>false</tt>.
108      *
109      * @param o
110      * the element to test for containment.
111      *
112      * @return <tt>true</tt> if <tt>o</tt> is contained in this
113      * collection; returns <tt>false</tt> otherwise.
114      */

115     public boolean contains(Object JavaDoc o) {
116         try {
117             return collection.contains(((Byte JavaDoc)o).byteValue());
118         } catch (ClassCastException JavaDoc cce) {
119         } catch (NullPointerException JavaDoc npe) {
120         }
121         return false;
122     }
123
124     /**
125      * Returns an iterator over this collection.
126      *
127      * @return an iterator over this collection.
128      */

129     public Iterator JavaDoc iterator()
130     { return new ByteIteratorToIteratorAdapter(collection.iterator()); }
131
132     /**
133      * Removes a specified element from this collection.
134      * The unwrapped element is removed from the underlying collection.
135      * <p>Note that this collection can never contain <tt>null</tt>
136      * values or values of other classes than {@link Byte Byte}.
137      * In those cases, this method will return <tt>false</tt>.
138      *
139      * @param o
140      * the Byte value to remove from this collection.
141      *
142      * @return <tt>true</tt> if this collection was modified
143      * as a result of removing <tt>o</tt>; returns
144      * <tt>false</tt> otherwise.
145      *
146      * @throws UnsupportedOperationException
147      * if the operation is not supported by the
148      * underlying collection.
149      */

150     public boolean remove(Object JavaDoc o) {
151         try {
152             return collection.remove(((Byte JavaDoc)o).byteValue());
153         } catch (ClassCastException JavaDoc cce) {
154         } catch (NullPointerException JavaDoc npe) {
155         }
156         return false;
157     }
158
159     /**
160      * Removes all the elements of a specified collection from
161      * this collection. The unwrapped elements are removed from
162      * the underlying collection.
163      * <p>This method is only overridden to work
164      * around a bug in {@link AbstractCollection AbstractCollection},
165      * which does not throw a
166      * {@link NullPointerException NullPointerException} when the
167      * argument is <tt>null</tt> and the collection is empty.
168      *
169      * @param c
170      * the collection whose elements to remove from this
171      * collection.
172      *
173      * @return <tt>true</tt> if this collection was modified
174      * as a result of removing the elements of <tt>c</tt>;
175      * returns <tt>false</tt> otherwise.
176      *
177      * @throws UnsupportedOperationException
178      * if the operation is not supported by the underlying
179      * collection.
180      *
181      * @throws NullPointerException
182      * if <tt>c</tt> is <tt>null</tt>.
183      */

184     public boolean removeAll(Collection c) {
185         if (c == null)
186             Exceptions.nullArgument("collection");
187         return super.removeAll(c);
188     }
189
190     /**
191      * Retains only the elements of a specified collection in
192      * this collection. The unwrapped elements are removed from
193      * the underlying collection.
194      * <p>This method is only overridden to work
195      * around a bug in {@link AbstractCollection AbstractCollection},
196      * which does not throw a
197      * {@link NullPointerException NullPointerException} when the
198      * argument is <tt>null</tt> and the collection is empty.
199      *
200      * @param c
201      * the collection whose elements to retain in this
202      * collection.
203      *
204      * @return <tt>true</tt> if this collection was modified
205      * as a result of removing the elements not contained
206      * in <tt>c</tt>;
207      * returns <tt>false</tt> otherwise.
208      *
209      * @throws UnsupportedOperationException
210      * if the operation is not supported by the underlying
211      * collection.
212      *
213      * @throws NullPointerException
214      * if <tt>c</tt> is <tt>null</tt>.
215      */

216     public boolean retainAll(Collection c) {
217         if (c == null)
218             Exceptions.nullArgument("collection");
219         return super.retainAll(c);
220     }
221
222     /**
223      * Returns the number of elements in this collection. The
224      * number of elements is the same as that of the underlying
225      * collection.
226      *
227      * @return the number of elements in this collection.
228      */

229     public int size()
230     { return collection.size(); }
231
232     /**
233      * Returns a hash code value for this collection. The hash code
234      * returned is that of the underlying collection.
235      *
236      * @return a hash code value for this collection.
237      */

238     public int hashCode()
239     { return collection.hashCode(); }
240
241 }
Popular Tags