Express paramaters as final
This commit is contained in:
parent
82d36386fd
commit
360d76193a
@ -2,7 +2,7 @@ package org.ahocorasick.trie;
|
|||||||
|
|
||||||
public class FragmentToken extends Token {
|
public class FragmentToken extends Token {
|
||||||
|
|
||||||
public FragmentToken(String fragment) {
|
public FragmentToken(final String fragment) {
|
||||||
super(fragment);
|
super(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,14 +23,14 @@ package org.ahocorasick.trie;
|
|||||||
*/
|
*/
|
||||||
public class Keyword implements Comparable {
|
public class Keyword implements Comparable {
|
||||||
private final String text;
|
private final String text;
|
||||||
private int depth;
|
private final int depth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create portion of potential match
|
* Create portion of potential match
|
||||||
* @param text content that matches
|
* @param text content that matches
|
||||||
* @param depth count of prior source tokens that comprise the match
|
* @param depth count of prior source tokens that comprise the match
|
||||||
*/
|
*/
|
||||||
public Keyword(String text, int depth) {
|
public Keyword(final String text, final int depth) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.depth = depth;
|
this.depth = depth;
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ public class Keyword implements Comparable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Object o) {
|
public int compareTo(final Object o) {
|
||||||
if (o instanceof Keyword) {
|
if (o instanceof Keyword) {
|
||||||
return text.compareTo(((Keyword) o).text);
|
return text.compareTo(((Keyword) o).text);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ public class MatchToken extends Token {
|
|||||||
|
|
||||||
private final Emit emit;
|
private final Emit emit;
|
||||||
|
|
||||||
public MatchToken(String fragment, Emit emit) {
|
public MatchToken(final String fragment, final Emit emit) {
|
||||||
super(fragment);
|
super(fragment);
|
||||||
this.emit = emit;
|
this.emit = emit;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,7 +54,7 @@ public class State {
|
|||||||
this.rootState = depth == 0 ? this : null;
|
this.rootState = depth == 0 ? this : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private State nextState(Transition transition, boolean ignoreRootState) {
|
private State nextState(final Transition transition, boolean ignoreRootState) {
|
||||||
State nextState = this.success.get(transition);
|
State nextState = this.success.get(transition);
|
||||||
if (!ignoreRootState && nextState == null && this.rootState != null) {
|
if (!ignoreRootState && nextState == null && this.rootState != null) {
|
||||||
nextState = this.rootState;
|
nextState = this.rootState;
|
||||||
@ -62,15 +62,15 @@ public class State {
|
|||||||
return nextState;
|
return nextState;
|
||||||
}
|
}
|
||||||
|
|
||||||
public State nextState(Transition transition) {
|
public State nextState(final Transition transition) {
|
||||||
return nextState(transition, false);
|
return nextState(transition, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public State nextStateIgnoreRootState(Transition transition) {
|
public State nextStateIgnoreRootState(final Transition transition) {
|
||||||
return nextState(transition, true);
|
return nextState(transition, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public State addState(Transition transition) {
|
public State addState(final Transition transition) {
|
||||||
State nextState = nextStateIgnoreRootState(transition);
|
State nextState = nextStateIgnoreRootState(transition);
|
||||||
if (nextState == null) {
|
if (nextState == null) {
|
||||||
nextState = new State(this.depth+1);
|
nextState = new State(this.depth+1);
|
||||||
@ -83,20 +83,20 @@ public class State {
|
|||||||
return this.depth;
|
return this.depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEmit(Keyword keyword) {
|
public void addEmit(final Keyword keyword) {
|
||||||
if (this.emits == null) {
|
if (this.emits == null) {
|
||||||
this.emits = new TreeSet<>();
|
this.emits = new TreeSet<>();
|
||||||
}
|
}
|
||||||
this.emits.add(keyword);
|
this.emits.add(keyword);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEmit(Collection<Keyword> emits) {
|
public void addEmit(final Collection<Keyword> emits) {
|
||||||
for (Keyword emit : emits) {
|
for (Keyword emit : emits) {
|
||||||
addEmit(emit);
|
addEmit(emit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEmitString(String key) {
|
public void addEmitString(final String key) {
|
||||||
addEmit(new Keyword(key, getDepth()));
|
addEmit(new Keyword(key, getDepth()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ public class State {
|
|||||||
return this.emits == null ? Collections.<Keyword> emptyList() : this.emits;
|
return this.emits == null ? Collections.<Keyword> emptyList() : this.emits;
|
||||||
}
|
}
|
||||||
|
|
||||||
public State failure(EmitCandidateFlushHandler emitCandidateFlushHandler) {
|
public State failure(final EmitCandidateFlushHandler emitCandidateFlushHandler) {
|
||||||
if (emitCandidateFlushHandler != null && this.failure.isRootState()) {
|
if (emitCandidateFlushHandler != null && this.failure.isRootState()) {
|
||||||
emitCandidateFlushHandler.flush();
|
emitCandidateFlushHandler.flush();
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ public class State {
|
|||||||
return failure(null);
|
return failure(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFailure(State failState) {
|
public void setFailure(final State failState) {
|
||||||
this.failure = failState;
|
this.failure = failState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ public abstract class Token {
|
|||||||
|
|
||||||
private final String fragment;
|
private final String fragment;
|
||||||
|
|
||||||
public Token(String fragment) {
|
public Token(final String fragment) {
|
||||||
this.fragment = fragment;
|
this.fragment = fragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ public class Tokenizer {
|
|||||||
|
|
||||||
private final String text;
|
private final String text;
|
||||||
|
|
||||||
public Tokenizer(Collection<Emit> emits, String text) {
|
public Tokenizer(final Collection<Emit> emits, final String text) {
|
||||||
this.emits = emits;
|
this.emits = emits;
|
||||||
this.text = text;
|
this.text = text;
|
||||||
}
|
}
|
||||||
@ -34,7 +34,9 @@ public class Tokenizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Token createFragment(Emit emit, String text, int lastCollectedPosition) {
|
private Token createFragment(Emit emit, String text, int lastCollectedPosition) {
|
||||||
return new FragmentToken(text.substring(lastCollectedPosition+1, emit == null ? text.length() : emit.getStart()));
|
return new FragmentToken(text.substring(
|
||||||
|
lastCollectedPosition+1,
|
||||||
|
emit == null ? text.length() : emit.getStart()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Token createMatch(Emit emit, String text) {
|
private Token createMatch(Emit emit, String text) {
|
||||||
|
|||||||
@ -28,7 +28,7 @@ public class Transition<T> {
|
|||||||
protected final int start;
|
protected final int start;
|
||||||
protected final int length;
|
protected final int length;
|
||||||
|
|
||||||
public Transition(T token, int start, int length) {
|
public Transition(final T token, int start, int length) {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
@ -56,7 +56,7 @@ public class Transition<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(final Object obj) {
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,16 +24,16 @@ public class Trie {
|
|||||||
|
|
||||||
private final State rootState;
|
private final State rootState;
|
||||||
|
|
||||||
private Trie(TrieConfig trieConfig) {
|
private Trie(final TrieConfig trieConfig) {
|
||||||
this.trieConfig = trieConfig;
|
this.trieConfig = trieConfig;
|
||||||
this.rootState = new State();
|
this.rootState = new State();
|
||||||
}
|
}
|
||||||
|
|
||||||
private abstract class KeywordTokenizer {
|
private abstract class KeywordTokenizer {
|
||||||
protected int position = 0;
|
protected int position = 0;
|
||||||
protected CharSequence input;
|
protected final CharSequence input;
|
||||||
protected int length;
|
protected int length;
|
||||||
protected KeywordTokenizer(CharSequence input) {
|
protected KeywordTokenizer(final CharSequence input) {
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.length = input.length();
|
this.length = input.length();
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ public class Trie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class WordTokenizer extends KeywordTokenizer {
|
private class WordTokenizer extends KeywordTokenizer {
|
||||||
public WordTokenizer(CharSequence input) {
|
public WordTokenizer(final CharSequence input) {
|
||||||
super(input);
|
super(input);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
@ -66,7 +66,7 @@ public class Trie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class CharacterTokenizer extends KeywordTokenizer {
|
private class CharacterTokenizer extends KeywordTokenizer {
|
||||||
public CharacterTokenizer(CharSequence input) {
|
public CharacterTokenizer(final CharSequence input) {
|
||||||
super(input);
|
super(input);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
@ -84,7 +84,7 @@ public class Trie {
|
|||||||
private final KeywordTokenizer tokenizer;
|
private final KeywordTokenizer tokenizer;
|
||||||
private final StringBuilder input;
|
private final StringBuilder input;
|
||||||
|
|
||||||
public TokenStream(CharSequence text) {
|
public TokenStream(final CharSequence text) {
|
||||||
input = new StringBuilder(text.length());
|
input = new StringBuilder(text.length());
|
||||||
for (int p = 0; p < text.length(); ++p) {
|
for (int p = 0; p < text.length(); ++p) {
|
||||||
char ch = text.charAt(p);
|
char ch = text.charAt(p);
|
||||||
@ -109,7 +109,7 @@ public class Trie {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addKeyword(CharSequence keyword) {
|
private void addKeyword(final CharSequence keyword) {
|
||||||
if (keyword == null || keyword.length() == 0) {
|
if (keyword == null || keyword.length() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -128,24 +128,24 @@ public class Trie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Collection<Emit> parseText(CharSequence text) {
|
public Collection<Emit> parseText(final CharSequence text) {
|
||||||
DefaultEmitHandler emitHandler = new DefaultEmitHandler();
|
DefaultEmitHandler emitHandler = new DefaultEmitHandler();
|
||||||
parseText(text, emitHandler);
|
parseText(text, emitHandler);
|
||||||
return emitHandler.getEmits();
|
return emitHandler.getEmits();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsMatch(CharSequence text) {
|
public boolean containsMatch(final CharSequence text) {
|
||||||
Emit firstMatch = firstMatch(text);
|
Emit firstMatch = firstMatch(text);
|
||||||
return firstMatch != null;
|
return firstMatch != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Emit firstMatch(CharSequence text) {
|
public Emit firstMatch(final CharSequence text) {
|
||||||
FirstMatchHandler emitHandler = new FirstMatchHandler();
|
FirstMatchHandler emitHandler = new FirstMatchHandler();
|
||||||
parseText(text, emitHandler);
|
parseText(text, emitHandler);
|
||||||
return emitHandler.getFirstMatch();
|
return emitHandler.getFirstMatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void parseText(CharSequence text, EmitHandler emitHandler) {
|
public void parseText(final CharSequence text, final EmitHandler emitHandler) {
|
||||||
|
|
||||||
final EmitCandidateHolder emitCandidateHolder = this.trieConfig.isAllowOverlaps() ?
|
final EmitCandidateHolder emitCandidateHolder = this.trieConfig.isAllowOverlaps() ?
|
||||||
new OverlappingEmitCandidateHolder() :
|
new OverlappingEmitCandidateHolder() :
|
||||||
@ -181,11 +181,14 @@ public class Trie {
|
|||||||
flushHandler.flush();
|
flushHandler.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private State getState(State currentState, Transition transition, EmitCandidateFlushHandler flushHandler) {
|
private State getState(final State currentState,
|
||||||
|
final Transition transition,
|
||||||
|
final EmitCandidateFlushHandler flushHandler) {
|
||||||
|
State failState = currentState;
|
||||||
State newCurrentState = currentState.nextState(transition);
|
State newCurrentState = currentState.nextState(transition);
|
||||||
while (newCurrentState == null) {
|
while (newCurrentState == null) {
|
||||||
currentState = currentState.failure(flushHandler);
|
failState = failState.failure(flushHandler);
|
||||||
newCurrentState = currentState.nextState(transition);
|
newCurrentState = failState.nextState(transition);
|
||||||
}
|
}
|
||||||
return newCurrentState;
|
return newCurrentState;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,7 @@ public class WordTransition extends Transition<String> {
|
|||||||
* @param word to match
|
* @param word to match
|
||||||
* @param start position of first character within the source string
|
* @param start position of first character within the source string
|
||||||
*/
|
*/
|
||||||
public WordTransition(String word, int start) {
|
public WordTransition(final String word, int start) {
|
||||||
super(word, start, word.length());
|
super(word, start, word.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ public class WordTransition extends Transition<String> {
|
|||||||
* Create a transition without regard for position
|
* Create a transition without regard for position
|
||||||
* @param word to match
|
* @param word to match
|
||||||
*/
|
*/
|
||||||
public WordTransition(String word) {
|
public WordTransition(final String word) {
|
||||||
this(word, 0);
|
this(word, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user