james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [1/6] james-project git commit: JAMES-2162 Introduce GuavaUtils
Date Tue, 03 Oct 2017 00:54:28 GMT
Repository: james-project
Updated Branches:
  refs/heads/master f458495a0 -> 651a03db6


JAMES-2162 Introduce GuavaUtils


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

Branch: refs/heads/master
Commit: 6e9ad73afc6d7ec13aeb8bf3e82861b901f3cabe
Parents: f458495
Author: benwa <btellier@linagora.com>
Authored: Mon Oct 2 17:30:54 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Oct 3 07:51:58 2017 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/util/GuavaUtils.java  | 37 +++++++++
 .../org/apache/james/util/GuavaUtilsTest.java   | 80 ++++++++++++++++++++
 2 files changed, 117 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6e9ad73a/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
b/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
new file mode 100644
index 0000000..518ead1
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.james.util;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.tuple.Pair;
+
+import com.github.steveash.guavate.Guavate;
+import com.google.common.collect.ImmutableListMultimap;
+
+public class GuavaUtils {
+    public static <K, V> ImmutableListMultimap<K, V> toMultimap(Map<K, List<V>>
rights) {
+        return rights.entrySet()
+            .stream()
+            .flatMap(e -> e.getValue().stream().map(right -> Pair.of(e.getKey(), right)))
+            .collect(Guavate.toImmutableListMultimap(Pair::getKey, Pair::getValue));
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e9ad73a/server/container/util-java8/src/test/java/org/apache/james/util/GuavaUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/GuavaUtilsTest.java
b/server/container/util-java8/src/test/java/org/apache/james/util/GuavaUtilsTest.java
new file mode 100644
index 0000000..3702e5e
--- /dev/null
+++ b/server/container/util-java8/src/test/java/org/apache/james/util/GuavaUtilsTest.java
@@ -0,0 +1,80 @@
+/****************************************************************
+ * 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.james.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
+import org.testcontainers.shaded.com.google.common.collect.ImmutableListMultimap;
+import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
+
+public class GuavaUtilsTest {
+
+    @Test
+    public void toMultimapShouldAcceptEmptyMaps() {
+        assertThat(GuavaUtils.toMultimap(ImmutableMap
+            .<String, List<String>>builder()
+            .build())
+            .asMap())
+            .isEqualTo(ImmutableMap.of());
+    }
+
+    @Test
+    public void toMultimapShouldAcceptSingleValuesMaps() {
+        assertThat(GuavaUtils.toMultimap(ImmutableMap
+            .<String, List<String>>builder()
+            .put("k1", ImmutableList.of("v1"))
+            .put("k2", ImmutableList.of("v2"))
+            .build())
+            .asMap())
+            .isEqualTo(ImmutableListMultimap.of(
+                "k1", "v1",
+                "k2", "v2")
+            .asMap());
+    }
+
+    @Test
+    public void toMultimapShouldAcceptMultiplesValuesMaps() {
+        assertThat(GuavaUtils.toMultimap(ImmutableMap
+            .<String, List<String>>builder()
+            .put("k1", ImmutableList.of("v1"))
+            .put("k2", ImmutableList.of("v2", "v2.1"))
+            .build())
+            .asMap())
+            .isEqualTo(ImmutableListMultimap.of(
+                "k1", "v1",
+                "k2", "v2",
+                "k2", "v2.1")
+                .asMap());
+    }
+
+    @Test
+    public void shouldStripEntriesWithEmptyList() {
+        assertThat(GuavaUtils.toMultimap(ImmutableMap
+            .<String, List<String>>builder()
+            .put("k1", ImmutableList.of())
+            .build())
+            .asMap())
+            .isEqualTo(ImmutableListMultimap.of().asMap());
+    }
+}
\ No newline at end of file


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


Mime
View raw message