All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.browseengine.bobo.util.ResultMerger Maven / Gradle / Ivy

Go to download

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;
          }
        }
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy