Created
September 23, 2015 02:12
-
-
Save bassemZohdy/d1bddd8d9e5a3fbf566d to your computer and use it in GitHub Desktop.
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
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
public interface Router<T> { | |
Router<T> chain(Predicate<T> p, Consumer<T> c); | |
Consumer<T> toConsumer(); | |
} |
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
import java.util.Optional; | |
import java.util.function.BiFunction; | |
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
public interface RouterMatchAll<T> extends Consumer<T>, Router<T> { | |
static <T> RouterMatchAll<T> of(Predicate<T> p1, Consumer<T> c1) { | |
BiFunction<Predicate<T>, Consumer<T>, RouterMatchAll<T>> preFun = (p, c) -> i -> Optional | |
.ofNullable(i).filter(p).ifPresent(c); | |
return preFun.apply(p1, c1); | |
} | |
@Override | |
public default RouterMatchAll<T> chain(Predicate<T> p, Consumer<T> c) { | |
return this.andThen(RouterMatchAll.of(p, c))::accept; | |
} | |
@Override | |
public default Consumer<T> toConsumer() { | |
return this; | |
} | |
} |
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
import java.util.Optional; | |
import java.util.function.BiFunction; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
public interface RouterMatchOnce<T> extends Function<Optional<T>, Optional<T>>, | |
Router<T> { | |
static <T> RouterMatchOnce<T> of(Predicate<T> p1, Consumer<T> c1) { | |
BiFunction<Predicate<T>, Consumer<T>, RouterMatchOnce<T>> preFun = (p, | |
c) -> o -> { | |
if (o.filter(p).isPresent()) { | |
c.accept(o.get()); | |
return Optional.empty(); | |
} | |
return o; | |
}; | |
return preFun.apply(p1, c1); | |
} | |
@Override | |
public default RouterMatchOnce<T> chain(Predicate<T> p, Consumer<T> c) { | |
return this.andThen(RouterMatchOnce.of(p, c))::apply; | |
} | |
@Override | |
public default Consumer<T> toConsumer() { | |
return i -> this.apply(Optional.ofNullable(i)); | |
} | |
} |
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
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
import java.util.stream.IntStream; | |
public class RouterTest { | |
public static void main(String[] args) { | |
Predicate<Integer> p1 = (Integer i) -> { | |
System.out.println("call p1"); | |
return i < 5; | |
}; | |
Predicate<Integer> p2 = (Integer i) -> { | |
System.out.println("call p2"); | |
return i > 5; | |
}; | |
Consumer<Integer> c1 = i -> System.out.println("less"); | |
Consumer<Integer> c2 = i -> System.out.println("more"); | |
Router<Integer> routerMatchAll = RouterMatchAll.of(p1, c1) | |
.chain(p2, c2); | |
IntStream.range(0, 10).boxed().forEach(routerMatchAll.toConsumer()); | |
System.out.println(">>>>>>>>>>>>>>>>>>>>>>"); | |
Router<Integer> routerMatchOnce = RouterMatchOnce.of(p1, c1).chain(p2, | |
c2); | |
IntStream.range(0, 10).boxed().forEach(routerMatchOnce.toConsumer()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment