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

com.browseengine.bobo.util.SearchResultMerger 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.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.lucene.util.PriorityQueue;

public class SearchResultMerger {
  private SearchResultMerger() {

  }

  public static class MergedIterator implements Iterator {
    private class IteratorCtx {
      public Iterator _iterator;
      public T _curVal;

      public IteratorCtx(Iterator iterator) {
        _iterator = iterator;
        _curVal = null;
      }

      public boolean fetch() {
        if (_iterator.hasNext()) {
          _curVal = _iterator.next();
          return true;
        }
        _curVal = null;
        return false;
      }
    }

    @SuppressWarnings("rawtypes")
    private final PriorityQueue _queue;

    @SuppressWarnings("unchecked")
    public MergedIterator(final List> sources, final Comparator comparator) {
      _queue = new PriorityQueue(sources.size()) {
        @Override
        protected boolean lessThan(Object o1, Object o2) {
          T v1 = ((IteratorCtx) o1)._curVal;
          T v2 = ((IteratorCtx) o2)._curVal;

          return (comparator.compare(v1, v2) < 0);
        }
      };

      for (Iterator iterator : sources) {
        IteratorCtx ctx = new IteratorCtx(iterator);
        if (ctx.fetch()) _queue.add(ctx);
      }
    }

    @Override
    public boolean hasNext() {
      return _queue.size() > 0;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T next() {
      IteratorCtx ctx = (IteratorCtx) _queue.top();
      T val = ctx._curVal;
      if (ctx.fetch()) {
        _queue.updateTop();
      } else {
        _queue.pop();
      }
      return val;
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  }

  public static  Iterator mergeIterator(List> results, Comparator comparator) {
    return new MergedIterator(results, comparator);
  }

  public static  ArrayList mergeResult(int offset, int count, List> results,
      Comparator comparator) {
    Iterator mergedIter = mergeIterator(results, comparator);

    for (int c = 0; c < offset && mergedIter.hasNext(); c++) {
      mergedIter.next();
    }

    ArrayList mergedList = new ArrayList();

    for (int c = 0; c < count && mergedIter.hasNext(); c++) {
      mergedList.add(mergedIter.next());
    }

    return mergedList;
  }
}