01: /*
02: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
03:
04: This file is part of the JODB (Java Objects Database) open source project.
05:
06: JODB is free software; you can redistribute it and/or modify it under
07: the terms of version 2 of the GNU General Public License as published
08: by the Free Software Foundation.
09:
10: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
11: WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13: for more details.
14:
15: You should have received a copy of the GNU General Public License along
16: with this program; if not, write to the Free Software Foundation, Inc.,
17: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18: */
19: package com.mobixess.jodb.core.query;
20:
21: import java.util.Vector;
22:
23: import com.mobixess.jodb.core.index.IndexIterator;
24:
25: public class LArrayChunkedBuffer implements IndexIterator {
26:
27: private int _chunkSize;
28:
29: private Vector<long[]> _chunks = new Vector<long[]>();
30:
31: private int _totalAccumulated;
32:
33: private int _totalInLastChunk;
34:
35: private int _iteratorChunkIndex = 0;
36:
37: private int _iteratorInChunkIndex = 0;
38:
39: public LArrayChunkedBuffer() {
40: this (500);
41: }
42:
43: /**
44: * @param chunkSize
45: */
46: public LArrayChunkedBuffer(int chunkSize) {
47: super ();
48: _chunkSize = chunkSize;
49: addChunk();
50: }
51:
52: private void addChunk() {
53: long[] newChunk = new long[_chunkSize];
54: _chunks.add(newChunk);
55: }
56:
57: public void add(long value) {
58: if (_totalInLastChunk == _chunkSize) {
59: addChunk();
60: _totalInLastChunk = 0;
61: }
62: long[] lastChunk = _chunks.elementAt(_chunks.size() - 1);
63: lastChunk[_totalInLastChunk++] = value;
64: _totalAccumulated++;
65: }
66:
67: public final void resetIterator() {
68: _iteratorChunkIndex = 0;
69: _iteratorInChunkIndex = 0;
70: }
71:
72: public final boolean hasNext() {
73: return _iteratorChunkIndex < _chunks.size() - 1
74: || _iteratorInChunkIndex < _totalInLastChunk;
75: }
76:
77: public final int length() {
78: return _totalAccumulated;
79: }
80:
81: public long next() {
82: if (!hasNext()) {
83: throw new IllegalStateException();
84: }
85: long[] currentChunk = _chunks.elementAt(_iteratorChunkIndex);
86: long result = currentChunk[_iteratorInChunkIndex++];
87: if (_iteratorInChunkIndex == currentChunk.length) {
88: _iteratorChunkIndex++;
89: _iteratorInChunkIndex = 0;
90: }
91: return result;
92: }
93:
94: }
|