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 267e895059
commit 255069624b
13 changed files with 200 additions and 185 deletions

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.ahocorasick</groupId> <groupId>org.ahocorasick</groupId>

View File

@ -67,7 +67,7 @@ public class Interval implements Intervalable {
if (!(o instanceof Intervalable)) { if (!(o instanceof Intervalable)) {
return false; return false;
} }
Intervalable other = (Intervalable)o; Intervalable other = (Intervalable) o;
return this.start == other.getStart() && return this.start == other.getStart() &&
this.end == other.getEnd(); this.end == other.getEnd();
} }
@ -82,7 +82,7 @@ public class Interval implements Intervalable {
if (!(o instanceof Intervalable)) { if (!(o instanceof Intervalable)) {
return -1; return -1;
} }
Intervalable other = (Intervalable)o; Intervalable other = (Intervalable) o;
int comparison = this.start - other.getStart(); int comparison = this.start - other.getStart();
return comparison != 0 ? comparison : this.end - other.getEnd(); return comparison != 0 ? comparison : this.end - other.getEnd();
} }

View File

@ -6,7 +6,7 @@ import java.util.List;
public class IntervalNode { public class IntervalNode {
private enum Direction { LEFT, RIGHT } private enum Direction {LEFT, RIGHT}
private IntervalNode left = null; private IntervalNode left = null;
private IntervalNode right = null; private IntervalNode right = null;
@ -93,12 +93,12 @@ public class IntervalNode {
List<Intervalable> overlaps = new ArrayList<Intervalable>(); List<Intervalable> overlaps = new ArrayList<Intervalable>();
for (Intervalable currentInterval : this.intervals) { for (Intervalable currentInterval : this.intervals) {
switch (direction) { switch (direction) {
case LEFT : case LEFT:
if (currentInterval.getStart() <= interval.getEnd()) { if (currentInterval.getStart() <= interval.getEnd()) {
overlaps.add(currentInterval); overlaps.add(currentInterval);
} }
break; break;
case RIGHT : case RIGHT:
if (currentInterval.getEnd() >= interval.getStart()) { if (currentInterval.getEnd() >= interval.getStart()) {
overlaps.add(currentInterval); overlaps.add(currentInterval);
} }

View File

@ -3,7 +3,9 @@ package org.ahocorasick.interval;
public interface Intervalable extends Comparable { public interface Intervalable extends Comparable {
public int getStart(); public int getStart();
public int getEnd(); public int getEnd();
public int size(); public int size();
} }

View File

@ -6,7 +6,7 @@ import java.util.*;
* <p> * <p>
* A state has various important tasks it must attend to: * A state has various important tasks it must attend to:
* </p> * </p>
* * <p>
* <ul> * <ul>
* <li>success; when a character points to another state, it must return that state</li> * <li>success; when a character points to another state, it must return that state</li>
* <li>failure; when a character has no matching state, the algorithm must be able to fall back on a * <li>failure; when a character has no matching state, the algorithm must be able to fall back on a
@ -14,7 +14,7 @@ import java.util.*;
* <li>emits; when this state is passed and keywords have been matched, the matches must be * <li>emits; when this state is passed and keywords have been matched, the matches must be
* 'emitted' so that they can be used later on.</li> * 'emitted' so that they can be used later on.</li>
* </ul> * </ul>
* * <p>
* <p> * <p>
* The root state is special in the sense that it has no failure state; it cannot fail. If it 'fails' * The root state is special in the sense that it has no failure state; it cannot fail. If it 'fails'
* it will still parse the next character and start from the root node. This ensures that the algorithm * it will still parse the next character and start from the root node. This ensures that the algorithm
@ -25,22 +25,30 @@ import java.util.*;
*/ */
public class State { public class State {
/** effective the size of the keyword */ /**
* effective the size of the keyword
*/
private final int depth; private final int depth;
/** only used for the root state to refer to itself in case no matches have been found */ /**
* only used for the root state to refer to itself in case no matches have been found
*/
private final State rootState; private final State rootState;
/** /**
* referred to in the white paper as the 'goto' structure. From a state it is possible to go * referred to in the white paper as the 'goto' structure. From a state it is possible to go
* to other states, depending on the character passed. * to other states, depending on the character passed.
*/ */
private final Map<Character,State> success = new HashMap<>(); private final Map<Character, State> success = new HashMap<>();
/** if no matching states are found, the failure state will be returned */ /**
* if no matching states are found, the failure state will be returned
*/
private State failure; private State failure;
/** whenever this state is reached, it will emit the matches keywords for future reference */ /**
* whenever this state is reached, it will emit the matches keywords for future reference
*/
private Set<String> emits; private Set<String> emits;
public State() { public State() {
@ -70,7 +78,7 @@ public class State {
return nextState(character, true); return nextState(character, true);
} }
public State addState( String keyword ) { public State addState(String keyword) {
State state = this; State state = this;
for (final Character character : keyword.toCharArray()) { for (final Character character : keyword.toCharArray()) {
@ -83,7 +91,7 @@ public class State {
public State addState(Character character) { public State addState(Character character) {
State nextState = nextStateIgnoreRootState(character); State nextState = nextStateIgnoreRootState(character);
if (nextState == null) { if (nextState == null) {
nextState = new State(this.depth+1); nextState = new State(this.depth + 1);
this.success.put(character, nextState); this.success.put(character, nextState);
} }
return nextState; return nextState;
@ -107,7 +115,7 @@ public class State {
} }
public Collection<String> emit() { public Collection<String> emit() {
return this.emits == null ? Collections.<String> emptyList() : this.emits; return this.emits == null ? Collections.<String>emptyList() : this.emits;
} }
public State failure() { public State failure() {

View File

@ -1,15 +1,17 @@
package org.ahocorasick.trie; 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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
import org.ahocorasick.interval.IntervalTree;
import org.ahocorasick.interval.Intervalable; import static java.lang.Character.isWhitespace;
import org.ahocorasick.trie.handler.DefaultEmitHandler;
import org.ahocorasick.trie.handler.EmitHandler;
/** /**
* Based on the Aho-Corasick white paper, Bell technologies: * Based on the Aho-Corasick white paper, Bell technologies:
@ -32,15 +34,14 @@ public class Trie {
* Used by the builder to add a text search keyword. * Used by the builder to add a text search keyword.
* *
* @param keyword The search term to add to the list of search terms. * @param keyword The search term to add to the list of search terms.
*
* @throws NullPointerException if the keyword is null. * @throws NullPointerException if the keyword is null.
*/ */
private void addKeyword(String keyword) { private void addKeyword(String keyword) {
if( keyword.isEmpty() ) { if (keyword.isEmpty()) {
return; return;
} }
if( isCaseInsensitive() ) { if (isCaseInsensitive()) {
keyword = keyword.toLowerCase(); keyword = keyword.toLowerCase();
} }
@ -52,9 +53,9 @@ public class Trie {
* *
* @param keywords List of search term to add to the list of search terms. * @param keywords List of search term to add to the list of search terms.
*/ */
private void addKeywords( final String[] keywords ) { private void addKeywords(final String[] keywords) {
for( final String keyword : keywords ) { for (final String keyword : keywords) {
addKeyword( keyword ); addKeyword(keyword);
} }
} }
@ -63,9 +64,9 @@ public class Trie {
* *
* @param keywords List of search term to add to the list of search terms. * @param keywords List of search term to add to the list of search terms.
*/ */
private void addKeywords( final Collection<String> keywords ) { private void addKeywords(final Collection<String> keywords) {
for( final String keyword : keywords ) { for (final String keyword : keywords) {
addKeyword( keyword ); addKeyword(keyword);
} }
} }
@ -95,11 +96,11 @@ public class Trie {
} }
private Token createFragment(final Emit emit, final String text, final int lastCollectedPosition) { private Token createFragment(final Emit emit, final String text, final 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) {
return new MatchToken(text.substring(emit.getStart(), emit.getEnd()+1), emit); return new MatchToken(text.substring(emit.getStart(), emit.getEnd() + 1), emit);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -118,7 +119,7 @@ public class Trie {
} }
if (!trieConfig.isAllowOverlaps()) { if (!trieConfig.isAllowOverlaps()) {
IntervalTree intervalTree = new IntervalTree((List<Intervalable>)(List<?>)collectedEmits); IntervalTree intervalTree = new IntervalTree((List<Intervalable>) (List<?>) collectedEmits);
intervalTree.removeOverlaps((List<Intervalable>) (List<?>) collectedEmits); intervalTree.removeOverlaps((List<Intervalable>) (List<?>) collectedEmits);
} }
@ -314,7 +315,8 @@ public class Trie {
/** /**
* Default (empty) constructor. * Default (empty) constructor.
*/ */
private TrieBuilder() {} private TrieBuilder() {
}
/** /**
* Configure the Trie to ignore case when searching for keywords in * 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. * Adds a keyword to the Trie's list of text search keywords.
* *
* @param keyword The keyword to add to the list. * @param keyword The keyword to add to the list.
*
* @return This builder. * @return This builder.
* @throws NullPointerException if the keyword is null. * @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. * Adds a list of keywords to the Trie's list of text search keywords.
* *
* @param keywords The keywords to add to the list. * @param keywords The keywords to add to the list.
*
* @return This builder. * @return This builder.
*/ */
public TrieBuilder addKeywords(final String... keywords) { 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. * Adds a list of keywords to the Trie's list of text search keywords.
* *
* @param keywords The keywords to add to the list. * @param keywords The keywords to add to the list.
*
* @return This builder. * @return This builder.
*/ */
public TrieBuilder addKeywords(final Collection<String> keywords) { public TrieBuilder addKeywords(final Collection<String> keywords) {
@ -420,18 +419,16 @@ public class Trie {
} }
/** /**
* @deprecated Use ignoreCase()
*
* @return This builder. * @return This builder.
* @deprecated Use ignoreCase()
*/ */
public TrieBuilder caseInsensitive() { public TrieBuilder caseInsensitive() {
return ignoreCase(); return ignoreCase();
} }
/** /**
* @deprecated Use ignoreOverlaps()
*
* @return This builder. * @return This builder.
* @deprecated Use ignoreOverlaps()
*/ */
public TrieBuilder removeOverlaps() { public TrieBuilder removeOverlaps() {
return ignoreOverlaps(); return ignoreOverlaps();

View File

@ -12,9 +12,13 @@ public class TrieConfig {
private boolean stopOnHit = false; private boolean stopOnHit = false;
public boolean isStopOnHit() { return stopOnHit; } public boolean isStopOnHit() {
return stopOnHit;
}
public void setStopOnHit(boolean stopOnHit) { this.stopOnHit = stopOnHit; } public void setStopOnHit(boolean stopOnHit) {
this.stopOnHit = stopOnHit;
}
public boolean isAllowOverlaps() { public boolean isAllowOverlaps() {
return allowOverlaps; return allowOverlaps;
@ -32,7 +36,9 @@ public class TrieConfig {
this.onlyWholeWords = onlyWholeWords; this.onlyWholeWords = onlyWholeWords;
} }
public boolean isOnlyWholeWordsWhiteSpaceSeparated() { return onlyWholeWordsWhiteSpaceSeparated; } public boolean isOnlyWholeWordsWhiteSpaceSeparated() {
return onlyWholeWordsWhiteSpaceSeparated;
}
public void setOnlyWholeWordsWhiteSpaceSeparated(boolean onlyWholeWordsWhiteSpaceSeparated) { public void setOnlyWholeWordsWhiteSpaceSeparated(boolean onlyWholeWordsWhiteSpaceSeparated) {
this.onlyWholeWordsWhiteSpaceSeparated = onlyWholeWordsWhiteSpaceSeparated; this.onlyWholeWordsWhiteSpaceSeparated = onlyWholeWordsWhiteSpaceSeparated;

View File

@ -2,29 +2,29 @@ package org.ahocorasick.interval;
import org.junit.Test; import org.junit.Test;
import java.util.*; import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.*;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
public class IntervalTest { public class IntervalTest {
@Test @Test
public void construct() { public void construct() {
Interval i = new Interval(1,3); Interval i = new Interval(1, 3);
assertEquals(1, i.getStart()); assertEquals(1, i.getStart());
assertEquals(3, i.getEnd()); assertEquals(3, i.getEnd());
} }
@Test @Test
public void size() { public void size() {
assertEquals(3, new Interval(0,2).size()); assertEquals(3, new Interval(0, 2).size());
} }
@Test @Test
public void intervaloverlaps() { public void intervaloverlaps() {
assertTrue(new Interval(1,3).overlapsWith(new Interval(2,4))); assertTrue(new Interval(1, 3).overlapsWith(new Interval(2, 4)));
} }
@Test @Test
@ -34,7 +34,7 @@ public class IntervalTest {
@Test @Test
public void pointOverlaps() { public void pointOverlaps() {
assertTrue(new Interval(1,3).overlapsWith(2)); assertTrue(new Interval(1, 3).overlapsWith(2));
} }
@Test @Test

View File

@ -20,7 +20,7 @@ public class IntervalTreeTest {
intervals.add(new Interval(4, 6)); intervals.add(new Interval(4, 6));
intervals.add(new Interval(5, 7)); intervals.add(new Interval(5, 7));
IntervalTree intervalTree = new IntervalTree(intervals); IntervalTree intervalTree = new IntervalTree(intervals);
List<Intervalable> overlaps = intervalTree.findOverlaps(new Interval(1,3)); List<Intervalable> overlaps = intervalTree.findOverlaps(new Interval(1, 3));
assertEquals(3, overlaps.size()); assertEquals(3, overlaps.size());
Iterator<Intervalable> overlapsIt = overlaps.iterator(); Iterator<Intervalable> overlapsIt = overlaps.iterator();
assertOverlap(overlapsIt.next(), 2, 4); assertOverlap(overlapsIt.next(), 2, 4);

View File

@ -13,9 +13,9 @@ public class IntervalableComparatorByPositionTest {
@Test @Test
public void sortOnPosition() { public void sortOnPosition() {
List<Intervalable> intervals = new ArrayList<Intervalable>(); List<Intervalable> intervals = new ArrayList<Intervalable>();
intervals.add(new Interval(4,5)); intervals.add(new Interval(4, 5));
intervals.add(new Interval(1,4)); intervals.add(new Interval(1, 4));
intervals.add(new Interval(3,8)); intervals.add(new Interval(3, 8));
Collections.sort(intervals, new IntervalableComparatorByPosition()); Collections.sort(intervals, new IntervalableComparatorByPosition());
assertEquals(4, intervals.get(0).size()); assertEquals(4, intervals.get(0).size());
assertEquals(6, intervals.get(1).size()); assertEquals(6, intervals.get(1).size());

View File

@ -13,9 +13,9 @@ public class IntervalableComparatorBySizeTest {
@Test @Test
public void sortOnSize() { public void sortOnSize() {
List<Intervalable> intervals = new ArrayList<Intervalable>(); List<Intervalable> intervals = new ArrayList<Intervalable>();
intervals.add(new Interval(4,5)); intervals.add(new Interval(4, 5));
intervals.add(new Interval(1,4)); intervals.add(new Interval(1, 4));
intervals.add(new Interval(3,8)); intervals.add(new Interval(3, 8));
Collections.sort(intervals, new IntervalableComparatorBySize()); Collections.sort(intervals, new IntervalableComparatorBySize());
assertEquals(6, intervals.get(0).size()); assertEquals(6, intervals.get(0).size());
assertEquals(4, intervals.get(1).size()); assertEquals(4, intervals.get(1).size());
@ -25,8 +25,8 @@ public class IntervalableComparatorBySizeTest {
@Test @Test
public void sortOnSizeThenPosition() { public void sortOnSizeThenPosition() {
List<Intervalable> intervals = new ArrayList<Intervalable>(); List<Intervalable> intervals = new ArrayList<Intervalable>();
intervals.add(new Interval(4,7)); intervals.add(new Interval(4, 7));
intervals.add(new Interval(2,5)); intervals.add(new Interval(2, 5));
Collections.sort(intervals, new IntervalableComparatorBySize()); Collections.sort(intervals, new IntervalableComparatorBySize());
assertEquals(2, intervals.get(0).getStart()); assertEquals(2, intervals.get(0).getStart());
assertEquals(4, intervals.get(1).getStart()); assertEquals(4, intervals.get(1).getStart());

View File

@ -1,6 +1,5 @@
package org.ahocorasick.trie; package org.ahocorasick.trie;
import org.ahocorasick.trie.State;
import org.junit.Test; import org.junit.Test;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;

View File

@ -1,14 +1,16 @@
package org.ahocorasick.trie; package org.ahocorasick.trie;
import org.ahocorasick.trie.handler.EmitHandler;
import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import org.ahocorasick.trie.handler.EmitHandler;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TrieTest { public class TrieTest {
private final static String[] ALPHABET = new String[]{ private final static String[] ALPHABET = new String[]{
@ -406,7 +408,7 @@ public class TrieTest {
.onlyWholeWordsWhiteSpaceSeparated() .onlyWholeWordsWhiteSpaceSeparated()
.addKeyword("#sugar-123") .addKeyword("#sugar-123")
.build(); .build();
Collection < Emit > emits = trie.parseText("#sugar-123 #sugar-1234"); // left, middle, right test Collection<Emit> emits = trie.parseText("#sugar-123 #sugar-1234"); // left, middle, right test
assertEquals(1, emits.size()); // Match must not be made assertEquals(1, emits.size()); // Match must not be made
checkEmit(emits.iterator().next(), 0, 9, "#sugar-123"); checkEmit(emits.iterator().next(), 0, 9, "#sugar-123");
} }
@ -415,19 +417,19 @@ public class TrieTest {
public void testLargeString() { public void testLargeString() {
final int interval = 100; final int interval = 100;
final int textSize = 1000000; final int textSize = 1000000;
final String keyword = FOOD[ 1 ]; final String keyword = FOOD[1];
final StringBuilder text = randomNumbers( textSize ); final StringBuilder text = randomNumbers(textSize);
injectKeyword( text, keyword, interval ); injectKeyword(text, keyword, interval);
Trie trie = Trie.builder() Trie trie = Trie.builder()
.onlyWholeWords() .onlyWholeWords()
.addKeyword( keyword ) .addKeyword(keyword)
.build(); .build();
final Collection<Emit> emits = trie.parseText( text ); final Collection<Emit> emits = trie.parseText(text);
assertEquals( textSize / interval, emits.size() ); assertEquals(textSize / interval, emits.size());
} }
/** /**
@ -436,11 +438,11 @@ public class TrieTest {
* @param count The number of numbers to generate. * @param count The number of numbers to generate.
* @return A character sequence filled with random digits. * @return A character sequence filled with random digits.
*/ */
private StringBuilder randomNumbers( int count ) { private StringBuilder randomNumbers(int count) {
final StringBuilder sb = new StringBuilder( count ); final StringBuilder sb = new StringBuilder(count);
while( --count > 0 ) { while (--count > 0) {
sb.append( randomInt( 0, 10 ) ); sb.append(randomInt(0, 10));
} }
return sb; return sb;
@ -457,15 +459,15 @@ public class TrieTest {
private void injectKeyword( private void injectKeyword(
final StringBuilder source, final StringBuilder source,
final String keyword, final String keyword,
final int interval ) { final int interval) {
final int length = source.length(); final int length = source.length();
for( int i = 0; i < length; i += interval ) { for (int i = 0; i < length; i += interval) {
source.replace( i, i + keyword.length(), keyword ); source.replace(i, i + keyword.length(), keyword);
} }
} }
private int randomInt( final int min, final int max ) { private int randomInt(final int min, final int max) {
return ThreadLocalRandom.current().nextInt( min, max ); return ThreadLocalRandom.current().nextInt(min, max);
} }
private void checkEmit(Emit next, int expectedStart, int expectedEnd, String expectedKeyword) { private void checkEmit(Emit next, int expectedStart, int expectedEnd, String expectedKeyword) {