Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created August 19, 2021 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaembo/da5697947748416656bd293e36b6a82d to your computer and use it in GitHub Desktop.
Save amaembo/da5697947748416656bd293e36b6a82d to your computer and use it in GitHub Desktop.
StreamEx reducingWithZero demo
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import java.util.stream.Collector;
class Test {
enum MyLattice {
/*
TOP
/ \
V1 V2
\ /
BOTTOM
*/
TOP, V1, V2, BOTTOM;
MyLattice join(MyLattice other) {
if (this == BOTTOM) return other;
if (other == BOTTOM) return this;
if (this == other) return this;
return TOP;
}
static Collector<MyLattice, ?, MyLattice> joining() {
return MoreCollectors.reducingWithZero(
TOP, BOTTOM, MyLattice::join);
}
}
public static void main(String[] args) {
MyLattice result = StreamEx.of("BOTTOM", "V1", "V2", "V1", "V1", "TOP")
.peek(e -> System.out.println("Processing " + e))
.map(MyLattice::valueOf)
.collect(MyLattice.joining());
System.out.println("Result: " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment