-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
959 additions
and
58 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/EndOfRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ca.bc.gov.nrs.vdyp.io; | ||
|
||
public enum EndOfRecord { | ||
END_OF_RECORD | ||
} |
99 changes: 99 additions & 0 deletions
99
vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/GroupingStreamingParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package ca.bc.gov.nrs.vdyp.io.parse; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Wraps a StreamingParser and groups its entries. | ||
* | ||
* @author Kevin Smith, Vivid Solutions | ||
* | ||
* @param <T> | ||
* @param <U> | ||
*/ | ||
public abstract class GroupingStreamingParser<T, U> implements StreamingParser<T> { | ||
|
||
final StreamingParser<U> delegate; | ||
|
||
Optional<List<U>> next = Optional.empty(); | ||
|
||
public GroupingStreamingParser(StreamingParser<U> delegate) { | ||
super(); | ||
this.delegate = delegate; | ||
} | ||
|
||
/** | ||
* Returns true if a child should be skipped | ||
* | ||
* @param nextChild | ||
* @return | ||
*/ | ||
protected abstract boolean skip(U nextChild); | ||
|
||
/** | ||
* Returns true if a schild is an end of group marker | ||
* | ||
* @param nextChild | ||
* @return | ||
*/ | ||
protected abstract boolean stop(U nextChild); | ||
|
||
/** | ||
* Convert a list of children to a result. | ||
* | ||
* @param children | ||
* @return | ||
*/ | ||
protected abstract T convert(List<U> children); | ||
|
||
@Override | ||
public T next() throws IOException, ResourceParseException { | ||
|
||
doGetNext(); | ||
var children = next.orElseThrow(() -> new IllegalStateException("Requested next group when there is none")); | ||
next = Optional.empty(); | ||
return convert(children); | ||
} | ||
|
||
protected void doGetNext() throws IOException, ResourceParseException { | ||
|
||
if (next.isEmpty()) { | ||
var nextResult = new ArrayList<U>(); | ||
|
||
var nextChild = safeNextChild(); | ||
while (nextChild.map(x -> !stop(x)).orElse(false)) { | ||
nextResult.add(nextChild.get()); | ||
nextChild = safeNextChild(); | ||
} | ||
if (nextChild.isEmpty()) { | ||
return; | ||
} | ||
next = Optional.of(nextResult); | ||
|
||
} | ||
} | ||
|
||
protected Optional<U> safeNextChild() throws IOException, ResourceParseException { | ||
Optional<U> result = Optional.empty(); | ||
while (result.isEmpty() && delegate.hasNext()) { | ||
if (delegate.hasNext()) | ||
result = Optional.of(delegate.next()).filter(x -> !skip(x)); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() throws IOException, ResourceParseException { | ||
doGetNext(); | ||
|
||
return next.isPresent(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ca.bc.gov.nrs.vdyp.model; | ||
|
||
public enum Layer { | ||
PRIMARY, SECONDARY, VETERAN | ||
} |
215 changes: 215 additions & 0 deletions
215
vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/GroupingStreamingParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package ca.bc.gov.nrs.vdyp.io.parse; | ||
|
||
import static ca.bc.gov.nrs.vdyp.test.VdypMatchers.assertEmpty; | ||
import static ca.bc.gov.nrs.vdyp.test.VdypMatchers.assertNext; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.easymock.EasyMock; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class GroupingStreamingParserTest { | ||
|
||
@Test | ||
public void testEmpty() throws Exception { | ||
var control = EasyMock.createControl(); | ||
|
||
StreamingParser<Integer> delegate = control.createMock("delegate", StreamingParser.class); | ||
|
||
EasyMock.expect(delegate.hasNext()).andStubReturn(false); | ||
|
||
control.replay(); | ||
|
||
var unit = new GroupingStreamingParser<List<Integer>, Integer>(delegate) { | ||
|
||
@Override | ||
protected boolean skip(Integer nextChild) { | ||
return nextChild % 3 == 0; | ||
} | ||
|
||
@Override | ||
protected boolean stop(Integer nextChild) { | ||
return nextChild % 5 == 0; | ||
} | ||
|
||
@Override | ||
protected List<Integer> convert(List<Integer> children) { | ||
return children; | ||
} | ||
|
||
}; | ||
|
||
assertEmpty(unit); | ||
|
||
control.verify(); | ||
} | ||
|
||
@Test | ||
public void testOneEntry() throws Exception { | ||
var control = EasyMock.createControl(); | ||
|
||
var mock = Arrays.asList(1, 5).iterator(); | ||
|
||
StreamingParser<Integer> delegate = control.createMock("delegate", StreamingParser.class); | ||
|
||
EasyMock.expect(delegate.hasNext()).andStubAnswer(mock::hasNext); | ||
EasyMock.expect(delegate.next()).andStubAnswer(mock::next); | ||
|
||
control.replay(); | ||
|
||
var unit = new GroupingStreamingParser<List<Integer>, Integer>(delegate) { | ||
|
||
@Override | ||
protected boolean skip(Integer nextChild) { | ||
return nextChild % 3 == 0; | ||
} | ||
|
||
@Override | ||
protected boolean stop(Integer nextChild) { | ||
return nextChild % 5 == 0; | ||
} | ||
|
||
@Override | ||
protected List<Integer> convert(List<Integer> children) { | ||
return children; | ||
} | ||
|
||
}; | ||
|
||
var group = assertNext(unit); | ||
assertThat(group, contains(1)); | ||
|
||
assertEmpty(unit); | ||
|
||
control.verify(); | ||
} | ||
|
||
@Test | ||
public void testMultipleEntries() throws Exception { | ||
var control = EasyMock.createControl(); | ||
|
||
var mock = Arrays.asList(1, 2, 5).iterator(); | ||
|
||
StreamingParser<Integer> delegate = control.createMock("delegate", StreamingParser.class); | ||
|
||
EasyMock.expect(delegate.hasNext()).andStubAnswer(mock::hasNext); | ||
EasyMock.expect(delegate.next()).andStubAnswer(mock::next); | ||
|
||
control.replay(); | ||
|
||
var unit = new GroupingStreamingParser<List<Integer>, Integer>(delegate) { | ||
|
||
@Override | ||
protected boolean skip(Integer nextChild) { | ||
return nextChild % 3 == 0; | ||
} | ||
|
||
@Override | ||
protected boolean stop(Integer nextChild) { | ||
return nextChild % 5 == 0; | ||
} | ||
|
||
@Override | ||
protected List<Integer> convert(List<Integer> children) { | ||
return children; | ||
} | ||
|
||
}; | ||
|
||
var group = assertNext(unit); | ||
assertThat(group, contains(1, 2)); | ||
|
||
assertEmpty(unit); | ||
|
||
control.verify(); | ||
|
||
} | ||
|
||
@Test | ||
public void testMultipleGroups() throws Exception { | ||
var control = EasyMock.createControl(); | ||
|
||
var mock = Arrays.asList(1, 5, 2, 5).iterator(); | ||
|
||
StreamingParser<Integer> delegate = control.createMock("delegate", StreamingParser.class); | ||
|
||
EasyMock.expect(delegate.hasNext()).andStubAnswer(mock::hasNext); | ||
EasyMock.expect(delegate.next()).andStubAnswer(mock::next); | ||
|
||
control.replay(); | ||
|
||
var unit = new GroupingStreamingParser<List<Integer>, Integer>(delegate) { | ||
|
||
@Override | ||
protected boolean skip(Integer nextChild) { | ||
return nextChild % 3 == 0; | ||
} | ||
|
||
@Override | ||
protected boolean stop(Integer nextChild) { | ||
return nextChild % 5 == 0; | ||
} | ||
|
||
@Override | ||
protected List<Integer> convert(List<Integer> children) { | ||
return children; | ||
} | ||
|
||
}; | ||
|
||
var group = assertNext(unit); | ||
assertThat(group, contains(1)); | ||
|
||
group = assertNext(unit); | ||
assertThat(group, contains(2)); | ||
|
||
assertEmpty(unit); | ||
|
||
control.verify(); | ||
|
||
} | ||
|
||
@Test | ||
public void testSkip() throws Exception { | ||
var control = EasyMock.createControl(); | ||
|
||
var mock = Arrays.asList(3, 1, 5).iterator(); | ||
|
||
StreamingParser<Integer> delegate = control.createMock("delegate", StreamingParser.class); | ||
|
||
EasyMock.expect(delegate.hasNext()).andStubAnswer(mock::hasNext); | ||
EasyMock.expect(delegate.next()).andStubAnswer(mock::next); | ||
|
||
control.replay(); | ||
|
||
var unit = new GroupingStreamingParser<List<Integer>, Integer>(delegate) { | ||
|
||
@Override | ||
protected boolean skip(Integer nextChild) { | ||
return nextChild % 3 == 0; | ||
} | ||
|
||
@Override | ||
protected boolean stop(Integer nextChild) { | ||
return nextChild % 5 == 0; | ||
} | ||
|
||
@Override | ||
protected List<Integer> convert(List<Integer> children) { | ||
return children; | ||
} | ||
|
||
}; | ||
|
||
var group = assertNext(unit); | ||
assertThat(group, contains(1)); | ||
|
||
assertEmpty(unit); | ||
|
||
control.verify(); | ||
} | ||
|
||
} |
Oops, something went wrong.