This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c94fcf7bb5896c972c0b372e1efbc75be0bdbb44
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Mon Apr 8 15:43:08 2019 +0700

    JAMES-2708 Avoid unecessary class nesting
    
    BlobExportImplChoice can directly be an enumeration
---
 .../apache/james/modules/BlobExportImplChoice.java | 72 ++++++----------------
 .../james/modules/BlobExportMechanismModule.java   | 10 +--
 .../james/modules/BlobExportImplChoiceTest.java    |  6 +-
 .../modules/BlobExportMechanismModuleTest.java     | 10 +--
 4 files changed, 32 insertions(+), 66 deletions(-)

diff --git 
a/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java
 
b/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java
index 4551929..a00eb78 100644
--- 
a/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java
+++ 
b/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportImplChoice.java
@@ -19,7 +19,6 @@
 
 package org.apache.james.modules;
 
-import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -30,38 +29,21 @@ import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 
-class BlobExportImplChoice {
+enum  BlobExportImplChoice {
+    LOCAL_FILE("localFile");
 
-    enum BlobExportImplName {
-        LOCAL_FILE("localFile");
+    private static Optional<BlobExportImplChoice> from(String implNameString) {
+        Preconditions.checkNotNull(implNameString);
 
-        private static Optional<BlobExportImplName> from(String 
implNameString) {
-            Preconditions.checkNotNull(implNameString);
-
-            return Stream.of(values())
-                .filter(impl -> impl.name.equals(implNameString))
-                .findFirst();
-        }
-
-        private static ImmutableList<String> plainImplNames() {
-            return Stream.of(values())
-                .map(impl -> impl.name)
-                .collect(Guavate.toImmutableList());
-        }
-
-        private final String name;
-
-        BlobExportImplName(String name) {
-            this.name = name;
-        }
-
-        String getImplName() {
-            return name;
-        }
+        return Stream.of(values())
+            .filter(impl -> impl.name.equals(implNameString))
+            .findFirst();
     }
 
-    static BlobExportImplChoice localFile() {
-        return new BlobExportImplChoice(BlobExportImplName.LOCAL_FILE);
+    private static ImmutableList<String> plainImplNames() {
+        return Stream.of(values())
+            .map(impl -> impl.name)
+            .collect(Guavate.toImmutableList());
     }
 
     static Optional<BlobExportImplChoice> from(Configuration configuration) {
@@ -70,41 +52,25 @@ class BlobExportImplChoice {
         Optional<String> sanitizedImplName = 
Optional.ofNullable(blobExportImpl)
             .map(String::trim);
 
-        return sanitizedImplName.map(name -> BlobExportImplName.from(name)
-                .map(BlobExportImplChoice::new)
-                .orElseThrow(() -> new 
IllegalArgumentException(unknownBlobExportErrorMessage(name))));
+        return sanitizedImplName.map(name -> BlobExportImplChoice.from(name)
+            .orElseThrow(() -> new 
IllegalArgumentException(unknownBlobExportErrorMessage(name))));
     }
 
     private static String unknownBlobExportErrorMessage(String blobExportImpl) 
{
         return String.format("unknown blob export mechanism '%s', please 
choose one in supported implementations(%s)",
             blobExportImpl,
-            Joiner.on(",").join(BlobExportImplName.plainImplNames()));
+            Joiner.on(",").join(BlobExportImplChoice.plainImplNames()));
     }
 
     private static final String BLOB_EXPORT_MECHANISM_IMPL = 
"blob.export.implementation";
 
-    private final BlobExportImplName impl;
-
-    private BlobExportImplChoice(BlobExportImplName implName) {
-        this.impl = implName;
-    }
-
-    public BlobExportImplName getImpl() {
-        return impl;
-    }
-
-    @Override
-    public final boolean equals(Object o) {
-        if (o instanceof BlobExportImplChoice) {
-            BlobExportImplChoice that = (BlobExportImplChoice) o;
+    private final String name;
 
-            return Objects.equals(this.impl, that.impl);
-        }
-        return false;
+    BlobExportImplChoice(String name) {
+        this.name = name;
     }
 
-    @Override
-    public final int hashCode() {
-        return Objects.hash(impl);
+    String getImplName() {
+        return name;
     }
 }
diff --git 
a/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportMechanismModule.java
 
b/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportMechanismModule.java
index 6fa7862..d228615 100644
--- 
a/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportMechanismModule.java
+++ 
b/server/container/guice/blob-export-guice/src/main/java/org/apache/james/modules/BlobExportMechanismModule.java
@@ -54,12 +54,12 @@ public class BlobExportMechanismModule extends 
AbstractModule {
             Configuration configuration = 
propertiesProvider.getConfiguration(ConfigurationComponent.NAME);
             return BlobExportImplChoice.from(configuration)
                 .orElseGet(() -> {
-                    LOGGER.warn("No blob export mechanism defined. Defaulting 
to " + BlobExportImplChoice.BlobExportImplName.LOCAL_FILE.getImplName());
-                    return BlobExportImplChoice.localFile();
+                    LOGGER.warn("No blob export mechanism defined. Defaulting 
to " + BlobExportImplChoice.LOCAL_FILE.getImplName());
+                    return BlobExportImplChoice.LOCAL_FILE;
                 });
         } catch (FileNotFoundException e) {
             LOGGER.warn("Could not find " + ConfigurationComponent.NAME + " 
configuration file, using localFile blob exporting as the default");
-            return BlobExportImplChoice.localFile();
+            return BlobExportImplChoice.LOCAL_FILE;
         }
     }
 
@@ -67,11 +67,11 @@ public class BlobExportMechanismModule extends 
AbstractModule {
     @Provides
     @Singleton
     BlobExportMechanism provideMechanism(BlobExportImplChoice implChoice, 
Provider<LocalFileBlobExportMechanism> localFileMechanismProvider) {
-        switch (implChoice.getImpl()) {
+        switch (implChoice) {
             case LOCAL_FILE:
                 return localFileMechanismProvider.get();
             default:
-                throw new RuntimeException("blobExportMechanism '" + 
implChoice.getImpl().getImplName() + "' is not supported yet");
+                throw new RuntimeException("blobExportMechanism '" + 
implChoice.getImplName() + "' is not supported yet");
         }
     }
 }
diff --git 
a/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java
 
b/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java
index 9affa58..b3da553 100644
--- 
a/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java
+++ 
b/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportImplChoiceTest.java
@@ -58,11 +58,11 @@ class BlobExportImplChoiceTest {
         configuration.addProperty("blob.export.implementation", "localFile");
 
         assertThat(BlobExportImplChoice.from(configuration))
-            .contains(BlobExportImplChoice.localFile());
+            .contains(BlobExportImplChoice.LOCAL_FILE);
     }
 
     @Test
-    void fromShouldThrowWhenCaseInSensitive() {
+    void fromShouldThrowWhenCaseInsensitive() {
         PropertiesConfiguration configuration = new PropertiesConfiguration();
         configuration.addProperty("blob.export.implementation", "localFILE");
 
@@ -76,6 +76,6 @@ class BlobExportImplChoiceTest {
         configuration.addProperty("blob.export.implementation", "  localFile   
");
 
         assertThat(BlobExportImplChoice.from(configuration))
-            .contains(BlobExportImplChoice.localFile());
+            .contains(BlobExportImplChoice.LOCAL_FILE);
     }
 }
\ No newline at end of file
diff --git 
a/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportMechanismModuleTest.java
 
b/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportMechanismModuleTest.java
index e9dbdd2..e011a90 100644
--- 
a/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportMechanismModuleTest.java
+++ 
b/server/container/guice/blob-export-guice/src/test/java/org/apache/james/modules/BlobExportMechanismModuleTest.java
@@ -49,7 +49,7 @@ class BlobExportMechanismModuleTest {
         FakePropertiesProvider noConfigurationFile = 
FakePropertiesProvider.builder().build();
 
         assertThat(module.provideChoice(noConfigurationFile))
-            .isEqualTo(BlobExportImplChoice.localFile());
+            .isEqualTo(BlobExportImplChoice.LOCAL_FILE);
     }
 
     @Test
@@ -62,7 +62,7 @@ class BlobExportMechanismModuleTest {
             .build();
 
         assertThat(module.provideChoice(noConfigurationFile))
-            .isEqualTo(BlobExportImplChoice.localFile());
+            .isEqualTo(BlobExportImplChoice.LOCAL_FILE);
     }
 
     @Test
@@ -79,19 +79,19 @@ class BlobExportMechanismModuleTest {
     }
 
     @Test
-    void provideChoiceShouldThrowWhenConfigurationIsMissing() throws Exception 
{
+    void provideChoiceShouldReturnDefaultWhenConfigurationIsMissing() throws 
Exception {
         PropertiesConfiguration configuration = new PropertiesConfiguration();
         FakePropertiesProvider noConfigurationFile = 
FakePropertiesProvider.builder()
             .register(NAME, configuration)
             .build();
 
         assertThat(module.provideChoice(noConfigurationFile))
-            .isEqualTo(BlobExportImplChoice.localFile());
+            .isEqualTo(BlobExportImplChoice.LOCAL_FILE);
     }
 
     @Test
     void provideMechanismShouldProvideFileExportWhenPassingLocalFileChoice() {
-        assertThat(module.provideMechanism(BlobExportImplChoice.localFile(), 
LOCAL_FILE_EXPORT_PROVIDER))
+        assertThat(module.provideMechanism(BlobExportImplChoice.LOCAL_FILE, 
LOCAL_FILE_EXPORT_PROVIDER))
             .isEqualTo(LOCAL_FILE_EXPORT);
     }
 }
\ No newline at end of file


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