james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [25/27] james-project git commit: JAMES-2132 More functional style on MDN reports formatting
Date Fri, 01 Sep 2017 01:18:38 GMT
JAMES-2132 More functional style on MDN reports formatting


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

Branch: refs/heads/master
Commit: 599bec32c02a181f3713c0849d15dc0f10974609
Parents: 1f443cd
Author: benwa <btellier@linagora.com>
Authored: Wed Aug 30 16:15:53 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Fri Sep 1 08:16:14 2017 +0700

----------------------------------------------------------------------
 mdn/pom.xml                                     |  4 ++
 .../java/org/apache/james/mdn/MDNReport.java    | 47 +++++++++-----------
 2 files changed, 26 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/599bec32/mdn/pom.xml
----------------------------------------------------------------------
diff --git a/mdn/pom.xml b/mdn/pom.xml
index 7eb7d4b..9e8441a 100644
--- a/mdn/pom.xml
+++ b/mdn/pom.xml
@@ -34,6 +34,10 @@
 
     <dependencies>
         <dependency>
+            <groupId>org.apache.james</groupId>
+            <artifactId>james-server-util-java8</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/599bec32/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
----------------------------------------------------------------------
diff --git a/mdn/src/main/java/org/apache/james/mdn/MDNReport.java b/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
index 9682485..2d84c86 100644
--- a/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
+++ b/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
@@ -23,16 +23,19 @@ import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.james.mdn.fields.Disposition;
 import org.apache.james.mdn.fields.Error;
 import org.apache.james.mdn.fields.ExtensionField;
+import org.apache.james.mdn.fields.Field;
 import org.apache.james.mdn.fields.FinalRecipient;
 import org.apache.james.mdn.fields.Gateway;
 import org.apache.james.mdn.fields.OriginalMessageId;
 import org.apache.james.mdn.fields.OriginalRecipient;
 import org.apache.james.mdn.fields.ReportingUserAgent;
 import org.apache.james.mdn.fields.Text;
+import org.apache.james.util.OptionalUtils;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
@@ -217,34 +220,28 @@ public class MDNReport {
     }
 
     public String formattedValue() {
-        return reportingUserAgentField.map(value -> value.formattedValue() + LINE_END).orElse("")
-            + gatewayField.map(value -> value.formattedValue() + LINE_END).orElse("")
-            + originalRecipientField.map(value -> value.formattedValue() + LINE_END).orElse("")
-            + finalRecipientField.formattedValue() + LINE_END
-            + originalMessageIdField.map(value -> value.formattedValue() + LINE_END).orElse("")
-            + dispositionField.formattedValue() + LINE_END
-            + formatErrors()
-            + formattedExtensionValue();
-    }
-
-    private String formatErrors() {
-        if (errorFields.isEmpty()) {
-            return "";
-        }
-        return errorFields.stream()
-            .map(Error::formattedValue)
+        Stream<Optional<? extends Field>> definedFields =
+            Stream.of(
+                reportingUserAgentField,
+                gatewayField,
+                originalRecipientField,
+                Optional.of(finalRecipientField),
+                originalMessageIdField,
+                Optional.of(dispositionField));
+        Stream<Optional<? extends Field>> errors =
+            errorFields.stream().map(Optional::of);
+        Stream<Optional<? extends Field>> extentions =
+            extensionFields.stream().map(Optional::of);
+
+        return Stream.concat(
+            definedFields,
+            Stream.concat(errors,
+                extentions))
+            .flatMap(OptionalUtils::toStream)
+            .map(Field::formattedValue)
             .collect(Collectors.joining(LINE_END)) + LINE_END;
     }
 
-    private String formattedExtensionValue() {
-        if (extensionFields.isEmpty()) {
-            return "";
-        }
-        return extensionFields.stream()
-            .map(ExtensionField::formattedValue)
-            .collect(Collectors.joining(EXTENSION_DELIMITER)) + LINE_END;
-    }
-
     @Override
     public final boolean equals(Object o) {
         if (o instanceof MDNReport) {


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