KickJava   Java API By Example, From Geeks To Geeks.

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


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.list.ByteList;
22 import bak.pcj.list.ByteListIterator;
23 import bak.pcj.util.Exceptions;
24
25 import java.util.AbstractList JavaDoc;
26 import java.util.ListIterator JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.AbstractCollection JavaDoc;
29
30 /**
31  * This class represents adapters of byte lists to Java Collection Framework lists.
32  * The adapter is implemented as a wrapper around a primitive list. Thus,
33  * changes to the underlying list are reflected by this
34  * list and vice versa.
35  *
36  * @see bak.pcj.list.ByteList
37  * @see java.util.List
38  *
39  * @author Søren Bak
40  * @version 1.2 20-08-2003 22:58
41  * @since 1.0
42  */

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

58     public ByteListToListAdapter(ByteList list) {
59         if (list == null)
60             Exceptions.nullArgument("list");
61         this.list = list;
62     }
63
64     /**
65      * Adds all the elements of a specified collection to
66      * this list starting at a specified index. The elements are
67      * inserted in the specified collection's iteration order.
68      * All elements from the specified index and forward are pushed
69      * to their successors' indices (<tt>c.size()</tt> indices).
70      * All elements are added to the underlying list.
71      * <p>This method is only overridden to work
72      * around a bug in {@link AbstractList AbstractList},
73      * which does not throw a
74      * {@link NullPointerException NullPointerException} when the
75      * argument is <tt>null</tt> and the list is empty.
76      *
77      * @param index
78      * the index at which to insert the elements of
79      * the specified collection. If
80      * <tt>index == size()</tt> the elements are appended
81      * to this list.
82      *
83      * @param c
84      * the collection whose elements to add to this
85      * list.
86      *
87      * @return <tt>true</tt> if this list was modified
88      * as a result of adding the elements of <tt>c</tt>;
89      * returns <tt>false</tt> otherwise.
90      *
91      * @throws UnsupportedOperationException
92      * if the operation is not supported by the
93      * underlying list.
94      *
95      * @throws NullPointerException
96      * if <tt>c</tt> is <tt>null</tt>.
97      *
98      * @throws IndexOutOfBoundsException
99      * if <tt>index</tt> does not denote a valid insertion
100      * position (valid: <tt>0 - size()</tt>).
101      *
102      * @throws IllegalArgumentException
103      * if an element of <tt>c</tt> is <tt>null</tt>.
104      *
105      * @throws ClassCastException
106      * if an element of <tt>c</tt> is not of class
107      * {@link Byte Byte}.
108      */

109     public boolean addAll(int index, Collection JavaDoc c) {
110         if (index > size() || index < 0)
111             Exceptions.indexOutOfBounds(index, 0, size());
112         return super.addAll(index, c);
113     }
114
115     /**
116      * Adds an element to this list at a specified index. All
117      * elements from the specified index and forward are pushed
118      * to their successor's indices. The element is added to
119      * the underlying collection.
120      *
121      * @param index
122      * the index at which to add the element. If
123      * <tt>index == size()</tt> the element is appended
124      * to this list.
125      *
126      * @param o
127      * the element to add to this list.
128      *
129      * @throws UnsupportedOperationException
130      * if the operation is not supported by this
131      * list.
132      *
133      * @throws IndexOutOfBoundsException
134      * if <tt>index</tt> does not denote a valid insertion
135      * position (valid: <tt>0 - size()</tt>).
136      *
137      * @throws IllegalArgumentException
138      * if <tt>o</tt> is <tt>null</tt>.
139      *
140      * @throws ClassCastException
141      * if <tt>o</tt> is not of class {@link Byte Byte}.
142      */

143     public void add(int index, Object JavaDoc o) {
144         if (o == null)
145             Exceptions.nullElementNotAllowed();
146         list.add(index, ((Byte JavaDoc)o).byteValue() );
147     }
148
149     /**
150      * Returns the element at a specified position in this list.
151      * The returned value will always be of class {@link Byte Byte}.
152      *
153      * @param index
154      * the position of the element to return.
155      *
156      * @return the element at the specified position.
157      *
158      * @throws IndexOutOfBoundsException
159      * if <tt>index</tt> does not denote a valid index
160      * in this list.
161      */

162     public Object JavaDoc get(int index)
163     { return new Byte JavaDoc(list.get(index)); }
164
165     /**
166      * Returns a list iterator over this list, starting from a
167      * specified index.
168      *
169      * @param index
170      * the index at which to begin the iteration.
171      *
172      * @return a list iterator over this list.
173      *
174      * @throws IndexOutOfBoundsException
175      * if <tt>index</tt> does not denote a valid
176      * iteration position (valid: <tt>0 - size()</tt>).
177      */

