-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JUnit 5.10.0 + Nahara Toolkit Pipeline a.k.a "Stream API at home"
- Loading branch information
Showing
14 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
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
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,18 @@ | ||
plugins { | ||
} | ||
|
||
eclipse { | ||
project.name = "Nahara Toolkit - Common - Pipeline" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} |
42 changes: 42 additions & 0 deletions
42
common/pipeline/src/main/java/nahara/common/pipeline/Pipeline.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,42 @@ | ||
package nahara.common.pipeline; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* <p>Represent a pipeline that takes in inputs and yield out outputs.</p> | ||
* <p>Pipelines can be used to filter and transform data, similar to {@link Stream}. I do not know why I made | ||
* this (well ok to be fair I think reusing pipeline transformation could reduce memory usage), but it could | ||
* be useful for someone. Only time will tell I guess?</p> | ||
* @param <I> Input type. | ||
* @param <O> Output type. | ||
*/ | ||
public interface Pipeline<I, O> { | ||
public Iterator<O> iteratorOf(Iterator<I> input); | ||
|
||
default Iterator<O> iteratorOf(I input) { | ||
return iteratorOf(Collections.singleton(input).iterator()); | ||
} | ||
|
||
default Iterator<O> iteratorOf(Iterable<I> input) { | ||
return iteratorOf(input.iterator()); | ||
} | ||
|
||
default Optional<O> firstNonnullOf(Iterator<I> input) { | ||
Iterator<O> iter = iteratorOf(input); | ||
|
||
while (iter.hasNext()) { | ||
O obj = iter.next(); | ||
if (obj == null) continue; | ||
return Optional.of(obj); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
default Optional<O> firstNonnullOf(I input) { | ||
return firstNonnullOf(Collections.singleton(input).iterator()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
common/pipeline/src/main/java/nahara/common/pipeline/PipelineBuilder.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,19 @@ | ||
package nahara.common.pipeline; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import nahara.common.pipeline.impl.PipelineBuilderImpl; | ||
import nahara.common.pipeline.impl.StartOfPipelineImpl; | ||
|
||
public interface PipelineBuilder<I, O> { | ||
public PipelineBuilder<I, O> filter(Predicate<O> predicate); | ||
public <M> PipelineBuilder<I, M> map(Function<O, M> mapper); | ||
|
||
// Create and build | ||
public Pipeline<I, O> build(); | ||
|
||
public static <T> PipelineBuilder<T, T> create() { | ||
return new PipelineBuilderImpl<>(new StartOfPipelineImpl<>()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
common/pipeline/src/main/java/nahara/common/pipeline/impl/FilterPipelineImpl.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,56 @@ | ||
package nahara.common.pipeline.impl; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Predicate; | ||
|
||
import nahara.common.pipeline.Pipeline; | ||
|
||
public class FilterPipelineImpl<I, O> implements Pipeline<I, O> { | ||
private Pipeline<I, O> previous; | ||
private Predicate<O> predicate; | ||
|
||
public FilterPipelineImpl(Pipeline<I, O> previous, Predicate<O> predicate) { | ||
this.previous = previous; | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public Iterator<O> iteratorOf(Iterator<I> input) { | ||
return new Iterator<O>() { | ||
private O nextObj = null; | ||
private boolean holdingNextObj = false, isEnded = false; | ||
private Iterator<O> prevIter = previous.iteratorOf(input); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (isEnded) return false; | ||
if (!holdingNextObj) { | ||
while (prevIter.hasNext()) { | ||
O obj = prevIter.next(); | ||
if (!predicate.test(obj)) continue; | ||
|
||
holdingNextObj = true; | ||
nextObj = obj; | ||
return true; | ||
} | ||
|
||
isEnded = true; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public O next() { | ||
if (!holdingNextObj) { | ||
if (isEnded || !hasNext()) throw new NoSuchElementException(); | ||
} | ||
|
||
holdingNextObj = false; | ||
return nextObj; | ||
} | ||
}; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
common/pipeline/src/main/java/nahara/common/pipeline/impl/MapPipelineImpl.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,53 @@ | ||
package nahara.common.pipeline.impl; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Function; | ||
|
||
import nahara.common.pipeline.Pipeline; | ||
|
||
public class MapPipelineImpl<I, M, O> implements Pipeline<I, O> { | ||
private Pipeline<I, M> previous; | ||
private Function<M, O> mapper; | ||
|
||
public MapPipelineImpl(Pipeline<I, M> previous, Function<M, O> mapper) { | ||
this.previous = previous; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public Iterator<O> iteratorOf(Iterator<I> input) { | ||
return new Iterator<O>() { | ||
private O nextObj = null; | ||
private boolean holdingNextObj = false, isEnded = false; | ||
private Iterator<M> prevIter = previous.iteratorOf(input); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (isEnded) return false; | ||
if (!holdingNextObj) { | ||
if (!prevIter.hasNext()) { | ||
isEnded = true; | ||
return false; | ||
} | ||
|
||
holdingNextObj = true; | ||
nextObj = mapper.apply(prevIter.next()); | ||
return true; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public O next() { | ||
if (!holdingNextObj) { | ||
if (isEnded || !hasNext()) throw new NoSuchElementException(); | ||
} | ||
|
||
holdingNextObj = false; | ||
return nextObj; | ||
} | ||
}; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
common/pipeline/src/main/java/nahara/common/pipeline/impl/PipelineBuilderImpl.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,31 @@ | ||
package nahara.common.pipeline.impl; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import nahara.common.pipeline.Pipeline; | ||
import nahara.common.pipeline.PipelineBuilder; | ||
|
||
public class PipelineBuilderImpl<I, O> implements PipelineBuilder<I, O> { | ||
private Pipeline<I, O> current; | ||
|
||
public PipelineBuilderImpl(Pipeline<I, O> current) { | ||
this.current = current; | ||
} | ||
|
||
@Override | ||
public PipelineBuilder<I, O> filter(Predicate<O> predicate) { | ||
current = new FilterPipelineImpl<>(current, predicate); | ||
return this; | ||
} | ||
|
||
@Override | ||
public <M> PipelineBuilder<I, M> map(Function<O, M> mapper) { | ||
return new PipelineBuilderImpl<>(new MapPipelineImpl<>(current, mapper)); | ||
} | ||
|
||
@Override | ||
public Pipeline<I, O> build() { | ||
return current; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
common/pipeline/src/main/java/nahara/common/pipeline/impl/StartOfPipelineImpl.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,12 @@ | ||
package nahara.common.pipeline.impl; | ||
|
||
import java.util.Iterator; | ||
|
||
import nahara.common.pipeline.Pipeline; | ||
|
||
public class StartOfPipelineImpl<I> implements Pipeline<I, I> { | ||
@Override | ||
public Iterator<I> iteratorOf(Iterator<I> input) { | ||
return input; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
common/pipeline/src/test/java/nahara/common/pipeline/PipelineTest.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,18 @@ | ||
package nahara.common.pipeline; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class PipelineTest { | ||
@Test | ||
void testMapping() { | ||
var pipeline = PipelineBuilder.<String>create() | ||
.map(v -> Integer.parseInt(v)) | ||
.map(v -> v + 1) | ||
.filter(v -> v != 0) | ||
.build(); | ||
assertEquals(124, pipeline.firstNonnullOf("123").orElseThrow()); | ||
assertTrue(pipeline.firstNonnullOf("-1").isEmpty()); | ||
} | ||
} |
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
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