JAMES-2366 Introduce a Memoized supplier for Java8
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6be6b357 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6be6b357 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6be6b357 Branch: refs/heads/master Commit: 6be6b35736f00162eaca43c177aeea6b78517339 Parents: 146208d Author: benwa <[email protected]> Authored: Thu Mar 29 10:08:56 2018 +0700 Committer: benwa <[email protected]> Committed: Tue Apr 3 17:00:14 2018 +0700 ---------------------------------------------------------------------- .../java/org/apache/james/util/GuavaUtils.java | 4 - .../org/apache/james/util/MemoizedSupplier.java | 30 +++++++ .../apache/james/util/MemoizedSupplierTest.java | 84 ++++++++++++++++++++ .../mailets/RecipientRewriteTableProcessor.java | 11 +-- .../james/webadmin/RandomPortSupplier.java | 12 ++- 5 files changed, 123 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/6be6b357/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 index f5493c9..518ead1 100644 --- 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 @@ -34,8 +34,4 @@ public class GuavaUtils { .flatMap(e -> e.getValue().stream().map(right -> Pair.of(e.getKey(), right))) .collect(Guavate.toImmutableListMultimap(Pair::getKey, Pair::getValue)); } - - public static <T> com.google.common.base.Supplier<T> toGuava(java.util.function.Supplier<T> javaSupplier) { - return javaSupplier::get; - } } http://git-wip-us.apache.org/repos/asf/james-project/blob/6be6b357/server/container/util-java8/src/main/java/org/apache/james/util/MemoizedSupplier.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MemoizedSupplier.java b/server/container/util-java8/src/main/java/org/apache/james/util/MemoizedSupplier.java new file mode 100644 index 0000000..d0b4e3b --- /dev/null +++ b/server/container/util-java8/src/main/java/org/apache/james/util/MemoizedSupplier.java @@ -0,0 +1,30 @@ +/**************************************************************** + * 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.function.Supplier; + +import com.google.common.base.Suppliers; + +public class MemoizedSupplier { + public static <T> Supplier<T> of(Supplier<T> originalSupplier) { + return Suppliers.memoize(originalSupplier::get)::get; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6be6b357/server/container/util-java8/src/test/java/org/apache/james/util/MemoizedSupplierTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/MemoizedSupplierTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/MemoizedSupplierTest.java new file mode 100644 index 0000000..e774354 --- /dev/null +++ b/server/container/util-java8/src/test/java/org/apache/james/util/MemoizedSupplierTest.java @@ -0,0 +1,84 @@ +/**************************************************************** + * 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.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; + +import org.junit.Test; + +public class MemoizedSupplierTest { + + @Test + public void getShouldReturnSuppliedValue() { + Supplier<Integer> supplier = MemoizedSupplier.of(() -> 42); + + assertThat(supplier.get()).isEqualTo(42); + } + + @Test + public void getShouldBeIdempotent() { + Supplier<Integer> supplier = MemoizedSupplier.of(() -> 42); + + supplier.get(); + assertThat(supplier.get()).isEqualTo(42); + } + + @Test + public void nullValueShouldBeSupported() { + Supplier<Integer> supplier = MemoizedSupplier.of(() -> null); + + supplier.get(); + assertThat(supplier.get()).isNull(); + } + + @Test + public void underlyingSupplierShouldBeCalledOnlyOnce() { + AtomicInteger atomicInteger = new AtomicInteger(0); + + Supplier<Integer> supplier = MemoizedSupplier.of(() -> { + atomicInteger.incrementAndGet(); + return 42; + }); + + supplier.get(); + supplier.get(); + + assertThat(atomicInteger.get()).isEqualTo(1); + } + + @Test + public void underlyingSupplierShouldBeCalledOnlyOnceWhenReturningNullValue() { + AtomicInteger atomicInteger = new AtomicInteger(0); + + Supplier<Integer> supplier = MemoizedSupplier.of(() -> { + atomicInteger.incrementAndGet(); + return null; + }); + + supplier.get(); + supplier.get(); + + assertThat(atomicInteger.get()).isEqualTo(1); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/6be6b357/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java index f725a4d..b3bb847 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java @@ -22,6 +22,7 @@ package org.apache.james.transport.mailets; import java.util.List; import java.util.Optional; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Stream; import javax.mail.MessagingException; @@ -37,7 +38,7 @@ import org.apache.james.rrt.api.RecipientRewriteTable.ErrorMappingException; import org.apache.james.rrt.api.RecipientRewriteTableException; import org.apache.james.rrt.lib.Mapping; import org.apache.james.rrt.lib.Mappings; -import org.apache.james.util.GuavaUtils; +import org.apache.james.util.MemoizedSupplier; import org.apache.james.util.OptionalUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailetContext; @@ -48,8 +49,6 @@ import com.github.fge.lambdas.Throwing; import com.github.steveash.guavate.Guavate; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; public class RecipientRewriteTableProcessor { @@ -190,10 +189,8 @@ public class RecipientRewriteTableProcessor { } private ImmutableList<Mapping> sanitizeMappingsWithNoDomain(Mappings mappings, DomainList domainList) throws MessagingException { - Supplier<Domain> defaultDomainSupplier = Suppliers.memoize( - GuavaUtils.toGuava( - Throwing.supplier(() -> getDefaultDomain(domainList)) - .sneakyThrow())); + Supplier<Domain> defaultDomainSupplier = MemoizedSupplier.of( + Throwing.supplier(() -> getDefaultDomain(domainList)).sneakyThrow()); return mappings.asStream() .filter(mapping -> !mapping.hasDomain()) http://git-wip-us.apache.org/repos/asf/james-project/blob/6be6b357/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java index 6f21591..7f545b2 100644 --- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java @@ -21,27 +21,25 @@ package org.apache.james.webadmin; import java.io.IOException; import java.net.ServerSocket; +import java.util.function.Supplier; +import org.apache.james.util.MemoizedSupplier; import org.apache.james.util.Port; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.base.Throwables; +import com.github.fge.lambdas.Throwing; public class RandomPortSupplier implements PortSupplier { - public static int findFreePort() { + public static int findFreePort() throws IOException { try (ServerSocket socket = new ServerSocket(0)) { return socket.getLocalPort(); - } catch (IOException e) { - throw Throwables.propagate(e); } } private final Supplier<Integer> portSupplier; public RandomPortSupplier() { - portSupplier = Suppliers.memoize(RandomPortSupplier::findFreePort); + portSupplier = MemoizedSupplier.of(Throwing.supplier(RandomPortSupplier::findFreePort)); } @Override --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
