pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / PhrasePositions.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import org.apache.lucene.index.*;
22
23 /**
24  * Position of a term in a document that takes into account the term offset within the phrase. 
25  */
26 final class PhrasePositions {
27   int doc;                                        // current doc
28   int position;                                   // position in doc
29   int count;                                      // remaining pos in this doc
30   int offset;                                     // position in phrase
31   final int ord;                                  // unique across all PhrasePositions instances
32   TermPositions tp;                               // stream of positions
33   PhrasePositions next;                           // used to make lists
34   PhrasePositions nextRepeating;                        // link to next repeating pp: standing for same term in different query offsets
35
36   PhrasePositions(TermPositions t, int o, int ord) {
37     tp = t;
38     offset = o;
39     this.ord = ord;
40   }
41
42   final boolean next() throws IOException {       // increments to next doc
43     if (!tp.next()) {
44       tp.close();                                 // close stream
45       doc = Integer.MAX_VALUE;                    // sentinel value
46       return false;
47     }
48     doc = tp.doc();
49     position = 0;
50     return true;
51   }
52
53   final boolean skipTo(int target) throws IOException {
54     if (!tp.skipTo(target)) {
55       tp.close();                                 // close stream
56       doc = Integer.MAX_VALUE;                    // sentinel value
57       return false;
58     }
59     doc = tp.doc();
60     position = 0;
61     return true;
62   }
63
64
65   final void firstPosition() throws IOException {
66     count = tp.freq();                            // read first pos
67     nextPosition();
68   }
69
70   /**
71    * Go to next location of this term current document, and set 
72    * <code>position</code> as <code>location - offset</code>, so that a 
73    * matching exact phrase is easily identified when all PhrasePositions 
74    * have exactly the same <code>position</code>.
75    */
76   final boolean nextPosition() throws IOException {
77     if (count-- > 0) {                            // read subsequent pos's
78       position = tp.nextPosition() - offset;
79       return true;
80     } else
81       return false;
82   }
83   
84   /** for debug purposes */
85   @Override
86   public String toString() {
87     String s = "d:"+doc+" o:"+offset+" p:"+position+" c:"+count;
88     if (nextRepeating!=null) {
89       s += " rpt[ "+nextRepeating+" ]";
90     }
91     return s;
92   }
93 }