001: /*
002: * $Id: RowViewRowIterator.java,v 1.15 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 java.util.List;
044: import java.util.Map;
045: import java.util.NoSuchElementException;
046:
047: import org.axiondb.AxionException;
048: import org.axiondb.Row;
049: import org.axiondb.RowDecorator;
050: import org.axiondb.RowIterator;
051: import org.axiondb.Selectable;
052: import org.axiondb.engine.commands.SelectCommand;
053: import org.axiondb.engine.rows.RowView;
054: import org.axiondb.functions.ConcreteFunction;
055:
056: /**
057: * A {@link org.axiondb.RowIterator}that creates {@link RowView}s based upon selected
058: * {@link Row}identifiers.
059: *
060: * @version $Revision: 1.15 $ $Date: 2005/12/20 18:32:41 $
061: * @author Ahimanikya Satapathy
062: */
063: public class RowViewRowIterator extends BaseRowIterator {
064:
065: public RowViewRowIterator(RowIterator rowIter, Map colIdToFieldMap,
066: List selected) {
067: _rowIter = rowIter;
068: _selected = selected;
069: _colIdToFieldMap = colIdToFieldMap;
070: _selectedColIndex = buildSelectedColumnIndex();
071: _decorator = new RowDecorator(colIdToFieldMap);
072: }
073:
074: public Row current() {
075: if (hasCurrent()) {
076: return _currentRow;
077: }
078: throw new NoSuchElementException("No current row has been set.");
079: }
080:
081: public int currentIndex() {
082: return _currentIndex;
083: }
084:
085: public boolean hasCurrent() {
086: return (null != _currentRow);
087: }
088:
089: public boolean hasNext() {
090: return _rowIter.hasNext();
091: }
092:
093: public boolean hasPrevious() {
094: return _rowIter.hasPrevious();
095: }
096:
097: public Row next() throws AxionException {
098: _currentIndex = _rowIter.nextIndex();
099: setCurrentRow(_rowIter.next());
100: return _currentRow;
101: }
102:
103: public int nextIndex() {
104: return _rowIter.nextIndex();
105: }
106:
107: public Row previous() throws AxionException {
108: setCurrentRow(_rowIter.previous());
109: _currentIndex = _rowIter.nextIndex();
110: return _currentRow;
111: }
112:
113: public int previousIndex() {
114: return _rowIter.previousIndex();
115: }
116:
117: public void reset() throws AxionException {
118: _currentRow = null;
119: _currentIndex = -1;
120: _rowIter.reset();
121: }
122:
123: public String toString() {
124: return "View(" + _rowIter + ")";
125: }
126:
127: private int[] buildSelectedColumnIndex() {
128: int[] selectedIds = new int[_selected.size()];
129: for (int i = 0, I = _selected.size(); i < I; i++) {
130: Object col = _selected.get(i);
131: if (col instanceof ConcreteFunction
132: || col instanceof SelectCommand) {
133: // give -ve index value to indicate RowView that
134: // column in this index is a function
135: selectedIds[i] = -1;
136: } else { // col is a Literal or ColumnIdentifier
137: selectedIds[i] = ((Integer) _colIdToFieldMap.get(col))
138: .intValue();
139: }
140: }
141: return selectedIds;
142: }
143:
144: private final void setCurrentRow(Row row) throws AxionException {
145: _currentRow = new RowView(row, row.getIdentifier(),
146: _selectedColIndex);
147: _decorator.setRow(currentIndex(), row);
148: for (int i = 0, I = _currentRow.size(); i < I; i++) {
149: Selectable sel = (Selectable) _selected.get(i);
150: _currentRow.set(i, sel.evaluate(_decorator));
151: }
152: }
153:
154: private Map _colIdToFieldMap;
155: private int _currentIndex = -1;
156: private RowView _currentRow;
157: private RowDecorator _decorator;
158:
159: private RowIterator _rowIter;
160: private List _selected;
161: private int[] _selectedColIndex;
162: }
|