Added missing override annotations. Added final modifier to Interval member variables. Updated documentation for ignoreCase (issue #33) and moved the ignore methods to the top of the builder to reflect their preferred calling order.

This commit is contained in:
djarvis 2016-11-29 22:34:55 -08:00
parent 2f1ec8d041
commit 267e895059
3 changed files with 38 additions and 26 deletions

View File

@ -2,8 +2,8 @@ package org.ahocorasick.interval;
public class Interval implements Intervalable {
private int start;
private int end;
private final int start;
private final int end;
/**
* Constructs an interval with a start and end position.
@ -21,6 +21,7 @@ public class Interval implements Intervalable {
*
* @return A number between 0 (start of text) and the text length.
*/
@Override
public int getStart() {
return this.start;
}
@ -30,6 +31,7 @@ public class Interval implements Intervalable {
*
* @return A number between getStart() + 1 and the text length.
*/
@Override
public int getEnd() {
return this.end;
}
@ -39,6 +41,7 @@ public class Interval implements Intervalable {
*
* @return The end position less the start position, plus one.
*/
@Override
public int size() {
return end - start + 1;
}
@ -47,6 +50,7 @@ public class Interval implements Intervalable {
* Answers whether the given interval overlaps this interval
* instance.
*
* @param other
* @return true The intervals overlap.
*/
public boolean overlapsWith(final Interval other) {

View File

@ -20,5 +20,4 @@ public class Emit extends Interval implements Intervalable {
public String toString() {
return super.toString() + "=" + this.keyword;
}
}

View File

@ -147,10 +147,17 @@ public class Trie {
}
}
/**
* The first matching text sequence.
*
* @param text The text to search for keywords.
* @return null if no matches found.
*/
public Emit firstMatch(final CharSequence text) {
if (!trieConfig.isAllowOverlaps()) {
// Slow path. Needs to find all the matches to detect overlaps.
Collection<Emit> parseText = parseText(text);
final Collection<Emit> parseText = parseText(text);
if (parseText != null && !parseText.isEmpty()) {
return parseText.iterator().next();
}
@ -170,7 +177,7 @@ public class Trie {
Collection<String> emitStrs = currentState.emit();
if (emitStrs != null && !emitStrs.isEmpty()) {
for (String emitStr : emitStrs) {
for (final String emitStr : emitStrs) {
final Emit emit = new Emit(position - emitStr.length() + 1, position, emitStr);
if (trieConfig.isOnlyWholeWords()) {
if (!isPartialMatch(text, emit)) {
@ -309,6 +316,29 @@ public class Trie {
*/
private TrieBuilder() {}
/**
* Configure the Trie to ignore case when searching for keywords in
* the text. This must be called before calling addKeyword because
* the algorithm converts keywords to lowercase as they are added,
* depending on this case sensitivity setting.
*
* @return This builder.
*/
public TrieBuilder ignoreCase() {
this.trieConfig.setCaseInsensitive(true);
return this;
}
/**
* Configure the Trie to ignore overlapping keywords.
*
* @return This builder.
*/
public TrieBuilder ignoreOverlaps() {
this.trieConfig.setAllowOverlaps(false);
return this;
}
/**
* Adds a keyword to the Trie's list of text search keywords.
*
@ -346,27 +376,6 @@ public class Trie {
return this;
}
/**
* Configure the Trie to ignore case when searching for keywords in
* the text.
*
* @return This builder.
*/
public TrieBuilder ignoreCase() {
this.trieConfig.setCaseInsensitive(true);
return this;
}
/**
* Configure the Trie to ignore overlapping keywords.
*
* @return This builder.
*/
public TrieBuilder ignoreOverlaps() {
this.trieConfig.setAllowOverlaps(false);
return this;
}
/**
* Configure the Trie to match whole keywords in the text.
*