Created
August 19, 2021 03:40
-
-
Save amaembo/da5697947748416656bd293e36b6a82d to your computer and use it in GitHub Desktop.
StreamEx reducingWithZero demo
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
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