Coverage Report - org.apache.ojb.broker.accesslayer.PagingIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
PagingIterator
N/A
N/A
2.2
 
 1  
 package org.apache.ojb.broker.accesslayer;
 2  
 
 3  
 /* Copyright 2003-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import org.apache.ojb.broker.PersistenceBrokerException;
 19  
 import org.apache.ojb.broker.query.Query;
 20  
 
 21  
 /**
 22  
  * PagingIterator is wrapper around an OJBIterator to support startAt endAt
 23  
  * positions. The PagingIterator returns rows <b>including</b> startAt 
 24  
  * and <b>including</b> endAt.
 25  
  * 
 26  
  * startAt = 1, endAt = 11 returns rows 1 to 11 if available
 27  
  * if endAt == Query.NO_END_AT_INDEX endAt is set to the last available row
 28  
  * 
 29  
  * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
 30  
  * @version $Id: PagingIterator.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
 31  
  */
 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; // position of the wrapped iterator
 40  
 
 41  
     /**
 42  
      * Constructor 
 43  
      * @param anIterator wrapped Iterator
 44  
      * @param startAt (first row is 1)
 45  
      * @param endAt (Query.NO_END_AT_INDEX stands for all available rows) 
 46  
      */
 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  
     /**
 84  
          * @see org.apache.ojb.broker.accesslayer.OJBIterator#size()
 85  
          */
 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  
     /**
 99  
      * @see org.apache.ojb.broker.accesslayer.OJBIterator#fullSize()
 100  
      */
 101  
     public int fullSize() throws PersistenceBrokerException
 102  
     {
 103  
         return m_fullSize;
 104  
     }
 105  
     
 106  
     /**
 107  
          * @see org.apache.ojb.broker.accesslayer.OJBIterator#absolute(int)
 108  
          */
 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  
     /**
 128  
          * @see org.apache.ojb.broker.accesslayer.OJBIterator#relative(int)
 129  
          */
 130  
     public boolean relative(int row) throws PersistenceBrokerException
 131  
     {
 132  
         return absolute(m_currentCursorPosition - (m_startAt - 1) + row);
 133  
     }
 134  
 
 135  
     /**
 136  
          * @see org.apache.ojb.broker.accesslayer.OJBIterator#releaseDbResources()
 137  
          */
 138  
     public void releaseDbResources()
 139  
     {
 140  
         m_iterator.releaseDbResources();
 141  
     }
 142  
 
 143  
     /**
 144  
          * remove is not supported
 145  
          */
 146  
     public void remove()
 147  
     {
 148  
         throw new UnsupportedOperationException("remove not supported by PagingIterator");
 149  
     }
 150  
 
 151  
     /**
 152  
          * @see java.util.Iterator#hasNext()
 153  
          */
 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  
     /**
 169  
          * @see java.util.Iterator#next()
 170  
          */
 171  
     public Object next()
 172  
     {
 173  
         m_currentCursorPosition++;
 174  
         return m_iterator.next();
 175  
     }
 176  
 
 177  
     /**
 178  
      * @see org.apache.ojb.broker.accesslayer.OJBIterator#disableLifeCycleEvents()
 179  
      */
 180  
     public void disableLifeCycleEvents()
 181  
     {
 182  
         m_iterator.disableLifeCycleEvents();       
 183  
     }
 184  
 }