001: package org.apache.lucene.search.spans;
002:
003: /**
004: * Copyright 2005 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.lucene.index.Term;
020: import org.apache.lucene.index.TermPositions;
021:
022: import java.io.IOException;
023:
024: /**
025: * Expert:
026: * Public for extension only
027: */
028: public class TermSpans implements Spans {
029: protected TermPositions positions;
030: protected Term term;
031: protected int doc;
032: protected int freq;
033: protected int count;
034: protected int position;
035:
036: public TermSpans(TermPositions positions, Term term)
037: throws IOException {
038:
039: this .positions = positions;
040: this .term = term;
041: doc = -1;
042: }
043:
044: public boolean next() throws IOException {
045: if (count == freq) {
046: if (!positions.next()) {
047: doc = Integer.MAX_VALUE;
048: return false;
049: }
050: doc = positions.doc();
051: freq = positions.freq();
052: count = 0;
053: }
054: position = positions.nextPosition();
055: count++;
056: return true;
057: }
058:
059: public boolean skipTo(int target) throws IOException {
060: // are we already at the correct position?
061: if (doc >= target) {
062: return true;
063: }
064:
065: if (!positions.skipTo(target)) {
066: doc = Integer.MAX_VALUE;
067: return false;
068: }
069:
070: doc = positions.doc();
071: freq = positions.freq();
072: count = 0;
073:
074: position = positions.nextPosition();
075: count++;
076:
077: return true;
078: }
079:
080: public int doc() {
081: return doc;
082: }
083:
084: public int start() {
085: return position;
086: }
087:
088: public int end() {
089: return position + 1;
090: }
091:
092: public String toString() {
093: return "spans("
094: + term.toString()
095: + ")@"
096: + (doc == -1 ? "START"
097: : (doc == Integer.MAX_VALUE) ? "END" : doc
098: + "-" + position);
099: }
100:
101: public TermPositions getPositions() {
102: return positions;
103: }
104: }
|