001: /*
002: Copyright (c) 2000-2005, Dennis M. Sosnoski.
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.runtime.impl;
030:
031: import java.util.Iterator;
032: import java.util.NoSuchElementException;
033:
034: /**
035: * Iterator class for values contained in an array range. This type of iterator
036: * can be used for any contiguous range of items in an object array.
037: *
038: * @author Dennis M. Sosnoski
039: * @version 1.1
040: */
041: public class ArrayRangeIterator implements Iterator {
042: /** Empty iterator used whenever possible. */
043: public static final ArrayRangeIterator EMPTY_ITERATOR = new ArrayRangeIterator(
044: null, 0, 0);
045:
046: /** Array supplying values for iteration. */
047: protected Object[] m_array;
048:
049: /** Offset of next iteration value. */
050: protected int m_offset;
051:
052: /** Ending offset for values. */
053: protected int m_limit;
054:
055: /**
056: * Internal constructor.
057: *
058: * @param array array containing values to be iterated
059: * @param start starting offset in array
060: * @param limit offset past end of values
061: */
062: private ArrayRangeIterator(Object[] array, int start, int limit) {
063: m_array = array;
064: m_offset = start;
065: m_limit = limit;
066: }
067:
068: /**
069: * Check for iteration element available.
070: *
071: * @return <code>true</code> if element available, <code>false</code> if
072: * not
073: */
074: public boolean hasNext() {
075: return m_offset < m_limit;
076: }
077:
078: /**
079: * Get next iteration element.
080: *
081: * @return next iteration element
082: * @exception NoSuchElementException if past end of iteration
083: */
084: public Object next() {
085: if (m_offset < m_limit) {
086: return m_array[m_offset++];
087: } else {
088: throw new NoSuchElementException();
089: }
090: }
091:
092: /**
093: * Remove element from iteration. This optional operation is not supported
094: * and always throws an exception.
095: *
096: * @exception UnsupportedOperationException for unsupported operation
097: */
098: public void remove() {
099: throw new UnsupportedOperationException();
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: public static Iterator buildIterator(Object[] array, int start,
112: int limit) {
113: if (array == null || start >= limit) {
114: return EMPTY_ITERATOR;
115: } else {
116: return new ArrayRangeIterator(array, start, limit);
117: }
118: }
119: }
|