package org.apache.lucene.search.spans;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.LeafSimScorer;
public class TermSpans extends Spans {
protected final PostingsEnum postings;
protected final Term term;
protected int doc;
protected int freq;
protected int count;
protected int position;
protected boolean readPayload;
private final float positionsCost;
public TermSpans(LeafSimScorer scorer,
PostingsEnum postings, Term term, float positionsCost) {
this.postings = Objects.requireNonNull(postings);
this.term = Objects.requireNonNull(term);
this.doc = -1;
this.position = -1;
assert positionsCost > 0;
this.positionsCost = positionsCost;
}
@Override
public int nextDoc() throws IOException {
doc = postings.nextDoc();
if (doc != DocIdSetIterator.NO_MORE_DOCS) {
freq = postings.freq();
assert freq >= 1;
count = 0;
}
position = -1;
return doc;
}
@Override
public int advance(int target) throws IOException {
assert target > doc;
doc = postings.advance(target);
if (doc != DocIdSetIterator.NO_MORE_DOCS) {
freq = postings.freq();
assert freq >= 1;
count = 0;
}
position = -1;
return doc;
}
@Override
public int docID() {
return doc;
}
@Override
public int nextStartPosition() throws IOException {
if (count == freq) {
assert position != NO_MORE_POSITIONS;
return position = NO_MORE_POSITIONS;
}
int prevPosition = position;
position = postings.nextPosition();
assert position >= prevPosition : "prevPosition="+prevPosition+" > position="+position;
assert position != NO_MORE_POSITIONS;
count++;
readPayload = false;
return position;
}
@Override
public int startPosition() {
return position;
}
@Override
public int endPosition() {
return (position == -1) ? -1
: (position != NO_MORE_POSITIONS) ? position + 1
: NO_MORE_POSITIONS;
}
@Override
public int width() {
return 0;
}
@Override
public long cost() {
return postings.cost();
}
@Override
public void collect(SpanCollector collector) throws IOException {
collector.collectLeaf(postings, position, term);
}
@Override
public float positionsCost() {
return positionsCost;
}
@Override
public String toString() {
return "spans(" + term.toString() + ")@" +
(doc == -1 ? "START" : (doc == NO_MORE_DOCS) ? "ENDDOC"
: doc + " - " + (position == NO_MORE_POSITIONS ? "ENDPOS" : position));
}
public PostingsEnum getPostings() {
return postings;
}
}