Example usage for org.apache.lucene.util.automaton Automaton createState

List of usage examples for org.apache.lucene.util.automaton Automaton createState

Introduction

In this page you can find the example usage for org.apache.lucene.util.automaton Automaton createState.

Prototype

public int createState() 

Source Link

Document

Create a new state.

Usage

From source file:com.lucene.MyPrefixQuery.java

License:Apache License

public static Automaton toAutomaton(BytesRef prefix) {
    final int numStatesAndTransitions = prefix.length + 1;
    final Automaton automaton = new Automaton(numStatesAndTransitions, numStatesAndTransitions);
    int lastState = automaton.createState();
    for (int i = 0; i < prefix.length; i++) {
        int state = automaton.createState();
        automaton.addTransition(lastState, state, prefix.bytes[prefix.offset + i] & 0xff);
        lastState = state;/*  w ww.ja  va 2  s  .co m*/
    }
    automaton.setAccept(lastState, true);
    automaton.addTransition(lastState, lastState, 0, 255);
    automaton.finishState();
    assert automaton.isDeterministic();
    return automaton;
}