com.browseengine.bobo.util.ResultMerger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bobo-browse Show documentation
Show all versions of bobo-browse Show documentation
Bobo is a Faceted Search implementation written purely in Java, an extension of Apache Lucene
The newest version!
package com.browseengine.bobo.util;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;
public class ResultMerger {
public static Iterator mergeResults(final Iterator[] results,
final Comparator comparator) {
return new Iterator() {
TreeMap> map = new TreeMap>(comparator);
{
for (Iterator result : results) {
if (result.hasNext()) {
map.put(result.next(), result);
}
}
}
@Override
public boolean hasNext() {
return map.size() > 0;
}
@Override
public T next() {
T first = map.firstKey();
Iterator iter = map.remove(first);
while (iter.hasNext()) {
T next = iter.next();
if (!map.containsKey(next)) {
map.put(next, iter);
break;
}
}
return first;
}
@Override
public void remove() {
T first = map.firstKey();
Iterator iter = map.remove(first);
while (iter.hasNext()) {
T next = iter.next();
if (!map.containsKey(next)) {
map.put(next, iter);
break;
}
}
}
};
}
}