JAMES-2291 Improve slightly InMemoryMailRepositoryStore code quality
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/eb895a0a Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/eb895a0a Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/eb895a0a Branch: refs/heads/master Commit: eb895a0af0d99a8adc3398cc4b1d904ab7bce923 Parents: 7050fd6 Author: benwa <[email protected]> Authored: Thu Jan 25 16:12:20 2018 +0700 Committer: benwa <[email protected]> Committed: Thu Jan 25 16:28:45 2018 +0700 ---------------------------------------------------------------------- .../utils/InMemoryMailRepositoryStore.java | 28 +++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/eb895a0a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/InMemoryMailRepositoryStore.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/InMemoryMailRepositoryStore.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/InMemoryMailRepositoryStore.java index dbf2d3f..05d25b6 100644 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/InMemoryMailRepositoryStore.java +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/InMemoryMailRepositoryStore.java @@ -38,6 +38,7 @@ import org.apache.james.repository.api.Initializable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.github.fge.lambdas.Throwing; import com.google.common.collect.ImmutableList; import com.google.inject.Inject; @@ -117,14 +118,17 @@ public class InMemoryMailRepositoryStore implements MailRepositoryStore, Configu @Override public MailRepository select(String destinationUrl) throws MailRepositoryStoreException { Destination destination = Destination.fromUrl(destinationUrl); - MailRepository mailRepository = destinationToRepositoryAssociations.get(destination.url); - if (mailRepository != null) { - return mailRepository; - } - mailRepository = retrieveMailRepository(destination); - mailRepository = initializeNewRepository(mailRepository, createRepositoryCombinedConfig(destination)); - destinationToRepositoryAssociations.putIfAbsent(destination.url, mailRepository); - return mailRepository; + return Optional.ofNullable(destinationToRepositoryAssociations.get(destination.url)) + .orElseGet(Throwing.supplier( + () -> createNewMailRepository(destination)) + .sneakyThrow()); + } + + private MailRepository createNewMailRepository(Destination destination) throws MailRepositoryStoreException { + MailRepository newMailRepository = retrieveMailRepository(destination); + newMailRepository = initializeNewRepository(newMailRepository, createRepositoryCombinedConfig(destination)); + destinationToRepositoryAssociations.putIfAbsent(destination.url, newMailRepository); + return newMailRepository; } private void readConfigurationEntry(HierarchicalConfiguration repositoryConfiguration) throws ConfigurationException { @@ -176,11 +180,9 @@ public class InMemoryMailRepositoryStore implements MailRepositoryStore, Configu } private MailRepository retrieveMailRepository(Destination destination) throws MailRepositoryStoreException { - MailRepositoryProvider repositoryProvider = protocolToRepositoryProvider.get(destination.protocol); - if (repositoryProvider == null) { - throw new MailRepositoryStoreException("No Mail Repository associated with " + destination.protocol); - } - return repositoryProvider.provide(destination.url); + return Optional.ofNullable(protocolToRepositoryProvider.get(destination.protocol)) + .orElseThrow(() -> new MailRepositoryStoreException("No Mail Repository associated with " + destination.protocol)) + .provide(destination.url); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
