From 267e8950592e685b0fc21c2cf6dad7db2775356a Mon Sep 17 00:00:00 2001 From: djarvis Date: Tue, 29 Nov 2016 22:34:55 -0800 Subject: [PATCH] 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. --- .../org/ahocorasick/interval/Interval.java | 8 ++- src/main/java/org/ahocorasick/trie/Emit.java | 1 - src/main/java/org/ahocorasick/trie/Trie.java | 55 +++++++++++-------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/ahocorasick/interval/Interval.java b/src/main/java/org/ahocorasick/interval/Interval.java index 50254cf..b6b756a 100644 --- a/src/main/java/org/ahocorasick/interval/Interval.java +++ b/src/main/java/org/ahocorasick/interval/Interval.java @@ -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) { diff --git a/src/main/java/org/ahocorasick/trie/Emit.java b/src/main/java/org/ahocorasick/trie/Emit.java index 60c1f9e..8c17253 100644 --- a/src/main/java/org/ahocorasick/trie/Emit.java +++ b/src/main/java/org/ahocorasick/trie/Emit.java @@ -20,5 +20,4 @@ public class Emit extends Interval implements Intervalable { public String toString() { return super.toString() + "=" + this.keyword; } - } diff --git a/src/main/java/org/ahocorasick/trie/Trie.java b/src/main/java/org/ahocorasick/trie/Trie.java index 88097a5..6df993d 100644 --- a/src/main/java/org/ahocorasick/trie/Trie.java +++ b/src/main/java/org/ahocorasick/trie/Trie.java @@ -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 parseText = parseText(text); + final Collection parseText = parseText(text); + if (parseText != null && !parseText.isEmpty()) { return parseText.iterator().next(); } @@ -170,7 +177,7 @@ public class Trie { Collection 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. *