KickJava   Java API By Example, From Geeks To Geeks.

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


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 an array range. This type of iterator
30  * can be used for any contiguous range of items in an object array.
31  *
32  * @author Dennis M. Sosnoski
33  * @version 1.1
34  */

35
36 public class ArrayRangeIterator implements Iterator JavaDoc
37 {
38     /** Empty iterator used whenever possible. */
39     public static final ArrayRangeIterator EMPTY_ITERATOR =
40         new ArrayRangeIterator(null, 0, 0);
41
42     /** Array supplying values for iteration. */
43     protected Object JavaDoc[] m_array;
44
45     /** Offset of next iteration value. */
46     protected int m_offset;
47
48     /** Ending offset for values. */
49     protected int m_limit;
50
51     /**
52      * Internal constructor.
53      *
54      * @param array array containing values to be iterated
55      * @param start starting offset in array
56      * @param limit offset past end of values
57      */

58
59     private ArrayRangeIterator(Object JavaDoc[] array, int start, int limit) {
60         m_array = array;
61         m_offset = start;
62         m_limit = limit;
63     }
64
65     /**
66      * Check for iteration element available.
67      *
68      * @return <code>true</code> if element available, <code>false</code> if
69      * not
70      */

71
72     public boolean hasNext() {
73         return m_offset < m_limit;
74     }
75
76     /**
77      * Get next iteration element.
78      *
79      * @return next iteration element
80      * @exception NoSuchElementException if past end of iteration
81      */

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

97
98     public void remove() {
99         throw new UnsupportedOperationException JavaDoc();
100     }
101
102     /**
103      * Build iterator.
104      *
105      * @param array array containing values to be iterated (may be
106      * <code>null</code>)
107      * @param start starting offset in array
108      * @param limit offset past end of values
109      * @return constructed iterator
110      */

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