JAMES-1781 Introduce FunctionGenerator A function generator is a function that produces an other function. We can compose the result of several FunctionGenerator so that they end up producing only one function.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/50e3ab58 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/50e3ab58 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/50e3ab58 Branch: refs/heads/master Commit: 50e3ab5844d6678c99210b08af588dfc69b1b24d Parents: 55116a7 Author: Benoit Tellier <[email protected]> Authored: Thu Jun 30 12:38:16 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Thu Sep 29 12:48:14 2016 +0200 ---------------------------------------------------------------------- .../apache/james/util/FunctionGenerator.java | 29 ++++++++++ .../james/util/FunctionGeneratorTest.java | 58 ++++++++++++++++++++ 2 files changed, 87 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/50e3ab58/server/container/util-java8/src/main/java/org/apache/james/util/FunctionGenerator.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/FunctionGenerator.java b/server/container/util-java8/src/main/java/org/apache/james/util/FunctionGenerator.java new file mode 100644 index 0000000..2342360 --- /dev/null +++ b/server/container/util-java8/src/main/java/org/apache/james/util/FunctionGenerator.java @@ -0,0 +1,29 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import java.util.function.Function; + +@FunctionalInterface +public interface FunctionGenerator<A, R> extends Function<A, Function<R, R>> { + default FunctionGenerator<A, R> composeGeneratedFunctions(FunctionGenerator<A, R> other) { + return a -> apply(a).compose(other.apply(a)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/50e3ab58/server/container/util-java8/src/test/java/org/apache/james/util/FunctionGeneratorTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/FunctionGeneratorTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/FunctionGeneratorTest.java new file mode 100644 index 0000000..9a7da21 --- /dev/null +++ b/server/container/util-java8/src/test/java/org/apache/james/util/FunctionGeneratorTest.java @@ -0,0 +1,58 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.function.Function; + +import org.junit.Test; + +public class FunctionGeneratorTest { + + @Test + public void composeGeneratedFunctionsShouldProduceTheCompositionOfBothOperations() { + FunctionGenerator<Integer, Integer> generateAddition = i -> (j -> j + i); + + Function<Integer, Integer> addSix = generateAddition.composeGeneratedFunctions(generateAddition).apply(3); + + assertThat(addSix.apply(42)).isEqualTo(48); + } + + @Test + public void composeGeneratedFunctionsShouldProduceTheCompositionOfBothOperationsWithDifferentArguments() { + FunctionGenerator<Integer, Integer> generateAddition = i -> (j -> j + i); + FunctionGenerator<Integer, Integer> generateDoubleAddition = i -> (j -> j + 2 * i); + + Function<Integer, Integer> addNine = generateAddition.composeGeneratedFunctions(generateDoubleAddition).apply(3); + + assertThat(addNine.apply(42)).isEqualTo(51); + } + + @Test + public void composeGeneratedFunctionsShouldReturnSameResultWhenOperationsCommute() { + FunctionGenerator<Integer, Integer> generateAddition = i -> (j -> j + i); + FunctionGenerator<Integer, Integer> generateDoubleAddition = i -> (j -> j + 2 * i); + + Function<Integer, Integer> addNine = generateDoubleAddition.composeGeneratedFunctions(generateAddition).apply(3); + + assertThat(addNine.apply(42)).isEqualTo(51); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
