001: package org.apache.ojb.broker.accesslayer;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.PersistenceBrokerException;
019: import org.apache.ojb.broker.query.Query;
020:
021: /**
022: * PagingIterator is wrapper around an OJBIterator to support startAt endAt
023: * positions. The PagingIterator returns rows <b>including</b> startAt
024: * and <b>including</b> endAt.
025: *
026: * startAt = 1, endAt = 11 returns rows 1 to 11 if available
027: * if endAt == Query.NO_END_AT_INDEX endAt is set to the last available row
028: *
029: * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
030: * @version $Id: PagingIterator.java,v 1.10.2.2 2005/12/21 22:22:58 tomdz Exp $
031: */
032: public class PagingIterator implements OJBIterator {
033: private final OJBIterator m_iterator;
034: private int m_startAt;
035: private int m_endAt;
036: private int m_rowLimit;
037: private int m_fullSize;
038: private int m_currentCursorPosition; // position of the wrapped iterator
039:
040: /**
041: * Constructor
042: * @param anIterator wrapped Iterator
043: * @param startAt (first row is 1)
044: * @param endAt (Query.NO_END_AT_INDEX stands for all available rows)
045: */
046: public PagingIterator(OJBIterator anIterator, int startAt, int endAt) {
047: super ();
048:
049: if (endAt != Query.NO_START_AT_INDEX && startAt > endAt) {
050: throw new PersistenceBrokerException(
051: "startAt must be less than endAt.");
052: }
053:
054: m_iterator = anIterator;
055: m_fullSize = m_iterator.size();
056:
057: if (startAt == Query.NO_START_AT_INDEX) {
058: m_startAt = 1;
059: } else {
060: m_startAt = startAt;
061: }
062:
063: if (endAt == Query.NO_END_AT_INDEX) {
064: m_endAt = m_fullSize;
065: } else {
066: m_endAt = Math.min(endAt, m_fullSize);
067: }
068:
069: m_rowLimit = Math.max(0, m_endAt - m_startAt + 1);
070: m_currentCursorPosition = m_startAt - 1;
071:
072: m_iterator.absolute(m_currentCursorPosition);
073: }
074:
075: /**
076: * @see org.apache.ojb.broker.accesslayer.OJBIterator#size()
077: */
078: public int size() throws PersistenceBrokerException {
079: if (m_fullSize < m_rowLimit) {
080: return m_fullSize;
081: } else {
082: return m_rowLimit;
083: }
084: }
085:
086: /**
087: * @see org.apache.ojb.broker.accesslayer.OJBIterator#fullSize()
088: */
089: public int fullSize() throws PersistenceBrokerException {
090: return m_fullSize;
091: }
092:
093: /**
094: * @see org.apache.ojb.broker.accesslayer.OJBIterator#absolute(int)
095: */
096: public boolean absolute(int row) throws PersistenceBrokerException {
097: int newPosition = (m_startAt - 1) + row;
098:
099: if (newPosition < m_startAt) {
100: newPosition = Math.max(m_endAt + row, m_startAt - 1);
101: }
102:
103: if (newPosition > m_endAt) {
104: newPosition = m_endAt;
105: }
106:
107: m_currentCursorPosition = newPosition;
108: return m_iterator.absolute(newPosition);
109: }
110:
111: /**
112: * @see org.apache.ojb.broker.accesslayer.OJBIterator#relative(int)
113: */
114: public boolean relative(int row) throws PersistenceBrokerException {
115: return absolute(m_currentCursorPosition - (m_startAt - 1) + row);
116: }
117:
118: /**
119: * @see org.apache.ojb.broker.accesslayer.OJBIterator#releaseDbResources()
120: */
121: public void releaseDbResources() {
122: m_iterator.releaseDbResources();
123: }
124:
125: /**
126: * remove is not supported
127: */
128: public void remove() {
129: throw new UnsupportedOperationException(
130: "remove not supported by PagingIterator");
131: }
132:
133: /**
134: * @see java.util.Iterator#hasNext()
135: */
136: public boolean hasNext() {
137: if (m_currentCursorPosition < m_endAt) {
138: return true;
139: } else {
140: releaseDbResources();
141: return false;
142: }
143:
144: }
145:
146: /**
147: * @see java.util.Iterator#next()
148: */
149: public Object next() {
150: m_currentCursorPosition++;
151: return m_iterator.next();
152: }
153:
154: /**
155: * @see org.apache.ojb.broker.accesslayer.OJBIterator#disableLifeCycleEvents()
156: */
157: public void disableLifeCycleEvents() {
158: m_iterator.disableLifeCycleEvents();
159: }
160: }
|