001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.runtime.util;
020:
021: import java.util.Iterator;
022: import java.util.ArrayList;
023: import java.util.Collections;
024:
025: import org.apache.beehive.netui.util.logging.Logger;
026:
027: /**
028: *
029: */
030: public final class PagedDataSet implements Iterator {
031:
032: private static final Logger LOGGER = Logger
033: .getInstance(PagedDataSet.class);
034:
035: private boolean _completedBootstrap = false;
036: private int _dataSetSize = 0;
037: private int _currentIndex = -1;
038: private int _startWindow = -1;
039: private int _endWindow = -1;
040:
041: private String _dataSource = null;
042: private ArrayList _list = null;
043: private Iterator _dataSetIterator = null;
044: private Object _currentItem = null;
045:
046: public PagedDataSet(String dataSource, Iterator iterator) {
047: _dataSource = dataSource;
048:
049: if (iterator == null)
050: _dataSetIterator = Collections.EMPTY_LIST.iterator();
051: else
052: _dataSetIterator = iterator;
053:
054: LOGGER.debug("iterator type: "
055: + _dataSetIterator.getClass().getName());
056:
057: /* 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 */
058: _list = new ArrayList();
059: while (_dataSetIterator.hasNext()) {
060: _list.add(_dataSetIterator.next());
061: _dataSetSize++;
062: }
063: _dataSetIterator = _list.iterator();
064: }
065:
066: public void createWindow(int startWindow, int windowSize) {
067: /* todo: error checking */
068: _startWindow = startWindow;
069: /* the size of the window is inclusive, so remove one from the total size */
070: _endWindow = _startWindow + windowSize - 1;
071: }
072:
073: public boolean hasNext() {
074: if (!_completedBootstrap && _startWindow > 0) {
075:
076: while (_dataSetIterator.hasNext()
077: && (_currentIndex + 1) != _startWindow) {
078: _dataSetIterator.next();
079: _currentIndex++;
080: }
081: _completedBootstrap = true;
082: } else if (_endWindow > -1 && (_currentIndex >= _endWindow)) {
083: LOGGER
084: .debug("current index: " + _currentIndex
085: + " _endRenderWindiw: " + _endWindow
086: + " end data set: "
087: + (_currentIndex >= _endWindow));
088: return false;
089: }
090:
091: boolean hasNext = _dataSetIterator.hasNext();
092: if (!hasNext) {
093: _currentIndex = -1;
094: _currentItem = null;
095: }
096:
097: return hasNext;
098: }
099:
100: public Object next() {
101: _currentItem = _dataSetIterator.next();
102: _currentIndex++;
103: return _currentItem;
104: }
105:
106: public void remove() {
107: throw new UnsupportedOperationException();
108: }
109:
110: public String getDataSource() {
111: return _dataSource;
112: }
113:
114: public int getSize() {
115: return _dataSetSize;
116: }
117:
118: public int getCurrentIndex() {
119: return _currentIndex;
120: }
121:
122: public Object getCurrentItem() {
123: return _currentItem;
124: }
125: }
|