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.list.IntList;
022: import bak.pcj.list.IntListIterator;
023: import bak.pcj.util.Exceptions;
024:
025: import java.util.AbstractList;
026: import java.util.ListIterator;
027: import java.util.Collection;
028: import java.util.AbstractCollection;
029:
030: /**
031: * This class represents adapters of int lists to Java Collection Framework lists.
032: * The adapter is implemented as a wrapper around a primitive list. Thus,
033: * changes to the underlying list are reflected by this
034: * list and vice versa.
035: *
036: * @see bak.pcj.list.IntList
037: * @see java.util.List
038: *
039: * @author Søren Bak
040: * @version 1.2 20-08-2003 22:58
041: * @since 1.0
042: */
043: public class IntListToListAdapter extends AbstractList {
044:
045: /** The underlying primitive list. */
046: protected IntList list;
047:
048: /**
049: * Creates a new adaption of a collection of int
050: * values to a Java Collections Framework collection.
051: *
052: * @param list
053: * the underlying primitive collection.
054: *
055: * @throws NullPointerException
056: * if <tt>collection</tt> is <tt>null</tt>.
057: */
058: public IntListToListAdapter(IntList list) {
059: if (list == null)
060: Exceptions.nullArgument("list");
061: this .list = list;
062: }
063:
064: /**
065: * Adds all the elements of a specified collection to
066: * this list starting at a specified index. The elements are
067: * inserted in the specified collection's iteration order.
068: * All elements from the specified index and forward are pushed
069: * to their successors' indices (<tt>c.size()</tt> indices).
070: * All elements are added to the underlying list.
071: * <p>This method is only overridden to work
072: * around a bug in {@link AbstractList AbstractList},
073: * which does not throw a
074: * {@link NullPointerException NullPointerException} when the
075: * argument is <tt>null</tt> and the list is empty.
076: *
077: * @param index
078: * the index at which to insert the elements of
079: * the specified collection. If
080: * <tt>index == size()</tt> the elements are appended
081: * to this list.
082: *
083: * @param c
084: * the collection whose elements to add to this
085: * list.
086: *
087: * @return <tt>true</tt> if this list was modified
088: * as a result of adding the elements of <tt>c</tt>;
089: * returns <tt>false</tt> otherwise.
090: *
091: * @throws UnsupportedOperationException
092: * if the operation is not supported by the
093: * underlying list.
094: *
095: * @throws NullPointerException
096: * if <tt>c</tt> is <tt>null</tt>.
097: *
098: * @throws IndexOutOfBoundsException
099: * 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 Integer Integer}.
108: */
109: public boolean addAll(int index, Collection 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 Integer Integer}.
142: */
143: public void add(int index, Object o) {
144: if (o == null)
145: Exceptions.nullElementNotAllowed();
146: list.add(index, ((Integer) o).intValue());
147: }
148:
149: /**
150: * Returns the element at a specified position in this list.
151: * The returned value will always be of class {@link Integer Integer}.
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 get(int index) {
163: return new Integer(list.get(index));
164: }
165:
166: /**
167: * Returns a list iterator over this list, starting from a
168: * specified index.
169: *
170: * @param index
171: * the index at which to begin the iteration.
172: *
173: * @return a list iterator over this list.
174: *
175: * @throws IndexOutOfBoundsException
176: * if <tt>index</tt> does not denote a valid
177: * iteration position (valid: <tt>0 - size()</tt>).
178: */
179: public ListIterator listIterator(int index) {
180: return new IntListIteratorToListIteratorAdapter(list
181: .listIterator(index));
182: }
183:
184: /**
185: * Removes the element at a specified index in this list. All
186: * elements following the removed element are pushed to their
187: * predecessor's indices. The element is removed from the
188: * underlying collection.
189: *
190: * @param index
191: * the index of the element to remove.
192: *
193: * @throws UnsupportedOperationException
194: * if the operation is not supported by the underlying
195: * list.
196: *
197: * @throws IndexOutOfBoundsException
198: * if <tt>index</tt> does not denote a valid
199: * element position (valid: <tt>0 - size()-1</tt>).
200: */
201: public Object remove(int index) {
202: return new Integer(list.removeElementAt(index));
203: }
204:
205: /**
206: * Removes all the elements of a specified collection from
207: * this list. The elements are removed from the
208: * underlying list.
209: * <p>This method is only overridden to work
210: * around a bug in {@link AbstractList AbstractList},
211: * which does not throw a
212: * {@link NullPointerException NullPointerException} when the
213: * argument is <tt>null</tt> and the list is empty. The
214: * bug is inherited from {@link AbstractCollection AbstractCollection}.
215: *
216: * @param c
217: * the collection whose elements to remove from this
218: * collection.
219: *
220: * @return <tt>true</tt> if this list was modified
221: * as a result of removing the elements of <tt>c</tt>;
222: * returns <tt>false</tt> otherwise.
223: *
224: * @throws UnsupportedOperationException
225: * if the operation is not supported by the underlying
226: * collection.
227: *
228: * @throws NullPointerException
229: * if <tt>c</tt> is <tt>null</tt>.
230: */
231: public boolean removeAll(Collection c) {
232: if (c == null)
233: Exceptions.nullArgument("collection");
234: return super .removeAll(c);
235: }
236:
237: /**
238: * Retains only the elements of a specified collection in
239: * this list. The elements are removed from the
240: * underlying list.
241: * <p>This method is only overridden to work
242: * around a bug in {@link AbstractList AbstractList},
243: * which does not throw a
244: * {@link NullPointerException NullPointerException} when the
245: * argument is <tt>null</tt> and the list is empty. The
246: * bug is inherited from {@link AbstractCollection AbstractCollection}.
247: *
248: * @param c
249: * the collection whose elements to retain in this
250: * collection.
251: *
252: * @return <tt>true</tt> if this list was modified
253: * as a result of removing the elements not contained
254: * in <tt>c</tt>;
255: * returns <tt>false</tt> otherwise.
256: *
257: * @throws UnsupportedOperationException
258: * if the operation is not supported by the underlying
259: * list.
260: *
261: * @throws NullPointerException
262: * if <tt>c</tt> is <tt>null</tt>.
263: */
264: public boolean retainAll(Collection c) {
265: if (c == null)
266: Exceptions.nullArgument("collection");
267: return super .retainAll(c);
268: }
269:
270: /**
271: * Sets a specified element to a new value. The corresponding
272: * element of the underlying list is set to the unwrapped value.
273: *
274: * @param index
275: * the index of the element whose value to set.
276: *
277: * @param o
278: * the new value of the specified element.
279: *
280: * @return the previous value of the element.
281: *
282: * @throws UnsupportedOperationException
283: * if the operation is not supported by the underlying
284: * list.
285: *
286: * @throws IndexOutOfBoundsException
287: * if <tt>index</tt> does not denote a valid
288: * element position (valid: <tt>0 - size()-1</tt>).
289: *
290: * @throws IllegalArgumentException
291: * if <tt>o</tt> is <tt>null</tt>.
292: *
293: * @throws ClassCastException
294: * if <tt>o</tt> is not of class {@link Integer Integer}.
295: */
296: public Object set(int index, Object o) {
297: if (o == null)
298: Exceptions.nullElementNotAllowed();
299: return new Integer(list.set(index, ((Integer) o).intValue()));
300: }
301:
302: /**
303: * Returns the number of elements in this list. The
304: * number of elements is the same as that of the underlying
305: * list.
306: *
307: * @return the number of elements in this list.
308: */
309: public int size() {
310: return list.size();
311: }
312:
313: }
|