chore: Add identity function. (#1671)
This commit is contained in:
parent
0855a3b035
commit
8f6f97aa2d
4 changed files with 61 additions and 8 deletions
|
|
@ -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<Object, Object> 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<Object, Object> funObj = ConstantFun.javaIdentityFunction();
|
||||
final Function<String, String> funStr = ConstantFun.javaIdentityFunction();
|
||||
try {
|
||||
Assert.assertTrue(funObj.equals(funStr));
|
||||
} catch (Exception e) {
|
||||
Assert.fail("Exception thrown: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -750,9 +750,7 @@ public class FlowTest extends StreamTest {
|
|||
mainInputs.add(Source.from(input2));
|
||||
|
||||
final Flow<Source<Integer, NotUsed>, List<Integer>, NotUsed> flow =
|
||||
Flow.<Source<Integer, NotUsed>>create()
|
||||
.flatMapConcat(ConstantFun.javaIdentityFunction())
|
||||
.grouped(6);
|
||||
Flow.<Source<Integer, NotUsed>>create().flatMapConcat(Function.identity()).grouped(6);
|
||||
CompletionStage<List<Integer>> future =
|
||||
Source.from(mainInputs).via(flow).runWith(Sink.head(), system);
|
||||
|
||||
|
|
@ -776,9 +774,7 @@ public class FlowTest extends StreamTest {
|
|||
mainInputs.add(Source.from(input4));
|
||||
|
||||
final Flow<Source<Integer, NotUsed>, List<Integer>, NotUsed> flow =
|
||||
Flow.<Source<Integer, NotUsed>>create()
|
||||
.flatMapMerge(3, ConstantFun.javaIdentityFunction())
|
||||
.grouped(60);
|
||||
Flow.<Source<Integer, NotUsed>>create().flatMapMerge(3, Function.identity()).grouped(60);
|
||||
CompletionStage<List<Integer>> future =
|
||||
Source.from(mainInputs).via(flow).runWith(Sink.head(), system);
|
||||
|
||||
|
|
|
|||
|
|
@ -471,7 +471,7 @@ public class SourceTest extends StreamTest {
|
|||
|
||||
CompletionStage<List<Integer>> future =
|
||||
Source.from(mainInputs)
|
||||
.flatMapConcat(ConstantFun.javaIdentityFunction())
|
||||
.flatMapConcat(Function.identity())
|
||||
.grouped(6)
|
||||
.runWith(Sink.head(), system);
|
||||
|
||||
|
|
@ -496,7 +496,7 @@ public class SourceTest extends StreamTest {
|
|||
|
||||
CompletionStage<List<Integer>> future =
|
||||
Source.from(mainInputs)
|
||||
.flatMapMerge(3, ConstantFun.javaIdentityFunction())
|
||||
.flatMapMerge(3, Function.identity())
|
||||
.grouped(60)
|
||||
.runWith(Sink.head(), system);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue