KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002, 2003 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.ByteListIterator;
22 import bak.pcj.util.Exceptions;
23 import java.util.ListIterator JavaDoc;
24
25 /**
26  * This class represents adaptions of Java Collections Framework
27  * list iterators to primitive list iterators over byte values.
28  *
29  * <p>
30  * Adapters from JCF collections to primitive collections will
31  * fail if the JCF collection contains <tt>null</tt> values or
32  * values of the wrong class. However, adapters are not fast
33  * failing in the case that the underlying collection should
34  * contain illegal values. To implement fast failure would require
35  * every operation to check every element of the underlying
36  * collection before doing anything. Instead validation methods
37  * are provided. They can be called using the assertion facility
38  * in the client code:
39  * <pre>
40  * CollectionToByteCollectionAdapter s;
41  * ...
42  * <b>assert</b> s.validate();
43  * </pre>
44  * or by letting the adapter throw an exception on illegal values:
45  * <pre>
46  * CollectionToByteCollectionAdapter s;
47  * ...
48  * s.evalidate(); // Throws an exception on illegal values
49  * </pre>
50  * Either way, validation must be invoked directly by the client
51  * code.
52  *
53  * @author S&oslash;ren Bak
54  * @version 1.2 20-08-2003 23:17
55  * @since 1.0
56  */

57 public class ListIteratorToByteListIteratorAdapter implements ByteListIterator {
58
59     /** The underlying iterator. */
60     protected ListIterator iterator;
61
62     /**
63      * Creates a new adaption of a list iterator to a primitive
64      * list iterator over byte values.
65      *
66      * @param iterator
67      * the iterator to adapt to a primitive iterator.
68      *
69      * @throws NullPointerException
70      * if <tt>iterator</tt> is <tt>null</tt>.
71      */

72     public ListIteratorToByteListIteratorAdapter(ListIterator iterator) {
73         if (iterator == null)
74             Exceptions.nullArgument("iterator");
75         this.iterator = iterator;
76     }
77
78     public void add(byte v)
79     { iterator.add(new Byte JavaDoc(v)); }
80
81     public boolean hasNext()
82     { return iterator.hasNext(); }
83
84     public boolean hasPrevious()
85     { return iterator.hasPrevious(); }
86
87     public byte next()
88     { return ((Byte JavaDoc)iterator.next()).byteValue(); }
89
90     public int nextIndex()
91     { return iterator.nextIndex(); }
92
93     public byte previous()
94     { return ((Byte JavaDoc)iterator.previous()).byteValue(); }
95
96     public int previousIndex()
97     { return iterator.previousIndex(); }
98
99     public void remove()
100     { iterator.remove(); }
101
102     public void set(byte v)
103     { iterator.set(new Byte JavaDoc(v)); }
104
105 }
Popular Tags