001: /*
002: * $Id: ChangingIndexedRowIterator.java,v 1.16 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:
041: package org.axiondb.engine.rowiterators;
042:
043: import org.axiondb.AxionException;
044: import org.axiondb.Function;
045: import org.axiondb.Index;
046: import org.axiondb.Row;
047: import org.axiondb.RowIterator;
048: import org.axiondb.Table;
049:
050: /**
051: * A {@link DelegatingRowIterator}that is wraps a {@link org.axiondb.RowIterator}from
052: * some {@link Index}, and that can be {@link #reset reset}to recreate the iterator for
053: * a new {@link BindVariable bound value}.
054: *
055: * @version $Revision: 1.16 $ $Date: 2005/12/20 18:32:41 $
056: * @author Amrish Lal
057: * @author Ahimanikya Satapathy
058: */
059: public class ChangingIndexedRowIterator implements
060: MutableIndexedRowIterator {
061:
062: public ChangingIndexedRowIterator(Index index, Table table,
063: Function fn) throws AxionException {
064: _index = index;
065: _table = table;
066: _function = fn;
067: reset();
068: }
069:
070: public void add(Row row) throws AxionException {
071: assertIndexSet();
072: _delegate.add(row);
073: }
074:
075: public Row current() {
076: assertIndexSet();
077: return _delegate.current();
078: }
079:
080: public int currentIndex() {
081: assertIndexSet();
082: return _delegate.currentIndex();
083: }
084:
085: public Row first() throws AxionException {
086: assertIndexSet();
087: return _delegate.first();
088: }
089:
090: public boolean hasCurrent() {
091: if (indexSet()) {
092: return _delegate.hasCurrent();
093: }
094: return false;
095: }
096:
097: public boolean hasNext() {
098: if (indexSet()) {
099: return _delegate.hasNext();
100: }
101: return false;
102: }
103:
104: public boolean hasPrevious() {
105: if (indexSet()) {
106: return _delegate.hasPrevious();
107: }
108: return false;
109: }
110:
111: public final boolean indexSet() {
112: return (_delegate != null);
113: }
114:
115: public boolean isEmpty() {
116: return (!hasNext() && !hasPrevious() && !hasCurrent());
117: }
118:
119: public Row last() throws AxionException {
120: assertIndexSet();
121: return _delegate.last();
122: }
123:
124: public Row next() throws AxionException {
125: assertIndexSet();
126: return _delegate.next();
127: }
128:
129: public int next(int count) throws AxionException {
130: assertIndexSet();
131: return _delegate.next(count);
132: }
133:
134: public int nextIndex() {
135: assertIndexSet();
136: return _delegate.nextIndex();
137: }
138:
139: public Row peekNext() throws AxionException {
140: assertIndexSet();
141: return _delegate.peekNext();
142: }
143:
144: public Row peekPrevious() throws AxionException {
145: assertIndexSet();
146: return _delegate.peekPrevious();
147: }
148:
149: public Row previous() throws AxionException {
150: assertIndexSet();
151: return _delegate.previous();
152: }
153:
154: public int previous(int count) throws AxionException {
155: assertIndexSet();
156: return _delegate.previous(count);
157: }
158:
159: public int previousIndex() {
160: assertIndexSet();
161: return _delegate.previousIndex();
162: }
163:
164: public void remove() throws AxionException {
165: assertIndexSet();
166: _delegate.remove();
167: }
168:
169: public void removeIndexKey() throws AxionException {
170: _delegate = null;
171: }
172:
173: public void reset() throws AxionException {
174: if (indexSet()) {
175: _delegate.reset();
176: }
177: }
178:
179: public void set(Row row) throws AxionException {
180: assertIndexSet();
181: _delegate.set(row);
182: }
183:
184: public void setIndexKey(Object value) throws AxionException {
185: _delegate = _index.getRowIterator(_table, _function, value);
186: reset();
187: }
188:
189: public int size() throws AxionException {
190: assertIndexSet();
191: return _delegate.size();
192: }
193:
194: public String toString() {
195: return "ChangingIndexed(index=" + _index.getName() + ";table="
196: + _table.getName() + ";condition=" + _function + ")";
197: }
198:
199: private void assertIndexSet() {
200: if (!indexSet()) {
201: throw new IllegalStateException("Index not set yet");
202: }
203: }
204:
205: private RowIterator _delegate;
206: private Function _function;
207: private Index _index;
208: private Table _table;
209: }
|