Optimize imports

Reformatted code (Java convention; tab is 4 spaces)
This commit is contained in:
robert-bor 2016-11-30 12:07:03 +01:00
parent 5edf6d8126
commit a45df04a26
6 changed files with 128 additions and 131 deletions

View File

@ -7,4 +7,5 @@ public interface Intervalable extends Comparable {
public int getEnd();
public int size();
}

View File

@ -74,11 +74,11 @@ public class State {
return nextState(character, false);
}
public State nextStateIgnoreRootState(final Character character) {
public State nextStateIgnoreRootState(Character character) {
return nextState(character, true);
}
public State addState(final String keyword) {
public State addState(String keyword) {
State state = this;
for (final Character character : keyword.toCharArray()) {
@ -88,7 +88,7 @@ public class State {
return state;
}
public State addState(final Character character) {
public State addState(Character character) {
State nextState = nextStateIgnoreRootState(character);
if (nextState == null) {
nextState = new State(this.depth + 1);
@ -101,14 +101,14 @@ public class State {
return this.depth;
}
public void addEmit(final String keyword) {
public void addEmit(String keyword) {
if (this.emits == null) {
this.emits = new TreeSet<>();
}
this.emits.add(keyword);
}
public void addEmit(final Collection<String> emits) {
public void addEmit(Collection<String> emits) {
for (String emit : emits) {
addEmit(emit);
}

View File

@ -1,15 +1,17 @@
package org.ahocorasick.trie;
import static java.lang.Character.isWhitespace;
import org.ahocorasick.interval.IntervalTree;
import org.ahocorasick.interval.Intervalable;
import org.ahocorasick.trie.handler.DefaultEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import org.ahocorasick.interval.IntervalTree;
import org.ahocorasick.interval.Intervalable;
import org.ahocorasick.trie.handler.DefaultEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
import static java.lang.Character.isWhitespace;
/**
* Based on the Aho-Corasick white paper, Bell technologies:
@ -32,7 +34,6 @@ public class Trie {
* Used by the builder to add a text search keyword.
*
* @param keyword The search term to add to the list of search terms.
*
* @throws NullPointerException if the keyword is null.
*/
private void addKeyword(String keyword) {
@ -314,7 +315,8 @@ public class Trie {
/**
* Default (empty) constructor.
*/
private TrieBuilder() {}
private TrieBuilder() {
}
/**
* Configure the Trie to ignore case when searching for keywords in
@ -343,7 +345,6 @@ public class Trie {
* Adds a keyword to the Trie's list of text search keywords.
*
* @param keyword The keyword to add to the list.
*
* @return This builder.
* @throws NullPointerException if the keyword is null.
*/
@ -356,7 +357,6 @@ public class Trie {
* Adds a list of keywords to the Trie's list of text search keywords.
*
* @param keywords The keywords to add to the list.
*
* @return This builder.
*/
public TrieBuilder addKeywords(final String... keywords) {
@ -368,7 +368,6 @@ public class Trie {
* Adds a list of keywords to the Trie's list of text search keywords.
*
* @param keywords The keywords to add to the list.
*
* @return This builder.
*/
public TrieBuilder addKeywords(final Collection<String> keywords) {
@ -420,18 +419,16 @@ public class Trie {
}
/**
* @deprecated Use ignoreCase()
*
* @return This builder.
* @deprecated Use ignoreCase()
*/
public TrieBuilder caseInsensitive() {
return ignoreCase();
}
/**
* @deprecated Use ignoreOverlaps()
*
* @return This builder.
* @deprecated Use ignoreOverlaps()
*/
public TrieBuilder removeOverlaps() {
return ignoreOverlaps();

View File

@ -3,20 +3,20 @@ package org.ahocorasick.interval;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.sort;
import static junit.framework.Assert.assertEquals;
public class IntervalableComparatorByPositionTest {
@Test
public void sortOnPosition() {
List<Intervalable> intervals = new ArrayList<>();
List<Intervalable> intervals = new ArrayList<Intervalable>();
intervals.add(new Interval(4, 5));
intervals.add(new Interval(1, 4));
intervals.add(new Interval(3, 8));
sort(intervals, new IntervalableComparatorByPosition());
Collections.sort(intervals, new IntervalableComparatorByPosition());
assertEquals(4, intervals.get(0).size());
assertEquals(6, intervals.get(1).size());
assertEquals(2, intervals.get(2).size());

View File

@ -3,20 +3,20 @@ package org.ahocorasick.interval;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.sort;
import static junit.framework.Assert.assertEquals;
public class IntervalableComparatorBySizeTest {
@Test
public void sortOnSize() {
List<Intervalable> intervals = new ArrayList<>();
List<Intervalable> intervals = new ArrayList<Intervalable>();
intervals.add(new Interval(4, 5));
intervals.add(new Interval(1, 4));
intervals.add(new Interval(3, 8));
sort(intervals, new IntervalableComparatorBySize());
Collections.sort(intervals, new IntervalableComparatorBySize());
assertEquals(6, intervals.get(0).size());
assertEquals(4, intervals.get(1).size());
assertEquals(2, intervals.get(2).size());
@ -24,10 +24,10 @@ public class IntervalableComparatorBySizeTest {
@Test
public void sortOnSizeThenPosition() {
List<Intervalable> intervals = new ArrayList<>();
List<Intervalable> intervals = new ArrayList<Intervalable>();
intervals.add(new Interval(4, 7));
intervals.add(new Interval(2, 5));
sort(intervals, new IntervalableComparatorBySize());
Collections.sort(intervals, new IntervalableComparatorBySize());
assertEquals(2, intervals.get(0).getStart());
assertEquals(4, intervals.get(1).getStart());
}

View File

@ -7,10 +7,9 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import static java.util.concurrent.ThreadLocalRandom.current;
import static junit.framework.Assert.assertEquals;
import static org.ahocorasick.trie.Trie.builder;
import static org.junit.Assert.assertTrue;
public class TrieTest {
@ -36,7 +35,7 @@ public class TrieTest {
@Test
public void keywordAndTextAreTheSame() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword(ALPHABET[0])
.build();
Collection<Emit> emits = trie.parseText(ALPHABET[0]);
@ -46,7 +45,7 @@ public class TrieTest {
@Test
public void keywordAndTextAreTheSameFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword(ALPHABET[0])
.build();
Emit firstMatch = trie.firstMatch(ALPHABET[0]);
@ -55,7 +54,7 @@ public class TrieTest {
@Test
public void textIsLongerThanKeyword() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword(ALPHABET[0])
.build();
Collection<Emit> emits = trie.parseText(" " + ALPHABET[0]);
@ -65,7 +64,7 @@ public class TrieTest {
@Test
public void textIsLongerThanKeywordFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword(ALPHABET[0])
.build();
Emit firstMatch = trie.firstMatch(" " + ALPHABET[0]);
@ -74,7 +73,7 @@ public class TrieTest {
@Test
public void variousKeywordsOneMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(ALPHABET)
.build();
Collection<Emit> emits = trie.parseText("bcd");
@ -84,7 +83,7 @@ public class TrieTest {
@Test
public void variousKeywordsFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(ALPHABET)
.build();
Emit firstMatch = trie.firstMatch("bcd");
@ -93,7 +92,7 @@ public class TrieTest {
@Test
public void ushersTestAndStopOnHit() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(PRONOUNS)
.stopOnHit()
.build();
@ -106,7 +105,7 @@ public class TrieTest {
@Test
public void ushersTest() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(PRONOUNS)
.build();
Collection<Emit> emits = trie.parseText("ushers");
@ -119,7 +118,7 @@ public class TrieTest {
@Test
public void ushersTestWithCapitalKeywords() {
Trie trie = builder()
Trie trie = Trie.builder()
.ignoreCase()
.addKeyword("HERS")
.addKeyword("HIS")
@ -136,7 +135,7 @@ public class TrieTest {
@Test
public void ushersTestFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(PRONOUNS)
.build();
Emit firstMatch = trie.firstMatch("ushers");
@ -145,7 +144,7 @@ public class TrieTest {
@Test
public void ushersTestByCallback() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(PRONOUNS)
.build();
@ -167,7 +166,7 @@ public class TrieTest {
@Test
public void misleadingTest() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword("hers")
.build();
Collection<Emit> emits = trie.parseText("h he her hers");
@ -177,7 +176,7 @@ public class TrieTest {
@Test
public void misleadingTestFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword("hers")
.build();
Emit firstMatch = trie.firstMatch("h he her hers");
@ -186,7 +185,7 @@ public class TrieTest {
@Test
public void recipes() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(FOOD)
.build();
Collection<Emit> emits = trie.parseText("2 cauliflowers, 3 tomatoes, 4 slices of veal, 100g broccoli");
@ -199,7 +198,7 @@ public class TrieTest {
@Test
public void recipesFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(FOOD)
.build();
Emit firstMatch = trie.firstMatch("2 cauliflowers, 3 tomatoes, 4 slices of veal, 100g broccoli");
@ -209,7 +208,7 @@ public class TrieTest {
@Test
public void longAndShortOverlappingMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeyword("he")
.addKeyword("hehehehe")
.build();
@ -226,7 +225,7 @@ public class TrieTest {
@Test
public void nonOverlapping() {
Trie trie = builder().removeOverlaps()
Trie trie = Trie.builder().removeOverlaps()
.addKeyword("ab")
.addKeyword("cba")
.addKeyword("ababc")
@ -241,7 +240,7 @@ public class TrieTest {
@Test
public void nonOverlappingFirstMatch() {
Trie trie = builder().removeOverlaps()
Trie trie = Trie.builder().removeOverlaps()
.addKeyword("ab")
.addKeyword("cba")
.addKeyword("ababc")
@ -253,7 +252,7 @@ public class TrieTest {
@Test
public void containsMatch() {
Trie trie = builder().removeOverlaps()
Trie trie = Trie.builder().removeOverlaps()
.addKeyword("ab")
.addKeyword("cba")
.addKeyword("ababc")
@ -263,7 +262,7 @@ public class TrieTest {
@Test
public void startOfChurchillSpeech() {
Trie trie = builder().removeOverlaps()
Trie trie = Trie.builder().removeOverlaps()
.addKeyword("T")
.addKeyword("u")
.addKeyword("ur")
@ -281,7 +280,7 @@ public class TrieTest {
@Test
public void partialMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.onlyWholeWords()
.addKeyword("sugar")
.build();
@ -292,7 +291,7 @@ public class TrieTest {
@Test
public void partialMatchFirstMatch() {
Trie trie = builder()
Trie trie = Trie.builder()
.onlyWholeWords()
.addKeyword("sugar")
.build();
@ -303,7 +302,7 @@ public class TrieTest {
@Test
public void tokenizeFullSentence() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(GREEK_LETTERS)
.build();
Collection<Token> tokens = trie.tokenize("Hear: Alpha team first, Beta from the rear, Gamma in reserve");
@ -321,7 +320,7 @@ public class TrieTest {
// @see https://github.com/robert-bor/aho-corasick/issues/5
@Test
public void testStringIndexOutOfBoundsException() {
Trie trie = builder().ignoreCase().onlyWholeWords()
Trie trie = Trie.builder().ignoreCase().onlyWholeWords()
.addKeywords(UNICODE)
.build();
Collection<Emit> emits = trie.parseText("TurninG OnCe AgAiN BÖRKÜ");
@ -335,7 +334,7 @@ public class TrieTest {
@Test
public void testIgnoreCase() {
Trie trie = builder().ignoreCase()
Trie trie = Trie.builder().ignoreCase()
.addKeywords(UNICODE)
.build();
Collection<Emit> emits = trie.parseText("TurninG OnCe AgAiN BÖRKÜ");
@ -349,7 +348,7 @@ public class TrieTest {
@Test
public void testIgnoreCaseFirstMatch() {
Trie trie = builder().ignoreCase()
Trie trie = Trie.builder().ignoreCase()
.addKeywords(UNICODE)
.build();
Emit firstMatch = trie.firstMatch("TurninG OnCe AgAiN BÖRKÜ");
@ -359,7 +358,7 @@ public class TrieTest {
@Test
public void tokenizeTokensInSequence() {
Trie trie = builder()
Trie trie = Trie.builder()
.addKeywords(GREEK_LETTERS)
.build();
Collection<Token> tokens = trie.tokenize("Alpha Beta Gamma");
@ -369,7 +368,7 @@ public class TrieTest {
// @see https://github.com/robert-bor/aho-corasick/issues/7
@Test
public void testZeroLength() {
Trie trie = builder().ignoreOverlaps().onlyWholeWords().ignoreCase()
Trie trie = Trie.builder().ignoreOverlaps().onlyWholeWords().ignoreCase()
.addKeyword("")
.build();
trie.tokenize("Try a natural lip and subtle bronzer to keep all the focus on those big bright eyes with NARS Eyeshadow Duo in Rated R And the winner is... Boots No7 Advanced Renewal Anti-ageing Glycolic Peel Kit ($25 amazon.com) won most-appealing peel.");
@ -380,7 +379,7 @@ public class TrieTest {
public void testUnicode1() {
String target = "LİKE THIS"; // The second character ('İ') is Unicode, which was read by AC as a 2-byte char
assertEquals("THIS", target.substring(5, 9)); // Java does it the right way
Trie trie = builder().ignoreCase().onlyWholeWords()
Trie trie = Trie.builder().ignoreCase().onlyWholeWords()
.addKeyword("this")
.build();
Collection<Emit> emits = trie.parseText(target);
@ -393,7 +392,7 @@ public class TrieTest {
@Test
public void testUnicode2() {
String target = "LİKE THIS"; // The second character ('İ') is Unicode, which was read by AC as a 2-byte char
Trie trie = builder()
Trie trie = Trie.builder()
.ignoreCase()
.onlyWholeWords()
.addKeyword("this")
@ -405,7 +404,7 @@ public class TrieTest {
@Test
public void testPartialMatchWhiteSpaces() {
Trie trie = builder()
Trie trie = Trie.builder()
.onlyWholeWordsWhiteSpaceSeparated()
.addKeyword("#sugar-123")
.build();
@ -423,7 +422,7 @@ public class TrieTest {
injectKeyword(text, keyword, interval);
Trie trie = builder()
Trie trie = Trie.builder()
.onlyWholeWords()
.addKeyword(keyword)
.build();
@ -439,10 +438,10 @@ public class TrieTest {
* @param count The number of numbers to generate.
* @return A character sequence filled with random digits.
*/
private StringBuilder randomNumbers(final int count) {
private StringBuilder randomNumbers(int count) {
final StringBuilder sb = new StringBuilder(count);
for (int i = count - 1; i >= 0; i--) {
while (--count > 0) {
sb.append(randomInt(0, 10));
}
@ -468,7 +467,7 @@ public class TrieTest {
}
private int randomInt(final int min, final int max) {
return current().nextInt(min, max);
return ThreadLocalRandom.current().nextInt(min, max);
}
private void checkEmit(Emit next, int expectedStart, int expectedEnd, String expectedKeyword) {