james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [10/24] james-project git commit: JAMES-2366 Improve mapAddress internal for memory and Cassandra implementations
Date Tue, 03 Apr 2018 10:02:56 GMT
JAMES-2366 Improve mapAddress internal for memory and Cassandra implementations


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

Branch: refs/heads/master
Commit: 5ef82035e50fb47aea621ecbf4274bf997f117ee
Parents: 36983a1
Author: benwa <btellier@linagora.com>
Authored: Wed Mar 28 10:25:35 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 16:59:44 2018 +0700

----------------------------------------------------------------------
 .../org/apache/james/util/OptionalUtils.java    | 13 +++-
 .../apache/james/util/OptionalUtilsTest.java    | 68 ++++++++++++++++++++
 .../CassandraRecipientRewriteTable.java         | 19 +++---
 .../rrt/memory/MemoryRecipientRewriteTable.java | 21 +++---
 4 files changed, 101 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/5ef82035/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 117efa3..4a3449c 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
@@ -20,6 +20,7 @@ package org.apache.james.util;
 
 import java.util.Arrays;
 import java.util.Optional;
+import java.util.function.Supplier;
 import java.util.stream.Stream;
 
 public class OptionalUtils {
@@ -43,7 +44,17 @@ public class OptionalUtils {
 
     @SafeVarargs
     public static <T> Optional<T> or(Optional<T>... optionals) {
-        return Arrays.stream(optionals)
+        return orStream(Arrays.stream(optionals));
+    }
+
+    @SafeVarargs
+    public static <T> Optional<T> orSuppliers(Supplier<Optional<T>>...
suppliers) {
+        return orStream(Arrays.stream(suppliers)
+            .map(Supplier::get));
+    }
+
+    private static <T> Optional<T> orStream(Stream<Optional<T>> stream)
{
+        return stream
             .filter(Optional::isPresent)
             .findFirst()
             .orElse(Optional.empty());

http://git-wip-us.apache.org/repos/asf/james-project/blob/5ef82035/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 3428c60..17fe06e 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
@@ -153,6 +153,74 @@ public class OptionalUtilsTest {
     }
 
     @Test
+    public void orSuppliersShouldReturnEmptyWhenNoParameter() {
+        assertThat(OptionalUtils.or())
+            .isEmpty();
+    }
+
+    @Test
+    public void orSuppliersShouldReturnEmptyWhenEmpty() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                Optional::empty))
+            .isEmpty();
+    }
+
+    @Test
+    public void orSuppliersShouldReturnValueWhenValue() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                () -> Optional.of(1)))
+            .contains(1);
+    }
+
+    @Test
+    public void orSuppliersShouldReturnEmptyWhenBothEmpty() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                () -> Optional.empty(),
+                () -> Optional.empty()))
+            .isEmpty();
+    }
+
+    @Test
+    public void orSuppliersShouldReturnFirstValueWhenOnlyFirstValue() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                () -> Optional.of(18),
+                Optional::empty))
+            .contains(18);
+    }
+
+    @Test
+    public void orSuppliersShouldReturnSecondValueWhenOnlySecondValue() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                Optional::empty,
+                () -> Optional.of(18)))
+            .contains(18);
+    }
+
+    @Test
+    public void orSuppliersShouldReturnFirstValueWhenBothValues() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                () -> Optional.of(1),
+                () -> Optional.of(2)))
+            .contains(1);
+    }
+
+    @Test
+    public void orSuppliersShouldReturnThirdValueWhenOnlyThirdValue() {
+        assertThat(
+            OptionalUtils.orSuppliers(
+                Optional::empty,
+                Optional::empty,
+                () -> Optional.of(1)))
+            .contains(1);
+    }
+
+    @Test
     public void containsDifferentShouldReturnTrueWhenNullStoreValue() throws Exception {
         assertThat(OptionalUtils.containsDifferent(Optional.of("any"), null)).isTrue();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/5ef82035/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/CassandraRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git a/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/CassandraRecipientRewriteTable.java
b/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/CassandraRecipientRewriteTable.java
index e854616..e4bdd03 100644
--- a/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/CassandraRecipientRewriteTable.java
+++ b/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/CassandraRecipientRewriteTable.java
@@ -37,11 +37,11 @@ import javax.inject.Inject;
 import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
 import org.apache.james.backends.cassandra.utils.CassandraUtils;
 import org.apache.james.core.Domain;
-import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
 import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.rrt.lib.MappingsImpl;
+import org.apache.james.util.OptionalUtils;
 
 import com.datastax.driver.core.PreparedStatement;
 import com.datastax.driver.core.Session;
@@ -94,7 +94,7 @@ public class CassandraRecipientRewriteTable extends AbstractRecipientRewriteTabl
     }
 
     @Override
