Author: aduprat
Date: Wed Jan 13 10:12:16 2016
New Revision: 1724387

URL: http://svn.apache.org/viewvc?rev=1724387&view=rev
Log:
JAMES-1648 Implement Collectors.toImmutableSet. Contributed by Baechler 
<matthieu.baech...@gmail.com>

Modified:
    
james/project/trunk/server/container/util-java8/src/main/java/org/apache/james/util/streams/Collectors.java
    
james/project/trunk/server/container/util-java8/src/test/java/org/apache/james/util/streams/CollectorsTest.java

Modified: 
james/project/trunk/server/container/util-java8/src/main/java/org/apache/james/util/streams/Collectors.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/container/util-java8/src/main/java/org/apache/james/util/streams/Collectors.java?rev=1724387&r1=1724386&r2=1724387&view=diff
==============================================================================
--- 
james/project/trunk/server/container/util-java8/src/main/java/org/apache/james/util/streams/Collectors.java
 (original)
+++ 
james/project/trunk/server/container/util-java8/src/main/java/org/apache/james/util/streams/Collectors.java
 Wed Jan 13 10:12:16 2016
@@ -21,11 +21,13 @@ package org.apache.james.util.streams;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 import java.util.function.Function;
 import java.util.stream.Collector;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 
 public class Collectors {
 
@@ -35,7 +37,14 @@ public class Collectors {
             return left;
         }, ImmutableList::copyOf);
     }
-    
+
+    public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> 
toImmutableSet() {
+        return Collector.of(ImmutableSet::builder, ImmutableSet.Builder::add, 
(left, right) -> {
+            left.addAll(right.build());
+            return left;
+        }, ImmutableSet.Builder::build);
+    }
+
     public static <T, K, V> Collector<T, ImmutableMap.Builder<K, V>, 
ImmutableMap<K, V>> toImmutableMap(
             Function<? super T, ? extends K> keyMapper,
             Function<? super T, ? extends V> valueMapper) {

Modified: 
james/project/trunk/server/container/util-java8/src/test/java/org/apache/james/util/streams/CollectorsTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/container/util-java8/src/test/java/org/apache/james/util/streams/CollectorsTest.java?rev=1724387&r1=1724386&r2=1724387&view=diff
==============================================================================
--- 
james/project/trunk/server/container/util-java8/src/test/java/org/apache/james/util/streams/CollectorsTest.java
 (original)
+++ 
james/project/trunk/server/container/util-java8/src/test/java/org/apache/james/util/streams/CollectorsTest.java
 Wed Jan 13 10:12:16 2016
@@ -25,13 +25,16 @@ import static org.assertj.core.data.MapE
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import com.google.common.collect.ImmutableSet;
 import org.junit.Test;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
 public class CollectorsTest {
+
     @Test
     public void 
immutableListCollectorShouldReturnEmptyImmutableListWhenEmptyStream() {
         String[] data = {};
@@ -58,7 +61,35 @@ public class CollectorsTest {
         assertThat(actual).isInstanceOf(ImmutableList.class);
         assertThat(actual).containsExactly("a", "b", "c");
     }
-    
+
+    @Test
+    public void 
immutableSetCollectorShouldReturnEmptyImmutableSetWhenEmptyStream() {
+        String[] data = {};
+        Set<String> actual = Arrays.stream(data)
+            .collect(Collectors.toImmutableSet());
+        assertThat(actual).isInstanceOf(ImmutableSet.class);
+        assertThat(actual).isEmpty();
+    }
+
+    @Test
+    public void 
immutableSetCollectorShouldReturnImmutableSetWhenOneElementStream() {
+        String[] data = {"a"};
+        Set<String> actual = Arrays.stream(data)
+            .collect(Collectors.toImmutableSet());
+        assertThat(actual).isInstanceOf(ImmutableSet.class);
+        assertThat(actual).containsExactly("a");
+    }
+
+    @Test
+    public void 
immutableSetCollectorShouldReturnImmutableSetWhen3ElementsStream() {
+        String[] data = {"a", "b", "c"};
+        Set<String> actual = Arrays.stream(data)
+            .collect(Collectors.toImmutableSet());
+        assertThat(actual).isInstanceOf(ImmutableSet.class);
+        assertThat(actual).containsExactly("a", "b", "c");
+    }
+
+
     @Test
     public void 
immutableMapCollectorShouldReturnEmptyImmutableMapWhenEmptyStream() {
         String[] data = {};



---------------------------------------------------------------------
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