KickJava   Java API By Example, From Geeks To Geeks.

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


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 sparse values in an array. This type of iterator
30  * can be used for an object array which has references interspersed with
31  * <code>null</code>s.
32  *
33  * @author Dennis M. Sosnoski
34  * @version 1.1
35  */

36
37 public class SparseArrayIterator 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     /**
46      * Internal constructor.
47      *
48      * @param array array containing values to be iterated
49      */

50
51     private SparseArrayIterator(Object JavaDoc[] array) {
52         m_array = array;
53         m_offset = -1;
54         advance();
55     }
56
57     /**
58      * Advance to next iteration value. This advances the current position in
59      * the array to the next non-<code>null</code> value.
60      *
61      * @return <code>true</code> if element available, <code>false</code> if
62      * not
63      */

64
65     protected boolean advance() {
66         while (++m_offset < m_array.length) {
67             if (m_array[m_offset] != null) {
68                 return true;
69             }
70         }
71         return false;
72     }
73
74     /**
75      * Check for iteration element available.
76      *
77      * @return <code>true</code> if element available, <code>false</code> if
78      * not
79      */

80
81     public boolean hasNext() {
82         return m_offset < m_array.length;
83     }
84
85     /**
86      * Get next iteration element.
87      *
88      * @return next iteration element
89      * @exception NoSuchElementException if past end of iteration
90      */

91
92     public Object JavaDoc next() {
93         if (m_offset < m_array.length) {
94             Object JavaDoc result = m_array[m_offset];
95             advance();
96             return result;
97         } else {
98             throw new NoSuchElementException JavaDoc();
99         }
100     }
101
102     /**
103      * Remove element from iteration. This optional operation is not supported
104      * and always throws an exception.
105      *
106      * @exception UnsupportedOperationException for unsupported operation
107      */

108
109     public void remove() {
110         throw new UnsupportedOperationException JavaDoc();
111     }
112
113     /**
114      * Build iterator.
115      *
116      * @param array array containing values to be iterated (may be
117      * <code>null</code>)
118      * @return constructed iterator
119      */

120
121     public static Iterator JavaDoc buildIterator(Object JavaDoc[] array) {
122         if (array == null || array.length == 0) {
123             return ArrayRangeIterator.EMPTY_ITERATOR;
124         } else {
125             return new SparseArrayIterator(array);
126         }
127     }
128 }
129
Popular Tags