KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntIterator;
22 import bak.pcj.util.Exceptions;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * This class represents adaptions of Java Collections Framework
27  * iterators to primitive iterators over int 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  * CollectionToIntCollectionAdapter 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  * CollectionToIntCollectionAdapter 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:18
55  * @since 1.0
56  */

57 public class IteratorToIntIteratorAdapter implements IntIterator {
58
59     /** The underlying iterator. */
60     private Iterator iterator;
61
62     /**
63      * Creates a new adaption to an iterator over int
64      * values.
65      *
66      * @param iterator
67      * the underlying iterator. This iterator must
68      * return values of class
69      * {@link Integer Integer}. Otherwise a
70      * {@link ClassCastException ClassCastException}
71      * will be thrown by
72      * {@link #next() next()}.
73      *
74      * @throws NullPointerException
75      * if <tt>iterator</tt> is <tt>null</tt>.
76      */

77     public IteratorToIntIteratorAdapter(Iterator iterator) {
78         if (iterator == null)
79             Exceptions.nullArgument("iterator");
80         this.iterator = iterator;
81     }
82
83     public boolean hasNext()
84     { return iterator.hasNext(); }
85
86     /**
87      * @throws ClassCastException
88      * if the underlying iterator returns an object
89      * that is not of class
90      * {@link Integer Integer}.
91      */

92     public int next()
93     { return ((Integer JavaDoc)iterator.next()).intValue(); }
94
95     public void remove()
96     { iterator.remove(); }
97
98 }
Popular Tags