Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Trie Serializable #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -78,8 +90,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Expand Down
27 changes: 22 additions & 5 deletions src/main/java/org/ahocorasick/interval/Interval.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.ahocorasick.interval;

public class Interval implements Intervalable {
import java.io.IOException;
import java.io.Serializable;

public class Interval implements Intervalable, Serializable {

private int start;
private int end;
Expand All @@ -10,21 +13,24 @@ public Interval(final int start, final int end) {
this.end = end;
}

@Override
public int getStart() {
return this.start;
}

@Override
public int getEnd() {
return this.end;
}

@Override
public int size() {
return end - start + 1;
}

public boolean overlapsWith(Interval other) {
return this.start <= other.getEnd() &&
this.end >= other.getStart();
this.end >= other.getStart();
}

public boolean overlapsWith(int point) {
Expand All @@ -36,9 +42,9 @@ public boolean equals(Object o) {
if (!(o instanceof Intervalable)) {
return false;
}
Intervalable other = (Intervalable)o;
Intervalable other = (Intervalable) o;
return this.start == other.getStart() &&
this.end == other.getEnd();
this.end == other.getEnd();
}

@Override
Expand All @@ -51,7 +57,7 @@ public int compareTo(Object o) {
if (!(o instanceof Intervalable)) {
return -1;
}
Intervalable other = (Intervalable)o;
Intervalable other = (Intervalable) o;
int comparison = this.start - other.getStart();
return comparison != 0 ? comparison : this.end - other.getEnd();
}
Expand All @@ -61,4 +67,15 @@ public String toString() {
return this.start + ":" + this.end;
}

protected void writeObject(java.io.ObjectOutputStream stream)
throws IOException {
stream.writeInt(start);
stream.writeInt(end);
}

protected void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
this.start = stream.readInt();
this.end = stream.readInt();
}
}
31 changes: 14 additions & 17 deletions src/main/java/org/ahocorasick/interval/IntervalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class IntervalNode {

private enum Direction { LEFT, RIGHT }

private IntervalNode left = null;
private IntervalNode right = null;
private int point;
private List<Intervalable> intervals = new ArrayList<Intervalable>();
private final int point;
private final List<Intervalable> intervals = new ArrayList<>();

public IntervalNode(List<Intervalable> intervals) {
this.point = determineMedian(intervals);

List<Intervalable> toLeft = new ArrayList<Intervalable>();
List<Intervalable> toRight = new ArrayList<Intervalable>();
List<Intervalable> toLeft = new ArrayList<>();
List<Intervalable> toRight = new ArrayList<>();

for (Intervalable interval : intervals) {
if (interval.getEnd() < this.point) {
Expand All @@ -37,7 +38,7 @@ public IntervalNode(List<Intervalable> intervals) {
}
}

public int determineMedian(List<Intervalable> intervals) {
private int determineMedian(List<Intervalable> intervals) {
int start = -1;
int end = -1;
for (Intervalable interval : intervals) {
Expand All @@ -55,7 +56,7 @@ public int determineMedian(List<Intervalable> intervals) {

public List<Intervalable> findOverlaps(Intervalable interval) {

List<Intervalable> overlaps = new ArrayList<Intervalable>();
List<Intervalable> overlaps = new ArrayList<>();

if (this.point < interval.getStart()) { // Tends to the right
addToOverlaps(interval, overlaps, findOverlappingRanges(this.right, interval));
Expand All @@ -72,25 +73,21 @@ public List<Intervalable> findOverlaps(Intervalable interval) {
return overlaps;
}

protected void addToOverlaps(Intervalable interval, List<Intervalable> overlaps, List<Intervalable> newOverlaps) {
for (Intervalable currentInterval : newOverlaps) {
if (!currentInterval.equals(interval)) {
overlaps.add(currentInterval);
}
}
private void addToOverlaps(Intervalable interval, List<Intervalable> overlaps, List<Intervalable> newOverlaps) {
overlaps.addAll(newOverlaps.stream().filter(currentInterval -> !currentInterval.equals(interval)).collect(Collectors.toList()));
}

protected List<Intervalable> checkForOverlapsToTheLeft(Intervalable interval) {
private List<Intervalable> checkForOverlapsToTheLeft(Intervalable interval) {
return checkForOverlaps(interval, Direction.LEFT);
}

protected List<Intervalable> checkForOverlapsToTheRight(Intervalable interval) {
private List<Intervalable> checkForOverlapsToTheRight(Intervalable interval) {
return checkForOverlaps(interval, Direction.RIGHT);
}

protected List<Intervalable> checkForOverlaps(Intervalable interval, Direction direction) {
private List<Intervalable> checkForOverlaps(Intervalable interval, Direction direction) {

List<Intervalable> overlaps = new ArrayList<Intervalable>();
List<Intervalable> overlaps = new ArrayList<>();
for (Intervalable currentInterval : this.intervals) {
switch (direction) {
case LEFT :
Expand All @@ -109,7 +106,7 @@ protected List<Intervalable> checkForOverlaps(Intervalable interval, Direction d
}


protected List<Intervalable> findOverlappingRanges(IntervalNode node, Intervalable interval) {
private List<Intervalable> findOverlappingRanges(IntervalNode node, Intervalable interval) {
if (node != null) {
return node.findOverlaps(interval);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/ahocorasick/interval/IntervalTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public List<Intervalable> removeOverlaps(List<Intervalable> intervals) {
// Sort the intervals on size, then left-most position
Collections.sort(intervals, new IntervalableComparatorBySize());

Set<Intervalable> removeIntervals = new TreeSet<Intervalable>();
Set<Intervalable> removeIntervals = new TreeSet<>();

for (Intervalable interval : intervals) {
// If the interval was already removed, ignore it
Expand All @@ -31,9 +31,7 @@ public List<Intervalable> removeOverlaps(List<Intervalable> intervals) {
}

// Remove all intervals that were overlapping
for (Intervalable removeInterval : removeIntervals) {
intervals.remove(removeInterval);
}
removeIntervals.forEach(intervals::remove);

// Sort the intervals, now on left-most position only
Collections.sort(intervals, new IntervalableComparatorByPosition());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/ahocorasick/interval/Intervalable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public interface Intervalable extends Comparable {

public int getStart();
public int getEnd();
public int size();
int getStart();
int getEnd();
int size();

}
21 changes: 20 additions & 1 deletion src/main/java/org/ahocorasick/trie/Emit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import org.ahocorasick.interval.Interval;
import org.ahocorasick.interval.Intervalable;

public class Emit extends Interval implements Intervalable {
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;

public class Emit extends Interval implements Intervalable, Serializable {

private final String keyword;

Expand All @@ -21,4 +25,19 @@ public String toString() {
return super.toString() + "=" + this.keyword;
}

@Override
protected void writeObject(java.io.ObjectOutputStream stream)
throws IOException {
super.writeObject(stream);
stream.writeUTF(keyword);
}

@Override
protected void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
Field f = this.getClass().getDeclaredField("keyword");
super.readObject(stream);
f.setAccessible(true);
f.set(this, stream.readUTF());
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/ahocorasick/trie/MatchToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class MatchToken extends Token {

private Emit emit;
private final Emit emit;

public MatchToken(String fragment, Emit emit) {
super(fragment);
Expand Down
Loading