From d6ead54649c7e759881b9ab329cc8b2d1bbeca56 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 12 Dec 2017 22:39:11 +0000 Subject: [PATCH] Add Before combinator --- .../net/kemitix/mon/combinator/Before.java | 78 +++++++++++++++++++ .../kemitix/mon/combinator/package-info.java | 22 ++++++ .../kemitix/mon/combinator/BeforeTest.java | 40 ++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/main/java/net/kemitix/mon/combinator/Before.java create mode 100644 src/main/java/net/kemitix/mon/combinator/package-info.java create mode 100644 src/test/java/net/kemitix/mon/combinator/BeforeTest.java diff --git a/src/main/java/net/kemitix/mon/combinator/Before.java b/src/main/java/net/kemitix/mon/combinator/Before.java new file mode 100644 index 0000000..2b854b7 --- /dev/null +++ b/src/main/java/net/kemitix/mon/combinator/Before.java @@ -0,0 +1,78 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon.combinator; + +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * Before pattern comninator. + * + *

Original from http://boundsofjava.com/newsletter/003-introducing-combinators-part1

+ * + * @param the argument type + * @param the result type + * + * @author Federico Peralta Schaffner (fps@boundsofjava.com) + */ +@FunctionalInterface +public interface Before extends + Function< + Consumer, + Function< + Function, + Function>> { + + /** + * Decorates a function with a Consumer that will be supplier with the argument before applying it to the function. + * + * @param before the consumer that will receive the argument before the function + * @param function the function to apply the argument to and return the result value of + * @param the argument type + * @param the result type + * + * @return a partially applied Function that will take an argument and return the result of applying it to the + * function parameter + */ + static Function decorate( + final Consumer before, + final Function function + ) { + return Before.create().apply(before) + .apply(function); + } + + /** + * Create a Before curried function. + * + * @param the argument type + * @param the result type + * + * @return a curried function that will pass the argument to before applying the supplied function + */ + static Before create() { + return before -> function -> argument -> { + before.accept(argument); + return function.apply(argument); + }; + } +} diff --git a/src/main/java/net/kemitix/mon/combinator/package-info.java b/src/main/java/net/kemitix/mon/combinator/package-info.java new file mode 100644 index 0000000..9ff7a6d --- /dev/null +++ b/src/main/java/net/kemitix/mon/combinator/package-info.java @@ -0,0 +1,22 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon.combinator; diff --git a/src/test/java/net/kemitix/mon/combinator/BeforeTest.java b/src/test/java/net/kemitix/mon/combinator/BeforeTest.java new file mode 100644 index 0000000..859ae6f --- /dev/null +++ b/src/test/java/net/kemitix/mon/combinator/BeforeTest.java @@ -0,0 +1,40 @@ +package net.kemitix.mon.combinator; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BeforeTest { + + @Test + public void canCreateBeforeCombinator() { + //given + final List events = new ArrayList<>(); + final Function squareDecorated = + Before.decorate(v -> before(v, events), i -> function(i, events)); + //when + final Integer result = squareDecorated.apply(2); + //then + assertThat(result).isEqualTo(4); + assertThat(events).containsExactly("before", "function"); + } + + private static void before( + final Integer v, + final List events + ) { + events.add("before"); + } + + private static Integer function( + final Integer i, + final List events + ) { + events.add("function"); + return i * i; + } +}