KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sosnoski > util > WrappedArrayIterator


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package com.sosnoski.util;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27
28 /**
29  * Iterator class for values contained in a possibly wrapped array range. This
30  * type of iterator can be used for items in an object array which are
31  * contiguous.modulo the array size.
32  *
33  * @author Dennis M. Sosnoski
34  * @version 1.1
35  */

36
37 public class WrappedArrayIterator implements Iterator JavaDoc
38 {
39     /** Array supplying values for iteration. */
40     protected Object JavaDoc[] m_array;
41
42     /** Offset of next iteration value. */
43     protected int m_offset;
44
45     /** Ending offset for values. */
46     protected int m_last;
47
48     /**
49      * Internal constructor.
50      *
51      * @param array array containing values to be iterated
52      * @param start offset of first value in array
53      * @param end offset of last value in array
54      */

55
56     private WrappedArrayIterator(Object JavaDoc[] array, int start, int end) {
57         m_array = array;
58         m_offset = start;
59         m_last = end;
60     }
61
62     /**
63      * Check for iteration element available.
64      *
65      * @return <code>true</code> if element available, <code>false</code> if
66      * not
67      */

68
69     public boolean hasNext() {
70         return m_offset != m_last;
71     }
72
73     /**
74      * Get next iteration element.
75      *
76      * @return next iteration element
77      * @exception NoSuchElementException if past end of iteration
78      */

79
80     public Object JavaDoc next() {
81         if (m_offset != m_last) {
82             Object JavaDoc result = m_array[m_offset];
83             m_offset = (m_offset+1) % m_array.length;
84             return result;
85         } else {
86             throw new NoSuchElementException JavaDoc();
87         }
88     }
89
90     /**
91      * Remove element from iteration. This optional operation is not supported
92      * and always throws an exception.
93      *
94      * @exception UnsupportedOperationException for unsupported operation
95      */

96
97     public void remove() {
98         throw new UnsupportedOperationException JavaDoc();
99     }
100
101     /**
102      * Build iterator.
103      *
104      * @param array array containing values to be iterated (may be
105      * <code>null</code>)
106      * @param start offset of first value in array
107      * @param end offset of last value in array (the same as start
108      * offset, if the array is empty)
109      * @return constructed iterator
110      */

111
112     public static Iterator JavaDoc buildIterator(Object JavaDoc[] array, int start, int end) {
113         if (array == null || start == end) {
114             return ArrayRangeIterator.EMPTY_ITERATOR;
115         } else {
116             return new WrappedArrayIterator(array, start, end);
117         }
118     }
119 }
120
Popular Tags