01: // kelondroCloneableMapIterator.java
02: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
03: // first published 25.04.2007 on http://yacy.net
04: //
05: // This is a part of YaCy, a peer-to-peer based web search engine
06: //
07: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
08: // $LastChangedRevision: 1986 $
09: // $LastChangedBy: orbiter $
10: //
11: // LICENSE
12: //
13: // This program is free software; you can redistribute it and/or modify
14: // it under the terms of the GNU General Public License as published by
15: // the Free Software Foundation; either version 2 of the License, or
16: // (at your option) any later version.
17: //
18: // This program is distributed in the hope that it will be useful,
19: // but WITHOUT ANY WARRANTY; without even the implied warranty of
20: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: // GNU General Public License for more details.
22: //
23: // You should have received a copy of the GNU General Public License
24: // along with this program; if not, write to the Free Software
25: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26:
27: package de.anomic.kelondro;
28:
29: import java.util.Iterator;
30: import java.util.TreeMap;
31:
32: public class kelondroCloneableMapIterator<E> implements
33: kelondroCloneableIterator<E> {
34:
35: TreeMap<E, ?> map;
36: E next, last;
37: Object start;
38: Iterator<E> iter;
39:
40: public kelondroCloneableMapIterator(TreeMap<E, ?> map, E start) {
41: // map must contain eiter a byte[]/Object or a String/Object mapping.
42: // start must be either of type byte[] or String
43: // this iterator iterates then only the key elements of the map
44: this .map = map;
45: this .start = start;
46: this .iter = map.keySet().iterator();
47: if (this .start == null) {
48: if (iter.hasNext())
49: this .next = iter.next();
50: else
51: this .next = null;
52: } else
53: while (iter.hasNext()) {
54: this .next = iter.next();
55: if (map.comparator().compare(next, start) > 1)
56: break;
57: }
58: this .last = null;
59: }
60:
61: @SuppressWarnings("unchecked")
62: public kelondroCloneableMapIterator<E> clone(Object modifier) {
63: return new kelondroCloneableMapIterator(map, modifier);
64: }
65:
66: public boolean hasNext() {
67: return this .next != null;
68: }
69:
70: public E next() {
71: // returns key-elements, not entry-elements
72: this .last = this .next;
73: if (this .iter.hasNext()) {
74: this .next = this .iter.next();
75: } else {
76: this .next = null;
77: }
78: return this .last;
79: }
80:
81: public void remove() {
82: this.map.remove(this.last);
83: }
84:
85: }
|