001: /*
002: * $Id: IntListIteratorChain.java,v 1.6 2004/09/09 23:47:45 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.util;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.ListIterator;
046: import java.util.NoSuchElementException;
047:
048: import org.apache.commons.collections.primitives.ArrayIntList;
049: import org.apache.commons.collections.primitives.IntList;
050: import org.apache.commons.collections.primitives.IntListIterator;
051:
052: /**
053: * Concatenates multiple {@link IntListIterator}s into
054: * a single {@link IntListIterator}.
055: * <p/>
056: * @version $Revision: 1.6 $ $Date: 2004/09/09 23:47:45 $
057: * @author Chuck Burdick
058: * @author Rodney Waldhoff
059: */
060: public class IntListIteratorChain implements IntListIterator {
061: public IntListIteratorChain() {
062: _listOfIterators = new ArrayList();
063: }
064:
065: public void addIterator(IntListIterator iter) {
066: assertNotStarted();
067: addTempListIfNeeded();
068: _listOfIterators.add(iter);
069: }
070:
071: public void addIterator(int value) {
072: assertNotStarted();
073: addToTempList(value);
074: }
075:
076: public boolean hasNext() {
077: ensureStarted();
078: if (_currentIterator != null && _currentIterator.hasNext()) {
079: return true;
080: } else if (_iteratorOverIterators.hasNext()) {
081: _currentIterator = (IntListIterator) _iteratorOverIterators
082: .next();
083: return hasNext();
084: } else {
085: return false;
086: }
087: }
088:
089: public boolean hasPrevious() {
090: ensureStarted();
091: if (_currentIterator != null && _currentIterator.hasPrevious()) {
092: return true;
093: } else if (_iteratorOverIterators.hasPrevious()) {
094: _currentIterator = (IntListIterator) _iteratorOverIterators
095: .previous();
096: return hasPrevious();
097: } else {
098: return false;
099: }
100: }
101:
102: public int next() {
103: if (hasNext()) {
104: _nextIndex++;
105: return _currentIterator.next();
106: }
107: throw new NoSuchElementException();
108: }
109:
110: public int previous() {
111: if (hasPrevious()) {
112: _nextIndex--;
113: return _currentIterator.previous();
114: }
115: throw new NoSuchElementException();
116: }
117:
118: public void add(int elt) {
119: throw new UnsupportedOperationException();
120: }
121:
122: public int nextIndex() {
123: return _nextIndex;
124: }
125:
126: public int previousIndex() {
127: return _nextIndex - 1;
128: }
129:
130: public void remove() {
131: throw new UnsupportedOperationException();
132: }
133:
134: public void set(int elt) {
135: throw new UnsupportedOperationException();
136: }
137:
138: private void assertNotStarted() throws IllegalStateException {
139: if (null != _iteratorOverIterators) {
140: throw new IllegalStateException("Already started iterating");
141: }
142: }
143:
144: private void ensureStarted() {
145: if (null == _iteratorOverIterators) {
146: addTempListIfNeeded();
147: _iteratorOverIterators = _listOfIterators.listIterator();
148: }
149: }
150:
151: private final void addTempListIfNeeded() {
152: if (null != _tempList) {
153: _listOfIterators.add(_tempList.listIterator());
154: _tempList = null;
155: } else if (_tempValueSet) {
156: _listOfIterators.add(new SingleElementIntListIterator(
157: _tempValue));
158: _tempValueSet = false;
159: }
160: }
161:
162: private final void addToTempList(int value) {
163: if (null == _tempList) {
164: if (!_tempValueSet) {
165: _tempValue = value;
166: _tempValueSet = true;
167: } else {
168: _tempList = new ArrayIntList(2);
169: _tempList.add(_tempValue);
170: _tempList.add(value);
171: _tempValueSet = false;
172: }
173: } else {
174: _tempList.add(value);
175: }
176: }
177:
178: private List _listOfIterators = null;
179: private IntList _tempList = null;
180: private boolean _tempValueSet = false;
181: private int _tempValue;
182: private IntListIterator _currentIterator = null;
183: private ListIterator _iteratorOverIterators = null;
184: private int _nextIndex = 0;
185:
186: private final class SingleElementIntListIterator implements
187: IntListIterator {
188:
189: public SingleElementIntListIterator(int value) {
190: _value = value;
191: }
192:
193: public boolean hasNext() {
194: return _before;
195: }
196:
197: public boolean hasPrevious() {
198: return !_before;
199: }
200:
201: public int next() {
202: if (_before) {
203: _before = false;
204: return _value;
205: }
206: throw new NoSuchElementException();
207: }
208:
209: public int nextIndex() {
210: return _before ? 0 : 1;
211: }
212:
213: public int previous() {
214: if (!_before) {
215: _before = true;
216: return _value;
217: }
218: throw new NoSuchElementException();
219: }
220:
221: public int previousIndex() {
222: return _before ? -1 : 0;
223: }
224:
225: public void remove() {
226: throw new UnsupportedOperationException();
227: }
228:
229: public void set(int arg0) {
230: throw new UnsupportedOperationException();
231: }
232:
233: public void add(int arg0) {
234: throw new UnsupportedOperationException();
235: }
236:
237: private boolean _before = true;
238: private int _value = 0;
239: }
240: }
|