KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > runtime > util > PagedDataSet


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.runtime.util;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23
24 import org.apache.beehive.netui.util.logging.Logger;
25
26 /**
27  *
28  */

29 public final class PagedDataSet
30     implements Iterator JavaDoc {
31
32     private static final Logger LOGGER = Logger.getInstance(PagedDataSet.class);
33
34     private boolean _completedBootstrap = false;
35     private int _dataSetSize = 0;
36     private int _currentIndex = -1;
37     private int _startWindow = -1;
38     private int _endWindow = -1;
39
40     private String JavaDoc _dataSource = null;
41     private ArrayList JavaDoc _list = null;
42     private Iterator JavaDoc _dataSetIterator = null;
43     private Object JavaDoc _currentItem = null;
44
45     public PagedDataSet(String JavaDoc dataSource, Iterator JavaDoc iterator) {
46         _dataSource = dataSource;
47
48         if(iterator == null)
49             _dataSetIterator = Collections.EMPTY_LIST.iterator();
50         else
51             _dataSetIterator = iterator;
52
53         LOGGER.debug("iterator type: " + _dataSetIterator.getClass().getName());
54
55         /* todo: would be nice to have a limit here so that this isn't needed in order to find the end of the data set */
56         _list = new ArrayList JavaDoc();
57         while(_dataSetIterator.hasNext()) {
58             _list.add(_dataSetIterator.next());
59             _dataSetSize++;
60         }
61         _dataSetIterator = _list.iterator();
62     }
63
64     public void createWindow(int startWindow, int windowSize) {
65         /* todo: error checking */
66         _startWindow = startWindow;
67         /* the size of the window is inclusive, so remove one from the total size */
68         _endWindow = _startWindow + windowSize - 1;
69     }
70
71     public boolean hasNext() {
72         if(!_completedBootstrap && _startWindow > 0) {
73
74             while(_dataSetIterator.hasNext() && (_currentIndex+1) != _startWindow) {
75                 _dataSetIterator.next();
76                 _currentIndex++;
77             }
78             _completedBootstrap = true;
79         }
80         else if(_endWindow > -1 && (_currentIndex >= _endWindow)) {
81             LOGGER.debug("current index: " + _currentIndex + " _endRenderWindiw: " + _endWindow + " end data set: " + (_currentIndex >= _endWindow));
82             return false;
83         }
84
85         boolean hasNext = _dataSetIterator.hasNext();
86         if(!hasNext) {
87             _currentIndex = -1;
88             _currentItem = null;
89         }
90
91         return hasNext;
92     }
93
94     public Object JavaDoc next() {
95         _currentItem = _dataSetIterator.next();
96         _currentIndex++;
97         return _currentItem;
98     }
99
100     public void remove() {
101         throw new UnsupportedOperationException JavaDoc();
102     }
103
104     public String JavaDoc getDataSource() {
105         return _dataSource;
106     }
107
108     public int getSize() {
109         return _dataSetSize;
110     }
111
112     public int getCurrentIndex() {
113         return _currentIndex;
114     }
115
116     public Object JavaDoc getCurrentItem() {
117         return _currentItem;
118     }
119 }
120
Popular Tags