1 package org.apache.lucene.util.fst;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.util.BytesRef;
24 /** Can next() and advance() through the terms in an FST
26 * @lucene.experimental
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;
34 public static class InputOutput<T> {
35 public BytesRef input;
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) {
44 result.input = current;
48 public InputOutput<T> current() {
52 public InputOutput<T> next() throws IOException {
53 //System.out.println(" enum.next");
58 /** Seeks to smallest term that's >= target. */
59 public InputOutput<T> seekCeil(BytesRef target) throws IOException {
61 targetLength = target.length;
66 /** Seeks to biggest term that's <= target. */
67 public InputOutput<T> seekFloor(BytesRef target) throws IOException {
69 targetLength = target.length;
75 protected int getTargetLabel() {
76 if (upto-1 == target.length) {
79 return target.bytes[target.offset + upto - 1] & 0xFF;
84 protected int getCurrentLabel() {
85 // current.offset fixed at 1
86 return current.bytes[upto] & 0xFF;
90 protected void setCurrentLabel(int label) {
91 current.bytes[upto] = (byte) label;
95 protected void grow() {
99 private InputOutput<T> setResult() {
103 current.length = upto-1;
104 result.output = output[upto];