001: /*
002: * $Id: ChainedRowIterator.java,v 1.14 2005/12/20 18:32:41 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 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: package org.axiondb.engine.rowiterators;
041:
042: import java.util.ArrayList;
043: import java.util.NoSuchElementException;
044:
045: import org.axiondb.AxionException;
046: import org.axiondb.Row;
047: import org.axiondb.RowIterator;
048:
049: /**
050: * Chains together one or more {@link RowIterator}s to make them look like one (similiar
051: * to a SQL UNION).
052: *
053: * @version $Revision: 1.14 $ $Date: 2005/12/20 18:32:41 $
054: * @author Rodney Waldhoff
055: * @author Ahimanikya Satapathy
056: */
057: public class ChainedRowIterator extends BaseRowIterator implements
058: RowIterator {
059:
060: public ChainedRowIterator() {
061: }
062:
063: public void add(Row row) throws AxionException {
064: getCurrentRowIterator().add(row);
065: }
066:
067: public void addRowIterator(RowIterator iter) {
068: _iterators.add(iter);
069: }
070:
071: public Row current() {
072: if (_currentRowSet) {
073: return _currentRow;
074: }
075: throw new NoSuchElementException("No current row.");
076: }
077:
078: public int currentIndex() {
079: return _currentIndex;
080: }
081:
082: public boolean hasCurrent() {
083: return _currentRowSet;
084: }
085:
086: public boolean hasNext() {
087: for (int i = _currentIterator, I = _iterators.size(); i < I; i++) {
088: RowIterator iter = (RowIterator) _iterators.get(i);
089: if (iter.hasNext()) {
090: return true;
091: }
092: }
093: return false;
094: }
095:
096: public boolean hasPrevious() {
097: for (int i = _currentIterator; i >= 0; i--) {
098: RowIterator iter = (RowIterator) _iterators.get(i);
099: if (iter.hasPrevious()) {
100: return true;
101: }
102: }
103: return false;
104: }
105:
106: public Row last() throws AxionException {
107: _currentIndex = -1;
108: _nextIndex = 0;
109: for (int i = 0, I = _iterators.size(); i < I; i++) {
110: RowIterator iter = (RowIterator) _iterators.get(i);
111: if (!iter.isEmpty()) {
112: _currentRow = iter.last();
113: _nextIndex += iter.nextIndex();
114: _currentRowSet = true;
115: }
116: }
117: _currentIndex = _nextIndex - 1;
118: return _currentRow;
119: }
120:
121: public Row next() throws AxionException {
122: for (; _currentIterator < _iterators.size(); _currentIterator++) {
123: RowIterator iter = getCurrentRowIterator();
124: if (iter.hasNext()) {
125: _currentRow = iter.next();
126: _currentRowSet = true;
127: _currentIndex = _nextIndex;
128: _nextIndex++;
129: return _currentRow;
130: }
131: }
132: throw new NoSuchElementException("No next row.");
133: }
134:
135: public int next(int count) throws AxionException {
136: int lastId = -1;
137: for (int i = 0; i < count; i++) {
138: int iterSize = _iterators.size();
139: for (; _currentIterator < iterSize; _currentIterator++) {
140: RowIterator iter = getCurrentRowIterator();
141: if (iter.hasNext()) {
142: lastId = iter.next(1);
143: _currentIndex = _nextIndex++;
144: break;
145: }
146: }
147: if (_currentIterator == iterSize) {
148: _currentIterator--;
149: throw new NoSuchElementException("No next row.");
150: }
151: }
152: _currentRow = null;
153: _currentRowSet = false;
154: return lastId;
155: }
156:
157: public int nextIndex() {
158: return _nextIndex;
159: }
160:
161: public Row previous() throws AxionException {
162: for (; _currentIterator >= 0; _currentIterator--) {
163: RowIterator iter = getCurrentRowIterator();
164: if (iter.hasPrevious()) {
165: _currentRow = iter.previous();
166: _currentRowSet = true;
167: _nextIndex--;
168: _currentIndex = _nextIndex;
169: return _currentRow;
170: }
171: }
172: throw new NoSuchElementException("No previous row.");
173: }
174:
175: public int previous(int count) throws AxionException {
176: int lastId = -1;
177: for (int i = 0; i < count; i++) {
178: for (; _currentIterator >= 0; _currentIterator--) {
179: RowIterator iter = getCurrentRowIterator();
180: if (iter.hasPrevious()) {
181: lastId = iter.previous(1);
182: _currentIndex = --_nextIndex;
183: break;
184: }
185: }
186:
187: if (_currentIterator == -1) {
188: _currentIterator++;
189: throw new NoSuchElementException("No previous row.");
190: }
191: }
192: _currentRow = null;
193: _currentRowSet = false;
194: return lastId;
195: }
196:
197: public int previousIndex() {
198: return _nextIndex - 1;
199: }
200:
201: public void remove() throws AxionException {
202: if (!hasCurrent()) {
203: throw new IllegalStateException("No current row.");
204: }
205: getCurrentRowIterator().remove();
206: _nextIndex--;
207: _currentRow = null;
208: _currentRowSet = false;
209: _currentIndex = -1;
210: }
211:
212: public void reset() throws AxionException {
213: for (int i = 0, I = _iterators.size(); i < I; i++) {
214: ((RowIterator) (_iterators.get(i))).reset();
215: }
216: _currentIndex = -1;
217: _nextIndex = 0;
218: _currentRowSet = false;
219: _currentRow = null;
220: _currentIterator = 0;
221: }
222:
223: public void set(Row row) throws AxionException {
224: if (!hasCurrent()) {
225: throw new IllegalStateException("No current row.");
226: }
227: getCurrentRowIterator().set(row);
228: _currentRow = row;
229: }
230:
231: public int size() throws AxionException {
232: int size = 0;
233: for (int i = 0, I = _iterators.size(); i < I; i++) {
234: size += ((RowIterator) (_iterators.get(i))).size();
235: }
236: return size;
237: }
238:
239: public String toString() {
240: return "Chained(" + _iterators + ")";
241: }
242:
243: private RowIterator getCurrentRowIterator() {
244: return (RowIterator) _iterators.get(_currentIterator);
245: }
246:
247: private int _currentIndex = -1;
248: private int _currentIterator = 0;
249: private Row _currentRow;
250: private boolean _currentRowSet = false;
251:
252: private ArrayList _iterators = new ArrayList(4);
253: private int _nextIndex = 0;
254: }
|