pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / instantiated / src / java / org / apache / lucene / store / instantiated / InstantiatedTermPositions.java
1 package org.apache.lucene.store.instantiated;
2
3 /**
4  * Copyright 2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.lucene.index.TermPositions;
20
21 import java.io.IOException;
22
23 /**
24  * A {@link org.apache.lucene.index.TermPositions} navigating an {@link InstantiatedIndexReader}.
25  */
26 public class InstantiatedTermPositions
27     extends InstantiatedTermDocs
28     implements TermPositions {
29
30   public int getPayloadLength() {
31     return currentDocumentInformation.getPayloads()[currentTermPositionIndex].length;
32   }
33
34   public byte[] getPayload(byte[] data, int offset) throws IOException {
35     byte[] payloads = currentDocumentInformation.getPayloads()[currentTermPositionIndex];
36
37     // read payloads lazily
38     if (data == null || data.length - offset < getPayloadLength()) {
39       // the array is too small to store the payload data,
40       return payloads;
41     } else {
42       System.arraycopy(payloads, 0, data, offset, payloads.length);
43       return data;
44     }
45   }
46
47   public boolean isPayloadAvailable() {
48     return currentDocumentInformation.getPayloads()[currentTermPositionIndex] != null;
49   }
50
51   public InstantiatedTermPositions(InstantiatedIndexReader reader) {
52     super(reader);
53   }
54
55   /**
56    * Returns next position in the current document.  It is an error to call
57    * this more than {@link #freq()} times
58    * without calling {@link #next()}<p> This is
59    * invalid until {@link #next()} is called for
60    * the first time.
61    */
62   public int nextPosition() {
63     currentTermPositionIndex++;
64     // if you get an array out of index exception here,
65     // it might be due to currentDocumentInformation.getIndexFromTerm not being set!!
66     return currentDocumentInformation.getTermPositions()[currentTermPositionIndex];
67   }
68
69   private int currentTermPositionIndex;
70
71   /**
72    * Moves to the next pair in the enumeration.
73    * <p> Returns true if there is such a next pair in the enumeration.
74    */
75   @Override
76   public boolean next() {
77     currentTermPositionIndex = -1;
78     return super.next();
79   }
80
81   /**
82    * Skips entries to the first beyond the current whose document number is
83    * greater than or equal to <currentTermPositionIndex>target</currentTermPositionIndex>. <p>Returns true iff there is such
84    * an entry.  <p>Behaves as if written: <pre>
85    *   boolean skipTo(int target) {
86    *     do {
87    *       if (!next())
88    *         return false;
89    *     } while (target > doc());
90    *     return true;
91    *   }
92    * </pre>
93    * Some implementations are considerably more efficient than that.
94    */
95   @Override
96   public boolean skipTo(int target) {
97     currentTermPositionIndex = -1;
98     return super.skipTo(target);
99   }
100 }