/
MergeIterator.java
182 lines (166 loc) · 6.24 KB
/
MergeIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// MergeIterator.java
// --------------------------
// part of The Kelondro Database
// (C) by Michael Peter Christen; mc@yacy.net
// first published on http://www.anomic.de
// Frankfurt, Germany, 2005
// last major change: 08.05.2005
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package net.yacy.kelondro.order;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import net.yacy.cora.order.CloneableIterator;
import net.yacy.cora.order.Order;
import net.yacy.kelondro.logging.Log;
public class MergeIterator<E> implements CloneableIterator<E> {
private final Comparator<E> comp;
private final CloneableIterator<E> a;
private final CloneableIterator<E> b;
private E na, nb;
private final Method merger;
private final boolean up;
public MergeIterator(
final CloneableIterator<E> a,
final CloneableIterator<E> b,
final Comparator<E> c,
final Method m,
final boolean up) {
// this works currently only for String-type key iterations
assert a != null;
assert b != null;
this.a = a;
this.b = b;
this.up = up;
this.comp = c;
this.merger = m;
nexta();
nextb();
}
@Override
public MergeIterator<E> clone(final Object modifier) {
assert this.a != null;
assert this.b != null;
assert this.merger != null;
return new MergeIterator<E>(this.a.clone(modifier), this.b.clone(modifier), this.comp, this.merger, this.up);
}
private void nexta() {
try {
if (this.a != null && this.a.hasNext()) this.na = this.a.next(); else this.na = null;
} catch (final ConcurrentModificationException e) {
this.na = null;
}
}
private void nextb() {
try {
if (this.b != null && this.b.hasNext()) this.nb = this.b.next(); else this.nb = null;
} catch (final ConcurrentModificationException e) {
this.nb = null;
}
}
@Override
public boolean hasNext() {
return (this.na != null) || (this.nb != null);
}
@Override
@SuppressWarnings("unchecked")
public E next() {
E s;
if (this.na == null) {
s = this.nb;
nextb();
return s;
}
if (this.nb == null) {
s = this.na;
nexta();
return s;
}
// compare the Objects
final int c = this.comp.compare(this.na, this.nb);
if (c == 0) {
try {
//System.out.print("MERGE OF " + na.toString() + " AND " + nb.toString() + ": ");
s = (E) this.merger.invoke(null, new Object[]{this.na, this.nb});
//System.out.println("RESULT IS " + s.toString());
} catch (final IllegalArgumentException e) {
Log.logException(e);
s = null;
} catch (final IllegalAccessException e) {
Log.logException(e);
s = null;
} catch (final InvocationTargetException e) {
Log.logException(e);
Log.logException(e.getCause());
s = null;
}
nexta();
nextb();
return s;
} else if ((this.up && c < 0) || (!this.up && c > 0)) {
s = this.na;
nexta();
return s;
} else {
s = this.nb;
nextb();
return s;
}
}
@Override
public void remove() {
throw new java.lang.UnsupportedOperationException("merge does not support remove");
}
public static <A> CloneableIterator<A> cascade(final Collection<CloneableIterator<A>> iterators, final Order<A> c, final Method merger, final boolean up) {
// this extends the ability to combine two iterators
// to the ability of combining a set of iterators
if (iterators == null) return null;
if (iterators.isEmpty()) return null;
return cascade(iterators.iterator(), c, merger, up);
}
private static <A> CloneableIterator<A> cascade(final Iterator<CloneableIterator<A>> iiterators, final Order<A> c, final Method merger, final boolean up) {
if (iiterators == null) return null;
if (!(iiterators.hasNext())) return null;
final CloneableIterator<A> one = iiterators.next();
if (!(iiterators.hasNext())) return one;
assert merger != null;
return new MergeIterator<A>(one, cascade(iiterators, c, merger, up), c, merger, up);
}
public static final Method simpleMerge;
static {
Method meth = null;
try {
final Class<?> c = net.yacy.kelondro.order.MergeIterator.class;
meth = c.getMethod("mergeEqualByReplace", new Class[]{Object.class, Object.class});
} catch (final SecurityException e) {
System.out.println("Error while initializing simpleMerge (1): " + e.getMessage());
meth = null;
} catch (final NoSuchMethodException e) {
System.out.println("Error while initializing simpleMerge (3): " + e.getMessage());
meth = null;
}
assert meth != null;
simpleMerge = meth;
}
// do not remove the following method, it is not reference anywhere directly but indirectly using reflection
// please see initialization of simpleMerge above
public static Object mergeEqualByReplace(final Object a, final Object b) {
return a;
}
}