add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / util / fst / BytesRefFSTEnum.java
1 package org.apache.lucene.util.fst;
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
22 import org.apache.lucene.util.BytesRef;
23
24 /** Can next() and advance() through the terms in an FST
25  *
26  * @lucene.experimental
27 */
28
29 public final class BytesRefFSTEnum<T> extends FSTEnum<T> {
30   private final BytesRef current = new BytesRef(10);
31   private final InputOutput<T> result = new InputOutput<T>();
32   private BytesRef target;
33
34   public static class InputOutput<T> {
35     public BytesRef input;
36     public T output;
37   }
38
39   /** doFloor controls the behavior of advance: if it's true
40    *  doFloor is true, advance positions to the biggest
41    *  term before target.  */
42   public BytesRefFSTEnum(FST<T> fst) {
43     super(fst);
44     result.input = current;
45     current.offset = 1;
46   }
47
48   public InputOutput<T> current() {
49     return result;
50   }
51
52   public InputOutput<T> next() throws IOException {
53     //System.out.println("  enum.next");
54     doNext();
55     return setResult();
56   }
57
58   /** Seeks to smallest term that's >= target. */
59   public InputOutput<T> seekCeil(BytesRef target) throws IOException {
60     this.target = target;
61     targetLength = target.length;
62     super.doSeekCeil();
63     return setResult();
64   }
65
66   /** Seeks to biggest term that's <= target. */
67   public InputOutput<T> seekFloor(BytesRef target) throws IOException {
68     this.target = target;
69     targetLength = target.length;
70     super.doSeekFloor();
71     return setResult();
72   }
73
74   @Override
75   protected int getTargetLabel() {
76     if (upto-1 == target.length) {
77       return FST.END_LABEL;
78     } else {
79       return target.bytes[target.offset + upto - 1] & 0xFF;
80     }
81   }
82
83   @Override
84   protected int getCurrentLabel() {
85     // current.offset fixed at 1
86     return current.bytes[upto] & 0xFF;
87   }
88
89   @Override
90   protected void setCurrentLabel(int label) {
91     current.bytes[upto] = (byte) label;
92   }
93
94   @Override
95   protected void grow() {
96     current.grow(upto+1);
97   }
98
99   private InputOutput<T> setResult() {
100     if (upto == 0) {
101       return null;
102     } else {
103       current.length = upto-1;
104       result.output = output[upto];
105       return result;
106     }
107   }
108 }