1 package org.apache.ojb.broker.accesslayer; 2 3 17 18 import org.apache.ojb.broker.PersistenceBrokerException; 19 import org.apache.ojb.broker.query.Query; 20 21 32 public class PagingIterator implements OJBIterator 33 { 34 private final OJBIterator m_iterator; 35 private int m_startAt; 36 private int m_endAt; 37 private int m_rowLimit; 38 private int m_fullSize; 39 private int m_currentCursorPosition; 41 47 public PagingIterator(OJBIterator anIterator, int startAt, int endAt) 48 { 49 super(); 50 51 if (endAt != Query.NO_START_AT_INDEX && startAt > endAt) 52 { 53 throw new PersistenceBrokerException("startAt must be less than endAt."); 54 } 55 56 m_iterator = anIterator; 57 m_fullSize = m_iterator.size(); 58 59 if (startAt == Query.NO_START_AT_INDEX) 60 { 61 m_startAt = 1; 62 } 63 else 64 { 65 m_startAt = startAt; 66 } 67 68 if (endAt == Query.NO_END_AT_INDEX) 69 { 70 m_endAt = m_fullSize; 71 } 72 else 73 { 74 m_endAt = Math.min(endAt, m_fullSize); 75 } 76 77 m_rowLimit = Math.max(0, m_endAt - m_startAt + 1); 78 m_currentCursorPosition = m_startAt - 1; 79 80 m_iterator.absolute(m_currentCursorPosition); 81 } 82 83 86 public int size() throws PersistenceBrokerException 87 { 88 if (m_fullSize < m_rowLimit) 89 { 90 return m_fullSize; 91 } 92 else 93 { 94 return m_rowLimit; 95 } 96 } 97 98 101 public int fullSize() throws PersistenceBrokerException 102 { 103 return m_fullSize; 104 } 105 106 109 public boolean absolute(int row) throws PersistenceBrokerException 110 { 111 int newPosition = (m_startAt - 1) + row; 112 113 if (newPosition < m_startAt) 114 { 115 newPosition = Math.max(m_endAt + row, m_startAt - 1); 116 } 117 118 if (newPosition > m_endAt) 119 { 120 newPosition = m_endAt; 121 } 122 123 m_currentCursorPosition = newPosition; 124 return m_iterator.absolute(newPosition); 125 } 126 127 130 public boolean relative(int row) throws PersistenceBrokerException 131 { 132 return absolute(m_currentCursorPosition - (m_startAt - 1) + row); 133 } 134 135 138 public void releaseDbResources() 139 { 140 m_iterator.releaseDbResources(); 141 } 142 143 146 public void remove() 147 { 148 throw new UnsupportedOperationException ("remove not supported by PagingIterator"); 149 } 150 151 154 public boolean hasNext() 155 { 156 if (m_currentCursorPosition < m_endAt) 157 { 158 return true; 159 } 160 else 161 { 162 releaseDbResources(); 163 return false; 164 } 165 166 } 167 168 171 public Object next() 172 { 173 m_currentCursorPosition++; 174 return m_iterator.next(); 175 } 176 177 180 public void disableLifeCycleEvents() 181 { 182 m_iterator.disableLifeCycleEvents(); 183 } 184 } 185 | Popular Tags |