Last active
January 18, 2017 00:49
-
-
Save sambatriste/d51cd479420795f68d0aa068198a9ab7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package hoge; | |
public interface ControlBreakPredicate<T> { | |
boolean shouldBreak(T previous, T current); | |
} |
This file contains hidden or 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
package hoge; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import nablarch.fw.DataReader; | |
import nablarch.fw.ExecutionContext; | |
public class GroupingRecordReader<T> implements DataReader<List<T>> { | |
/** 参照結果レコードのイテレータ */ | |
private final MyIterator<T> records; | |
private final ControlBreakPredicate<T> predicate; | |
public GroupingRecordReader(Iterator<T> records, ControlBreakPredicate<T> predicate) { | |
this.records = new MyIterator<>(records); | |
this.predicate = predicate; | |
} | |
private T previous; | |
private T current; | |
@Override | |
public synchronized List<T> read(ExecutionContext ctx) { | |
List<T> groupList = new ArrayList<>(); | |
if (!hasNext(ctx)) { | |
if (previous != null) { | |
groupList.add(previous); | |
previous = null; | |
return groupList; | |
} | |
return null; | |
} | |
try { | |
while (true) { | |
if (previous == null) { | |
previous = records.next(); | |
groupList.add(previous); | |
} | |
if (current == null) { | |
current = records.next(); | |
} | |
if (predicate.shouldBreak(previous, current)) { | |
previous = current; | |
return groupList; | |
} | |
groupList.add(current); | |
previous = current; | |
current = records.next(); | |
} | |
} catch (NoMoreRecord e) { | |
return groupList; | |
} | |
} | |
private static class MyIterator<T> implements Iterator<T> { | |
private final Iterator<T> delegate; | |
private MyIterator(Iterator<T> delegate) { | |
this.delegate = delegate; | |
} | |
@Override | |
public boolean hasNext() { | |
return delegate.hasNext(); | |
} | |
@Override | |
public T next() { | |
if (!delegate.hasNext()) { | |
throw new NoMoreRecord(); | |
} | |
return delegate.next(); | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
@Override | |
public synchronized boolean hasNext(ExecutionContext ctx) { | |
return records.hasNext(); | |
} | |
@Override | |
public synchronized void close(ExecutionContext ctx) { | |
} | |
} |
This file contains hidden or 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
package hoge; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import org.hamcrest.CoreMatchers; | |
import org.junit.Test; | |
import nablarch.fw.ExecutionContext; | |
public class GroupingRecordReaderTest { | |
private ControlBreakPredicate<Integer> controlBreakPredicate = new ControlBreakPredicate<Integer>() { | |
@Override | |
public boolean shouldBreak(Integer previous, Integer current) { | |
return previous < current; | |
} | |
}; | |
private final ExecutionContext ctx = new ExecutionContext(); | |
@Test | |
public void test() { | |
GroupingRecordReader<Integer> sut = createSut(1, 1, 2, 2, 2); | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(Arrays.asList(1, 1))); | |
} | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(Arrays.asList(2, 2, 2))); | |
} | |
} | |
@Test | |
public void test2() { | |
GroupingRecordReader<Integer> sut = createSut(1); | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(Arrays.asList(1))); | |
} | |
} | |
@Test | |
public void test3() { | |
GroupingRecordReader<Integer> sut = createSut(Collections.<Integer>emptyList()); | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(CoreMatchers.<List<Integer>>nullValue())); | |
} | |
} | |
@Test | |
public void test4() { | |
GroupingRecordReader<Integer> sut = createSut(1, 2, 3); | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(Arrays.asList(1))); | |
} | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(Arrays.asList(2))); | |
} | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(Arrays.asList(3))); | |
} | |
{ | |
List<Integer> group = sut.read(ctx); | |
assertThat(group, is(CoreMatchers.<List<Integer>>nullValue())); | |
} | |
} | |
private GroupingRecordReader<Integer> createSut(Integer... values) { | |
return createSut(Arrays.asList(values)); | |
} | |
private GroupingRecordReader<Integer> createSut(List<Integer> values) { | |
return new GroupingRecordReader<>(values.iterator(), controlBreakPredicate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment