diff --git a/actor-tests/src/test/java/org/apache/pekko/util/ConstantFunJavaTest.java b/actor-tests/src/test/java/org/apache/pekko/util/ConstantFunJavaTest.java new file mode 100644 index 00000000000..dd0d08977f3 --- /dev/null +++ b/actor-tests/src/test/java/org/apache/pekko/util/ConstantFunJavaTest.java @@ -0,0 +1,46 @@ +/* + * 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.pekko.util; + +import org.apache.pekko.japi.function.Function; +import org.junit.Assert; +import org.junit.Test; + +public class ConstantFunJavaTest { + @Test + public void alwaysReturnSameValue() { + final Object value = new Object(); + final Function fun = ConstantFun.javaIdentityFunction(); + try { + Assert.assertTrue(fun.apply(value) == value); + } catch (Exception e) { + Assert.fail("Exception thrown: " + e.getMessage()); + } + } + + @Test + public void alwaysReturnFunctionInstance() { + final Function funObj = ConstantFun.javaIdentityFunction(); + final Function funStr = ConstantFun.javaIdentityFunction(); + try { + Assert.assertTrue(funObj.equals(funStr)); + } catch (Exception e) { + Assert.fail("Exception thrown: " + e.getMessage()); + } + } +} diff --git a/actor/src/main/scala/org/apache/pekko/japi/function/Function.scala b/actor/src/main/scala/org/apache/pekko/japi/function/Function.scala index 2c7da12c3a5..3e0713ea94d 100644 --- a/actor/src/main/scala/org/apache/pekko/japi/function/Function.scala +++ b/actor/src/main/scala/org/apache/pekko/japi/function/Function.scala @@ -13,6 +13,8 @@ package org.apache.pekko.japi.function +import org.apache.pekko.util.ConstantFun + import scala.annotation.nowarn /** @@ -28,6 +30,15 @@ trait Function[-T, +R] extends java.io.Serializable { def apply(param: T): R } +object Function { + + /** + * Returns a `Function` that always returns its input argument. + * @since 1.2.0 + */ + def identity[T]: Function[T, T] = ConstantFun.javaIdentityFunction +} + /** * A Function interface. Used to create 2-arg first-class-functions is Java. * `Serializable` is needed to be able to grab line number for Java 8 lambdas. diff --git a/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/FlowTest.java b/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/FlowTest.java index 70a6aa98c60..29f3397b3ee 100644 --- a/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/FlowTest.java +++ b/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/FlowTest.java @@ -750,9 +750,7 @@ public void mustBeAbleToUseConcatAllWithSources() throws Exception { mainInputs.add(Source.from(input2)); final Flow, List, NotUsed> flow = - Flow.>create() - .flatMapConcat(ConstantFun.javaIdentityFunction()) - .grouped(6); + Flow.>create().flatMapConcat(Function.identity()).grouped(6); CompletionStage> future = Source.from(mainInputs).via(flow).runWith(Sink.head(), system); @@ -776,9 +774,7 @@ public void mustBeAbleToUseFlatMapMerge() throws Exception { mainInputs.add(Source.from(input4)); final Flow, List, NotUsed> flow = - Flow.>create() - .flatMapMerge(3, ConstantFun.javaIdentityFunction()) - .grouped(60); + Flow.>create().flatMapMerge(3, Function.identity()).grouped(60); CompletionStage> future = Source.from(mainInputs).via(flow).runWith(Sink.head(), system); diff --git a/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/SourceTest.java b/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/SourceTest.java index e7c37e55a3d..96180c51b79 100644 --- a/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/SourceTest.java +++ b/stream-tests/src/test/java/org/apache/pekko/stream/javadsl/SourceTest.java @@ -471,7 +471,7 @@ public void mustBeAbleToUseConcatAllWithSources() throws Exception { CompletionStage> future = Source.from(mainInputs) - .flatMapConcat(ConstantFun.javaIdentityFunction()) + .flatMapConcat(Function.identity()) .grouped(6) .runWith(Sink.head(), system); @@ -496,7 +496,7 @@ public void mustBeAbleToUseFlatMapMerge() throws Exception { CompletionStage> future = Source.from(mainInputs) - .flatMapMerge(3, ConstantFun.javaIdentityFunction()) + .flatMapMerge(3, Function.identity()) .grouped(60) .runWith(Sink.head(), system);