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

com.mysema.query.lucene.ResultIterator Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
/**
 *
 */
package com.mysema.query.lucene;

import java.io.IOException;

import javax.annotation.Nullable;

import org.apache.commons.collections15.Transformer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;

import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.QueryException;

/**
 * @author tiwe
 *
 * @param 
 */
public final class ResultIterator implements CloseableIterator {

    private final ScoreDoc[] scoreDocs;

    private int cursor;

    private final Searcher searcher;

    @Nullable
    private final FieldSelector fieldSelector;

    private final Transformer transformer;

    public ResultIterator(ScoreDoc[] scoreDocs, int offset, Searcher searcher, @Nullable FieldSelector fieldSelector, Transformer transformer) {
        this.scoreDocs = scoreDocs;
        this.cursor = offset;
        this.searcher = searcher;
        this.fieldSelector = fieldSelector;
        this.transformer = transformer;
    }

    @Override
    public boolean hasNext() {
        return cursor != scoreDocs.length;
    }

    @Override
    public T next() {
        try {
            Document document;
            if (fieldSelector != null){
                document = searcher.doc(scoreDocs[cursor++].doc, fieldSelector);
            }else{
                document = searcher.doc(scoreDocs[cursor++].doc);
            }
            return transformer.transform(document);
        } catch (IOException e) {
            throw new QueryException(e);
        }
    }

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

    @Override
    public void close() {

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy