pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / spans / TermSpans.java
1 package org.apache.lucene.search.spans;
2 /**
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.index.TermPositions;
21
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.Collection;
25
26 /**
27  * Expert:
28  * Public for extension only
29  */
30 public class TermSpans extends Spans {
31   protected TermPositions positions;
32   protected Term term;
33   protected int doc;
34   protected int freq;
35   protected int count;
36   protected int position;
37
38
39   public TermSpans(TermPositions positions, Term term) throws IOException {
40
41     this.positions = positions;
42     this.term = term;
43     doc = -1;
44   }
45
46   @Override
47   public boolean next() throws IOException {
48     if (count == freq) {
49       if (!positions.next()) {
50         doc = Integer.MAX_VALUE;
51         return false;
52       }
53       doc = positions.doc();
54       freq = positions.freq();
55       count = 0;
56     }
57     position = positions.nextPosition();
58     count++;
59     return true;
60   }
61
62   @Override
63   public boolean skipTo(int target) throws IOException {
64     if (!positions.skipTo(target)) {
65       doc = Integer.MAX_VALUE;
66       return false;
67     }
68
69     doc = positions.doc();
70     freq = positions.freq();
71     count = 0;
72
73     position = positions.nextPosition();
74     count++;
75
76     return true;
77   }
78
79   @Override
80   public int doc() {
81     return doc;
82   }
83
84   @Override
85   public int start() {
86     return position;
87   }
88
89   @Override
90   public int end() {
91     return position + 1;
92   }
93
94   // TODO: Remove warning after API has been finalized
95   @Override
96   public Collection<byte[]> getPayload() throws IOException {
97     byte [] bytes = new byte[positions.getPayloadLength()]; 
98     bytes = positions.getPayload(bytes, 0);
99     return Collections.singletonList(bytes);
100   }
101
102   // TODO: Remove warning after API has been finalized
103   @Override
104   public boolean isPayloadAvailable() {
105     return positions.isPayloadAvailable();
106   }
107
108   @Override
109   public String toString() {
110     return "spans(" + term.toString() + ")@" +
111             (doc == -1 ? "START" : (doc == Integer.MAX_VALUE) ? "END" : doc + "-" + position);
112   }
113
114
115   public TermPositions getPositions() {
116     return positions;
117   }
118 }