KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > cursor > impl > CursorImpl


1 package org.sapia.util.cursor.impl;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.NoSuchElementException JavaDoc;
6
7 import org.sapia.util.cursor.Batch;
8 import org.sapia.util.cursor.Cursor;
9 import org.sapia.util.cursor.CursorException;
10 import org.sapia.util.cursor.CursorFeed;
11 import org.sapia.util.cursor.CursorListener;
12
13 /**
14  * @author Yanick Duchesne
15  *
16  * <dl>
17  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class CursorImpl implements Cursor{
23   
24   private int _batchSize;
25   private List JavaDoc _batches = new ArrayList JavaDoc(20);
26   private int _pos, _totalCount;
27   private boolean _finished;
28   private CursorFeed _feed;
29   private Batch _currentBatch;
30   private CursorListener _listener;
31   
32   /**
33    * @param feed a <code>CursorFeed</code>.
34    * @param listener a <code>CursorListener</code>, or <code>null</code> if no listener is provided.
35    * @param batchSize the size of the <code>Batch</code> instances that will
36    * be created by this instance.
37    */

38   public CursorImpl(CursorFeed feed, CursorListener listener, int batchSize){
39     _batchSize = batchSize;
40     _feed = feed;
41     _listener = listener;
42   }
43   
44   /**
45    * @see org.sapia.util.cursor.Cursor#getBatchCount()
46    */

47   public int getBatchCount() {
48     return _batches.size();
49   }
50   
51   /**
52    * @see org.sapia.util.cursor.Cursor#getItemCount()
53    */

54   public int getItemCount() {
55     return _totalCount;
56   }
57   
58   /**
59    *
60    * @see Cursor#getBatchSize()
61    */

62   public int getBatchSize(){
63     return _batchSize;
64   }
65   
66   /**
67    * @see org.sapia.util.cursor.Cursor#getCurrentBatch()
68    */

69   public Batch getCurrentBatch(){
70     if(_currentBatch == null){
71       throw new NoSuchElementException JavaDoc();
72     }
73     return _currentBatch;
74   }
75   
76   /**
77    * @see Cursor#hasNextBatch()
78    */

79   public boolean hasNextBatch(){
80     if(_finished){
81       return false;
82     }
83     else if(_pos >= _batches.size()){
84       Object JavaDoc[] items = newItems();
85       int count = 0;
86       try{
87         count = _feed.read(items);
88       }catch(Exception JavaDoc e){
89         throw new CursorException("Could not feed", e);
90       }
91       if(count == 0){
92         _finished = true;
93         return false;
94       }
95       Batch b = new BatchImpl(items, _listener, count, _totalCount);
96       _batches.add(b);
97       _totalCount = _totalCount+count;
98       return true;
99     }
100     else{
101       return true;
102     }
103   }
104   
105   /**
106    * @see Cursor#hasPreviousBatch()
107    */

108   public boolean hasPreviousBatch(){
109     return _pos > 0;
110   }
111   
112   /**
113    * @see Cursor#nextBatch()
114    */

115   public Batch nextBatch(){
116     if(_pos >= _batches.size()){
117       throw new NoSuchElementException JavaDoc();
118     }
119     return _currentBatch = (Batch)_batches.get(_pos++);
120   }
121   
122   /**
123    * @return Cursor#previousBatch()
124    */

125   public Batch previousBatch(){
126     if(_pos <= 0){
127       throw new NoSuchElementException JavaDoc();
128     }
129     if(_pos >= _batches.size()){
130       _pos--;
131     }
132     return _currentBatch = (Batch)_batches.get(_pos--);
133   }
134   
135   /**
136    * @return Cursor#toLast()
137    */

138   public Cursor toLast(){
139     if(_batches.size() > 0)
140       _pos = _batches.size() - 1;
141     return this;
142   }
143   
144   /**
145    * @return Cursor#toFirst()
146    */

147   public Cursor toFirst(){
148     _pos = 0;
149     return this;
150   }
151   
152   /**
153    * @see org.sapia.util.cursor.Cursor#available()
154    */

155   public int available() {
156     try{
157       return _feed.available();
158     }catch(Exception JavaDoc e){
159       throw new CursorException("Could not poll underlying feed", e);
160     }
161   }
162   
163   /**
164    * @see org.sapia.util.cursor.Cursor#close()
165    */

166   public void close() {
167     _feed.close();
168   }
169   
170   private Object JavaDoc[] newItems(){
171     return new Object JavaDoc[_batchSize];
172   }
173 }
174
Popular Tags