001: /*
002: * $Id: ReverseSortedRowIterator.java,v 1.1 2005/03/31 00:07:06 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 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 java.util.NoSuchElementException;
044:
045: import org.axiondb.AxionException;
046: import org.axiondb.Row;
047: import org.axiondb.RowIterator;
048:
049: /**
050: * Reverse a SortedRowIterator.
051: *
052: * @version $Revision: 1.1 $ $Date: 2005/03/31 00:07:06 $
053: * @author Ahimanikya Satapathy
054: */
055: public class ReverseSortedRowIterator extends BaseRowIterator {
056:
057: public ReverseSortedRowIterator(RowIterator sortedIterator)
058: throws AxionException {
059: _delegate = sortedIterator;
060: reset();
061: }
062:
063: /** Not supported in the base implementation. */
064: public void add(Row row) throws AxionException {
065: throw new UnsupportedOperationException();
066: }
067:
068: public Row current() {
069: if (!hasCurrent()) {
070: throw new NoSuchElementException("No Current Row");
071: }
072: return getDelegate().current();
073: }
074:
075: public int currentIndex() {
076: return _currentIndex;
077: }
078:
079: public Row first() throws AxionException {
080: reset();
081: return peekNext();
082: }
083:
084: public boolean hasCurrent() {
085: if (_currentIndex == -1) {
086: return false;
087: }
088: return getDelegate().hasCurrent();
089: }
090:
091: public boolean hasNext() {
092: return getDelegate().hasPrevious();
093: }
094:
095: public boolean hasPrevious() {
096: return getDelegate().hasNext();
097: }
098:
099: public boolean isEmpty() {
100: return getDelegate().isEmpty();
101: }
102:
103: public Row last() throws AxionException {
104: if (!hasNext()) {
105: previous();
106: }
107: Row row = null;
108: while (hasNext()) {
109: row = next();
110: }
111: return row;
112: }
113:
114: public Row next() throws AxionException {
115: _currentIndex = _nextIndex;
116: _nextIndex++;
117: return getDelegate().previous();
118: }
119:
120: public int nextIndex() {
121: return _nextIndex;
122: }
123:
124: public Row peekNext() throws AxionException {
125: next();
126: return previous();
127: }
128:
129: public Row peekPrevious() throws AxionException {
130: previous();
131: return next();
132: }
133:
134: public Row previous() throws AxionException {
135: _nextIndex--;
136: _currentIndex = _nextIndex;
137: return getDelegate().next();
138: }
139:
140: public int previousIndex() {
141: return _nextIndex - 1;
142: }
143:
144: public void remove() throws AxionException {
145: getDelegate().remove();
146: _currentIndex = -1;
147: _nextIndex--;
148: }
149:
150: public void reset() throws AxionException {
151: getDelegate().reset();
152: _currentIndex = -1;
153: _nextIndex = 0;
154: while (getDelegate().hasNext()) {
155: getDelegate().next();
156: }
157: }
158:
159: public void set(Row row) throws AxionException {
160: getDelegate().set(row);
161: }
162:
163: public String toString() {
164: return "ReverseSorted(" + getDelegate() + ")";
165: }
166:
167: private RowIterator getDelegate() {
168: return _delegate;
169: }
170:
171: private int _currentIndex = -1;
172: private int _nextIndex = 0;
173: private RowIterator _delegate = null;
174:
175: }
|