Skip to content

Commit

Permalink
chore: Add identity function. (#1671)
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin authored Jan 6, 2025
1 parent 0855a3b commit 8f6f97a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
11 changes: 11 additions & 0 deletions actor/src/main/scala/org/apache/pekko/japi/function/Function.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.apache.pekko.japi.function

import org.apache.pekko.util.ConstantFun

import scala.annotation.nowarn

/**
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,7 @@ public void mustBeAbleToUseConcatAllWithSources() throws Exception {
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);

Expand All @@ -776,9 +774,7 @@ public void mustBeAbleToUseFlatMapMerge() throws Exception {
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public void mustBeAbleToUseConcatAllWithSources() throws Exception {

CompletionStage<List<Integer>> future =
Source.from(mainInputs)
.flatMapConcat(ConstantFun.javaIdentityFunction())
.flatMapConcat(Function.identity())
.grouped(6)
.runWith(Sink.head(), system);

Expand All @@ -496,7 +496,7 @@ public void mustBeAbleToUseFlatMapMerge() throws Exception {

CompletionStage<List<Integer>> future =
Source.from(mainInputs)
.flatMapMerge(3, ConstantFun.javaIdentityFunction())
.flatMapMerge(3, Function.identity())
.grouped(60)
.runWith(Sink.head(), system);

Expand Down

0 comments on commit 8f6f97a

Please sign in to comment.