001: /*
002: * $Id: ListRowIterator.java,v 1.2 2005/04/22 02:28:53 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.List;
044: import java.util.NoSuchElementException;
045:
046: import org.axiondb.Row;
047: import org.axiondb.RowIterator;
048:
049: /**
050: * A {@link RowIterator}that for a given {@link java.util.List}.
051: *
052: * @version $Revision: 1.2 $ $Date: 2005/04/22 02:28:53 $
053: * @author Ahimanikya Satapathy
054: */
055: public class ListRowIterator extends BaseRowIterator {
056:
057: public ListRowIterator(List list) {
058: _list = list;
059: }
060:
061: public Row current() {
062: if (!hasCurrent()) {
063: throw new NoSuchElementException("No current row.");
064: }
065: return _current;
066: }
067:
068: public int currentIndex() {
069: return _currentIndex;
070: }
071:
072: public boolean hasCurrent() {
073: return (null != _current);
074: }
075:
076: public boolean hasNext() {
077: return nextIndex() < _list.size();
078: }
079:
080: public boolean hasPrevious() {
081: return nextIndex() > 0;
082: }
083:
084: public Row last() {
085: _nextIndex = _list.size();
086: _nextId = _list.size();
087: previous();
088:
089: _nextIndex++;
090: _nextId++;
091: return current();
092: }
093:
094: public Row next() {
095: if (!hasNext()) {
096: throw new NoSuchElementException("No next row");
097: }
098: _currentId = _nextId++;
099: _current = (Row) (_list.get(_currentId));
100: _currentIndex = _nextIndex;
101: _nextIndex++;
102: return _current;
103: }
104:
105: public int nextIndex() {
106: return _nextIndex;
107: }
108:
109: public Row previous() {
110: if (!hasPrevious()) {
111: throw new NoSuchElementException("No previous row");
112: }
113:
114: _currentId = (--_nextId);
115: _current = (Row) (_list.get(_currentId));
116: _nextIndex--;
117: _currentIndex = _nextIndex;
118: return _current;
119: }
120:
121: public int previousIndex() {
122: return _nextIndex - 1;
123: }
124:
125: public void reset() {
126: _current = null;
127: _nextIndex = 0;
128: _currentIndex = -1;
129: _nextId = 0;
130: }
131:
132: public int size() {
133: return _list.size();
134: }
135:
136: private Row _current = null;
137: private int _currentId = -1;
138: private int _currentIndex = -1;
139: private List _list = null;
140: private int _nextId = 0;
141: private int _nextIndex = 0;
142: }
|