01: /*
02: This source file is part of Smyle, a database library.
03: For up-to-date information, see http://www.drjava.de/smyle
04: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
05:
06: This library is free software; you can redistribute it and/or
07: modify it under the terms of the GNU Lesser General Public
08: License as published by the Free Software Foundation; either
09: version 2.1 of the License, or (at your option) any later version.
10:
11: This library is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public
17: License along with this library; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19:
20: For full license text, see doc/license/lgpl.txt in this distribution
21: */
22:
23: package drjava.smyle.core;
24:
25: import java.io.*;
26: import java.util.*;
27: import java.lang.Object;
28: import java.lang.ref.*;
29: import org.artsProject.mcop.*;
30: import org.artsProject.mcop.core.*;
31: import org.artsProject.util.*;
32: import drjava.smyle.*;
33: import drjava.smyle.meta.*;
34:
35: class TableScan<T extends Struct<T>> implements TableIterator<T> {
36: TableImpl<T> table;
37: Filter<T> filter;
38: int expectedModCount;
39: int i = -1;
40:
41: TableScan(TableImpl<T> table, Filter<T> filter) {
42: this .table = table;
43: expectedModCount = table.modCount;
44: this .filter = filter;
45: findNext();
46: }
47:
48: private void checkForComodification() {
49: if (table.modCount != expectedModCount)
50: throw new ConcurrentModificationException();
51: }
52:
53: public boolean hasNext() {
54: checkForComodification();
55: return i < table.size();
56: }
57:
58: public T next() {
59: checkForComodification();
60: T result = table.loadElement(i);
61: findNext();
62: return result;
63: }
64:
65: public ChunkRef nextChunk() {
66: checkForComodification();
67: ChunkRef result = table.getElementChunkRef(i);
68: findNext();
69: return result;
70: }
71:
72: public void remove() {
73: checkForComodification();
74: ++expectedModCount;
75: table.remove(i - 1);
76: i -= 2;
77: //System.out.println("i now: "+i+" table size now: "+table.size());
78: findNext();
79: }
80:
81: void findNext() {
82: while (++i < table.size()) {
83: if (filter == null
84: || table.matches(filter, table.loadElement(i)))
85: return;
86: }
87: }
88: }
|