-    protected void addMappingInternal(String user, Domain domain, Mapping mapping) throws
RecipientRewriteTableException {
+    protected void addMappingInternal(String user, Domain domain, Mapping mapping) {
         executor.executeVoid(insertStatement.bind()
             .setString(USER, getFixedUser(user))
             .setString(DOMAIN, getFixedDomain(domain).asString())
@@ -103,7 +103,7 @@ public class CassandraRecipientRewriteTable extends AbstractRecipientRewriteTabl
     }
 
     @Override
-    protected void removeMappingInternal(String user, Domain domain, Mapping mapping) throws
RecipientRewriteTableException {
+    protected void removeMappingInternal(String user, Domain domain, Mapping mapping) {
         executor.executeVoid(deleteStatement.bind()
                 .setString(USER, getFixedUser(user))
                 .setString(DOMAIN, getFixedDomain(domain).asString())
@@ -112,7 +112,7 @@ public class CassandraRecipientRewriteTable extends AbstractRecipientRewriteTabl
     }
 
     @Override
-    protected Mappings getUserDomainMappingsInternal(String user, Domain domain) throws RecipientRewriteTableException
{
+    protected Mappings getUserDomainMappingsInternal(String user, Domain domain) {
         return retrieveMappings(user, domain)
             .orElse(null);
     }
@@ -130,7 +130,7 @@ public class CassandraRecipientRewriteTable extends AbstractRecipientRewriteTabl
     }
 
     @Override
-    protected Map<String, Mappings> getAllMappingsInternal() throws RecipientRewriteTableException
{
+    protected Map<String, Mappings> getAllMappingsInternal() {
         Map<String, Mappings> map = executor.execute(retrieveAllMappingsStatement.bind())
             .thenApply(resultSet -> cassandraUtils.convertToStream(resultSet)
                 .map(row -> new UserMapping(row.getString(USER), Domain.of(row.getString(DOMAIN)),
row.getString(MAPPING)))
@@ -176,10 +176,11 @@ public class CassandraRecipientRewriteTable extends AbstractRecipientRewriteTabl
     }
 
     @Override
-    protected String mapAddressInternal(String user, Domain domain) throws RecipientRewriteTableException
{
-        Mappings mappings = retrieveMappings(user, domain)
-            .orElseGet(() -> retrieveMappings(WILDCARD, domain)
-                .orElseGet(MappingsImpl::empty));
+    protected String mapAddressInternal(String user, Domain domain) {
+        Mappings mappings = OptionalUtils.orSuppliers(
+            () -> retrieveMappings(user, domain),
+            () -> retrieveMappings(WILDCARD, domain))
+                .orElse(MappingsImpl.empty());
 
         return !mappings.isEmpty() ? mappings.serialize() : null;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/5ef82035/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git a/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
b/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
index 2662e13..c8d3965 100644
--- a/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
+++ b/server/data/data-memory/src/main/java/org/apache/james/rrt/memory/MemoryRecipientRewriteTable.java
@@ -28,11 +28,11 @@ import java.util.stream.Stream;
 
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.james.core.Domain;
-import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
 import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.rrt.lib.MappingsImpl;
+import org.apache.james.util.OptionalUtils;
 
 import com.github.steveash.guavate.Guavate;
 import com.google.common.base.Objects;
@@ -93,32 +93,33 @@ public class MemoryRecipientRewriteTable extends AbstractRecipientRewriteTable
{
     }
 
     @Override
-    protected void addMappingInternal(String user, Domain domain, Mapping mapping) throws
RecipientRewriteTableException {
+    protected void addMappingInternal(String user, Domain domain, Mapping mapping) {
         mappingEntries.add(new InMemoryMappingEntry(getFixedUser(user), getFixedDomain(domain),
mapping));
     }
 
     @Override
-    protected void removeMappingInternal(String user, Domain domain, Mapping mapping) throws
RecipientRewriteTableException {
+    protected void removeMappingInternal(String user, Domain domain, Mapping mapping) {
         mappingEntries.remove(new InMemoryMappingEntry(getFixedUser(user), getFixedDomain(domain),
mapping));
     }
 
     @Override
-    protected Mappings getUserDomainMappingsInternal(String user, Domain domain) throws RecipientRewriteTableException
{
+    protected Mappings getUserDomainMappingsInternal(String user, Domain domain) {
         return retrieveMappings(user, domain)
             .orElse(null);
     }
 
     @Override
-    protected String mapAddressInternal(String user, Domain domain) throws RecipientRewriteTableException
{
-        Mappings mappings = retrieveMappings(user, domain)
-            .orElse(retrieveMappings(WILDCARD, domain)
-                .orElse(MappingsImpl.empty()));
+    protected String mapAddressInternal(String user, Domain domain) {
+        Mappings mappings = OptionalUtils.orSuppliers(
+            () -> retrieveMappings(user, domain),
+            () -> retrieveMappings(WILDCARD, domain))
+            .orElse(MappingsImpl.empty());
 
         return !mappings.isEmpty() ? mappings.serialize() : null;
     }
 
     @Override
-    protected Map<String, Mappings> getAllMappingsInternal() throws RecipientRewriteTableException
{
+    protected Map<String, Mappings> getAllMappingsInternal() {
         if (mappingEntries.isEmpty()) {
             return null;
         }
@@ -136,7 +137,7 @@ public class MemoryRecipientRewriteTable extends AbstractRecipientRewriteTable
{
             .map(InMemoryMappingEntry::getMapping));
     }
 
-    private Optional<Mappings> retrieveMappings(final String user, final Domain domain)
{
+    private Optional<Mappings> retrieveMappings(String user, Domain domain) {
         Stream<Mapping> userEntries = mappingEntries.stream()
             .filter(mappingEntry -> user.equals(mappingEntry.getUser()) && domain.equals(mappingEntry.getDomain()))
             .map(InMemoryMappingEntry::getMapping);


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