You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/03/28 03:21:57 UTC

[james-project] 13/23: MAILBOX-388 Add runnable -> function util

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 1fe0d84086f81fb28a559f306434e50468c897b0
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Mar 21 13:38:10 2019 +0700

    MAILBOX-388 Add runnable -> function util
---
 .../org/apache/james/util/FunctionalUtils.java     |  7 ++++++
 .../org/apache/james/util/FunctionalUtilsTest.java | 28 +++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java b/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java
index 54c8305..651f8ba 100644
--- a/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java
+++ b/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java
@@ -31,6 +31,13 @@ public class FunctionalUtils {
         };
     }
 
+    public static <T> UnaryOperator<T> identityWithSideEffect(Runnable runnable) {
+        return argument -> {
+            runnable.run();
+            return argument;
+        };
+    }
+
     public static <T> Predicate<T> toPredicate(Function<T, Boolean> function) {
         return value -> function.apply(value);
     }
diff --git a/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java b/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java
index 2147ef4..6c736b0 100644
--- a/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java
+++ b/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java
@@ -32,15 +32,41 @@ class FunctionalUtilsTest {
     @Nested
     class ToFunction {
         @Test
-        void shouldCallConsumerAndReturnTheGivenParameter() {
+        void toFunctionShouldReturnTheGivenParameter() {
             Counter counter = new Counter(26);
             Consumer<Integer> consumer = counter::increment;
             Function<Integer, Integer> function = FunctionalUtils.toFunction(consumer);
 
             assertThat(function.apply(16)).isEqualTo(16);
+        }
+
+        @Test
+        void toFunctionShouldCallConsumer() {
+            Counter counter = new Counter(26);
+            Consumer<Integer> consumer = counter::increment;
+            FunctionalUtils.toFunction(consumer).apply(16);
+
             assertThat(counter.getCounter()).isEqualTo(42);
         }
 
+        @Test
+        void identityWithSideEffectShouldReturnTheGivenParameterForRunnable() {
+            Counter counter = new Counter(26);
+            Runnable runnable = () -> counter.increment(1);
+            Function<Integer, Integer> function = FunctionalUtils.identityWithSideEffect(runnable);
+
+            assertThat(function.apply(16)).isEqualTo(16);
+        }
+
+        @Test
+        void identityWithSideEffectShouldCallRunnable() {
+            Counter counter = new Counter(26);
+            Runnable runnable = () -> counter.increment(1);
+            FunctionalUtils.identityWithSideEffect(runnable).apply(23);
+
+            assertThat(counter.getCounter()).isEqualTo(27);
+        }
+
         private class Counter {
             private Integer counter;
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org