001: /*
002: * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
020: * IN THE SOFTWARE.
021: */
022:
023: package org.jibx.runtime.impl;
024:
025: import java.util.Iterator;
026: import java.util.NoSuchElementException;
027:
028: /**
029: * Iterator class for sparse values in an array. This type of iterator
030: * can be used for an object array which has references interspersed with
031: * <code>null</code>s.
032: *
033: * @author Dennis M. Sosnoski
034: * @version 1.1
035: */
036: public class SparseArrayIterator implements Iterator {
037: /** Array supplying values for iteration. */
038: protected Object[] m_array;
039:
040: /** Offset of next iteration value. */
041: protected int m_offset;
042:
043: /**
044: * Internal constructor.
045: *
046: * @param array array containing values to be iterated
047: */
048: private SparseArrayIterator(Object[] array) {
049: m_array = array;
050: m_offset = -1;
051: advance();
052: }
053:
054: /**
055: * Advance to next iteration value. This advances the current position in
056: * the array to the next non-<code>null</code> value.
057: *
058: * @return <code>true</code> if element available, <code>false</code> if
059: * not
060: */
061: protected boolean advance() {
062: while (++m_offset < m_array.length) {
063: if (m_array[m_offset] != null) {
064: return true;
065: }
066: }
067: return false;
068: }
069:
070: /**
071: * Check for iteration element available.
072: *
073: * @return <code>true</code> if element available, <code>false</code> if
074: * not
075: */
076: public boolean hasNext() {
077: return m_offset < m_array.length;
078: }
079:
080: /**
081: * Get next iteration element.
082: *
083: * @return next iteration element
084: * @exception NoSuchElementException if past end of iteration
085: */
086: public Object next() {
087: if (m_offset < m_array.length) {
088: Object result = m_array[m_offset];
089: advance();
090: return result;
091: } else {
092: throw new NoSuchElementException();
093: }
094: }
095:
096: /**
097: * Remove element from iteration. This optional operation is not supported
098: * and always throws an exception.
099: *
100: * @exception UnsupportedOperationException for unsupported operation
101: */
102: public void remove() {
103: throw new UnsupportedOperationException();
104: }
105:
106: /**
107: * Build iterator.
108: *
109: * @param array array containing values to be iterated (may be
110: * <code>null</code>)
111: * @return constructed iterator
112: */
113: public static Iterator buildIterator(Object[] array) {
114: if (array == null || array.length == 0) {
115: return ArrayRangeIterator.EMPTY_ITERATOR;
116: } else {
117: return new SparseArrayIterator(array);
118: }
119: }
120: }
|