178     public ListIterator listIterator(int index)
179     { return new ByteListIteratorToListIteratorAdapter(list.listIterator(index)); }
180
181     /**
182      * Removes the element at a specified index in this list. All
183      * elements following the removed element are pushed to their
184      * predecessor's indices. The element is removed from the
185      * underlying collection.
186      *
187      * @param index
188      * the index of the element to remove.
189      *
190      * @throws UnsupportedOperationException
191      * if the operation is not supported by the underlying
192      * list.
193      *
194      * @throws IndexOutOfBoundsException
195      * if <tt>index</tt> does not denote a valid
196      * element position (valid: <tt>0 - size()-1</tt>).
197      */

198     public Object JavaDoc remove(int index)
199     { return new Byte JavaDoc(list.removeElementAt(index)); }
200
201     /**
202      * Removes all the elements of a specified collection from
203      * this list. The elements are removed from the
204      * underlying list.
205      * <p>This method is only overridden to work
206      * around a bug in {@link AbstractList AbstractList},
207      * which does not throw a
208      * {@link NullPointerException NullPointerException} when the
209      * argument is <tt>null</tt> and the list is empty. The
210      * bug is inherited from {@link AbstractCollection AbstractCollection}.
211      *
212      * @param c
213      * the collection whose elements to remove from this
214      * collection.
215      *
216      * @return <tt>true</tt> if this list was modified
217      * as a result of removing the elements of <tt>c</tt>;
218      * returns <tt>false</tt> otherwise.
219      *
220      * @throws UnsupportedOperationException
221      * if the operation is not supported by the underlying
222      * collection.
223      *
224      * @throws NullPointerException
225      * if <tt>c</tt> is <tt>null</tt>.
226      */

227     public boolean removeAll(Collection JavaDoc c) {
228         if (c == null)
229             Exceptions.nullArgument("collection");
230         return super.removeAll(c);
231     }
232
233     /**
234      * Retains only the elements of a specified collection in
235      * this list. The elements are removed from the
236      * underlying list.
237      * <p>This method is only overridden to work
238      * around a bug in {@link AbstractList AbstractList},
239      * which does not throw a
240      * {@link NullPointerException NullPointerException} when the
241      * argument is <tt>null</tt> and the list is empty. The
242      * bug is inherited from {@link AbstractCollection AbstractCollection}.
243      *
244      * @param c
245      * the collection whose elements to retain in this
246      * collection.
247      *
248      * @return <tt>true</tt> if this list was modified
249      * as a result of removing the elements not contained
250      * in <tt>c</tt>;
251      * returns <tt>false</tt> otherwise.
252      *
253      * @throws UnsupportedOperationException
254      * if the operation is not supported by the underlying
255      * list.
256      *
257      * @throws NullPointerException
258      * if <tt>c</tt> is <tt>null</tt>.
259      */

260     public boolean retainAll(Collection JavaDoc c) {
261         if (c == null)
262             Exceptions.nullArgument("collection");
263         return super.retainAll(c);
264     }
265
266     /**
267      * Sets a specified element to a new value. The corresponding
268      * element of the underlying list is set to the unwrapped value.
269      *
270      * @param index
271      * the index of the element whose value to set.
272      *
273      * @param o
274      * the new value of the specified element.
275      *
276      * @return the previous value of the element.
277      *
278      * @throws UnsupportedOperationException
279      * if the operation is not supported by the underlying
280      * list.
281      *
282      * @throws IndexOutOfBoundsException
283      * if <tt>index</tt> does not denote a valid
284      * element position (valid: <tt>0 - size()-1</tt>).
285      *
286      * @throws IllegalArgumentException
287      * if <tt>o</tt> is <tt>null</tt>.
288      *
289      * @throws ClassCastException
290      * if <tt>o</tt> is not of class {@link Byte Byte}.
291      */

292     public Object JavaDoc set(int index, Object JavaDoc o) {
293         if (o == null)
294             Exceptions.nullElementNotAllowed();
295         return new Byte JavaDoc(list.set(index, ((Byte JavaDoc)o).byteValue()));
296     }
297
298     /**
299      * Returns the number of elements in this list. The
300      * number of elements is the same as that of the underlying
301      * list.
302      *
303      * @return the number of elements in this list.
304      */

305     public int size()
306     { return list.size(); }
307
308
309 }
Popular Tags