001    package com.khubla.pragmatach.framework.dao;
002    
003    import java.util.List;
004    
005    import com.khubla.pragmatach.framework.api.PragmatachException;
006    
007    /**
008     * @author tome
009     */
010    public class PagerImpl<T, I> implements Pager<T> {
011       /**
012        * number of records to return at a time
013        */
014       private final int batchsize;
015       /**
016        * current records
017        */
018       private int currentRecords = 0;
019       /**
020        * total size
021        */
022       private final int totalRecords;
023       /**
024        * dao
025        */
026       private final DAO<T, I> dao;
027    
028       /**
029        * ctor
030        */
031       public PagerImpl(DAO<T, I> dao, int batchsize) throws PragmatachException {
032          this.batchsize = batchsize;
033          this.dao = dao;
034          this.totalRecords = (int) dao.count();
035       }
036    
037       public int getBatchsize() {
038          return batchsize;
039       }
040    
041       @Override
042       public boolean hasNext() {
043          return (currentRecords < totalRecords) ? true : false;
044       }
045    
046       @Override
047       public List<T> next() throws PragmatachException {
048          try {
049             /*
050              * query
051              */
052             final List<T> ret = dao.getAll(currentRecords, batchsize);
053             currentRecords += ret.size();
054             return ret;
055          } catch (final Exception e) {
056             throw new PragmatachException("Exception in next", e);
057          }
058       }
059    }