Skip to content

Commit

Permalink
chore: Add identity function.
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Jan 4, 2025
1 parent dad6b9f commit 2332be6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
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 @@ -749,9 +749,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 @@ -775,9 +773,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 @@ -470,7 +470,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 @@ -495,7 +495,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 2332be6

Please sign in to comment.