Repository: james-project
Updated Branches:
  refs/heads/master c383bdf3a -> a11ce53c0


JAMES-2346 OptionalUtils.or can now take more than 2 parameters


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/ec73d3f7
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/ec73d3f7
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/ec73d3f7

Branch: refs/heads/master
Commit: ec73d3f77f9eac10b0f0e675ea0275e64aa47bd0
Parents: c383bdf
Author: Raphael Ouazana <raphael.ouaz...@linagora.com>
Authored: Fri Mar 9 17:27:57 2018 +0100
Committer: benwa <btell...@linagora.com>
Committed: Tue Mar 13 15:10:18 2018 +0700

----------------------------------------------------------------------
 .../org/apache/james/util/OptionalUtils.java    |  9 ++++--
 .../apache/james/util/OptionalUtilsTest.java    | 32 ++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/ec73d3f7/server/container/util-java8/src/main/java/org/apache/james/util/OptionalUtils.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/main/java/org/apache/james/util/OptionalUtils.java
 
b/server/container/util-java8/src/main/java/org/apache/james/util/OptionalUtils.java
index b08abfe..117efa3 100644
--- 
a/server/container/util-java8/src/main/java/org/apache/james/util/OptionalUtils.java
+++ 
b/server/container/util-java8/src/main/java/org/apache/james/util/OptionalUtils.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.util;
 
+import java.util.Arrays;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -40,10 +41,12 @@ public class OptionalUtils {
             .orElse(Stream.of());
     }
 
-    public static <T> Optional<T> or(Optional<T> optional1, Optional<T> 
optional2) {
-        return optional1.map(Optional::of)
+    @SafeVarargs
+    public static <T> Optional<T> or(Optional<T>... optionals) {
+        return Arrays.stream(optionals)
             .filter(Optional::isPresent)
-            .orElse(optional2);
+            .findFirst()
+            .orElse(Optional.empty());
     }
 
     public static <T> boolean containsDifferent(Optional<T> requestValue, T 
storeValue) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/ec73d3f7/server/container/util-java8/src/test/java/org/apache/james/util/OptionalUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/test/java/org/apache/james/util/OptionalUtilsTest.java
 
b/server/container/util-java8/src/test/java/org/apache/james/util/OptionalUtilsTest.java
index fcf237c..3428c60 100644
--- 
a/server/container/util-java8/src/test/java/org/apache/james/util/OptionalUtilsTest.java
+++ 
b/server/container/util-java8/src/test/java/org/apache/james/util/OptionalUtilsTest.java
@@ -85,6 +85,28 @@ public class OptionalUtilsTest {
     }
 
     @Test
+    public void orShouldReturnEmptyWhenNoParameter() {
+        assertThat(OptionalUtils.or())
+            .isEmpty();
+    }
+
+    @Test
+    public void orShouldReturnEmptyWhenEmpty() {
+        assertThat(
+            OptionalUtils.or(
+                Optional.empty()))
+            .isEmpty();
+    }
+
+    @Test
+    public void orShouldReturnValueWhenValue() {
+        assertThat(
+            OptionalUtils.or(
+                Optional.of(1)))
+            .contains(1);
+    }
+
+    @Test
     public void orShouldReturnEmptyWhenBothEmpty() {
         assertThat(
             OptionalUtils.or(
@@ -121,6 +143,16 @@ public class OptionalUtilsTest {
     }
 
     @Test
+    public void orShouldReturnThirdValueWhenOnlyThirdValue() {
+        assertThat(
+            OptionalUtils.or(
+                Optional.empty(),
+                Optional.empty(),
+                Optional.of(1)))
+            .contains(1);
+    }
+
+    @Test
     public void containsDifferentShouldReturnTrueWhenNullStoreValue() throws 
Exception {
         assertThat(OptionalUtils.containsDifferent(Optional.of("any"), 
null)).isTrue();
     }


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

Reply via email to