You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2016/09/29 10:52:01 UTC

[11/12] james-project git commit: POC with bifunction

POC with bifunction


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

Branch: refs/heads/master
Commit: 1ebb82caa9f7948a7d9d890d3b40f95cef660901
Parents: 5cfe856
Author: Matthieu Baechler <ma...@linagora.com>
Authored: Tue Sep 27 20:45:34 2016 +0200
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Thu Sep 29 12:51:00 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/james/util/ValuePatch.java  |  1 +
 .../vacation/CassandraVacationDAO.java          | 46 ++++++++++----------
 2 files changed, 25 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1ebb82ca/server/container/util-java8/src/main/java/org/apache/james/util/ValuePatch.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/ValuePatch.java b/server/container/util-java8/src/main/java/org/apache/james/util/ValuePatch.java
index 5c3d01a..27a065f 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/ValuePatch.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/ValuePatch.java
@@ -22,6 +22,7 @@ package org.apache.james.util;
 import java.util.NoSuchElementException;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.function.BiFunction;
 import java.util.function.Function;
 
 import com.google.common.base.Preconditions;

http://git-wip-us.apache.org/repos/asf/james-project/blob/1ebb82ca/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationDAO.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationDAO.java b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationDAO.java
index 6cecbcb..8394be1 100644
--- a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationDAO.java
+++ b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationDAO.java
@@ -27,6 +27,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
 import java.time.ZonedDateTime;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
+import java.util.function.BiFunction;
 import java.util.function.Function;
 
 import javax.inject.Inject;
@@ -39,7 +40,6 @@ import org.apache.james.jmap.api.vacation.AccountId;
 import org.apache.james.jmap.api.vacation.Vacation;
 import org.apache.james.jmap.api.vacation.VacationPatch;
 import org.apache.james.jmap.cassandra.vacation.tables.CassandraVacationTable;
-import org.apache.james.util.FunctionGenerator;
 import org.apache.james.util.ValuePatch;
 
 import com.datastax.driver.core.PreparedStatement;
@@ -55,7 +55,7 @@ public class CassandraVacationDAO {
     private final CassandraAsyncExecutor cassandraAsyncExecutor;
     private final PreparedStatement readStatement;
     private final UserType zonedDateTimeUserType;
-    private final FunctionGenerator<VacationPatch, Insert> insertGeneratorPipeline;
+    private final BiFunction<VacationPatch, Insert, Insert> insertGeneratorPipeline;
 
     @Inject
     public CassandraVacationDAO(Session session, CassandraTypesProvider cassandraTypesProvider) {
@@ -67,16 +67,16 @@ public class CassandraVacationDAO {
             .where(eq(CassandraVacationTable.ACCOUNT_ID,
                 bindMarker(CassandraVacationTable.ACCOUNT_ID))));
 
-        insertGeneratorPipeline = ImmutableList.<FunctionGenerator<VacationPatch, Insert>>of(
-            patch -> applyPatchForField(CassandraVacationTable.SUBJECT, patch.getSubject()),
-            patch -> applyPatchForField(CassandraVacationTable.HTML, patch.getHtmlBody()),
-            patch -> applyPatchForField(CassandraVacationTable.TEXT, patch.getTextBody()),
-            patch -> applyPatchForField(CassandraVacationTable.IS_ENABLED, patch.getIsEnabled()),
-            patch -> applyPatchForFieldZonedDateTime(CassandraVacationTable.FROM_DATE, patch.getFromDate()),
-            patch -> applyPatchForFieldZonedDateTime(CassandraVacationTable.TO_DATE, patch.getToDate()))
+        insertGeneratorPipeline = ImmutableList.of(
+            applyPatchForField(CassandraVacationTable.SUBJECT, VacationPatch::getSubject),
+            applyPatchForField(CassandraVacationTable.HTML, VacationPatch::getHtmlBody),
+            applyPatchForField(CassandraVacationTable.TEXT, VacationPatch::getTextBody),
+            applyPatchForField(CassandraVacationTable.IS_ENABLED, VacationPatch::getIsEnabled),
+            applyPatchForFieldZonedDateTime(CassandraVacationTable.FROM_DATE, VacationPatch::getFromDate),
+            applyPatchForFieldZonedDateTime(CassandraVacationTable.TO_DATE, VacationPatch::getToDate))
             .stream()
-            .reduce(FunctionGenerator::composeGeneratedFunctions)
-            .get();
+            .reduce((vacation, insert) -> insert, 
+                    (a, b) -> (vacation, insert) -> b.apply(vacation, a.apply(vacation, insert)));
     }
 
     public CompletableFuture<Void> modifyVacation(AccountId accountId, VacationPatch vacationPatch) {
@@ -108,23 +108,25 @@ public class CassandraVacationDAO {
     }
 
     private Insert createSpecificUpdate(VacationPatch vacationPatch, Insert baseInsert) {
-        return insertGeneratorPipeline
-            .apply(vacationPatch)
-            .apply(baseInsert);
+        return insertGeneratorPipeline.apply(vacationPatch, baseInsert);
     }
 
-    public <T> Function<Insert, Insert> applyPatchForField(String field, ValuePatch<T> valuePatch) {
-        return valuePatch.mapNotKeptToOptional(optionalValue -> applyPatchForField(field, optionalValue))
-            .orElse(Function.identity());
+    public <T> BiFunction<VacationPatch, Insert, Insert> applyPatchForField(String field, Function<VacationPatch, ValuePatch<T>> getter) {
+        return (vacation, insert) -> 
+            getter.apply(vacation)
+                .mapNotKeptToOptional(optionalValue -> applyPatchForField(field, optionalValue, insert))
+                .orElse(insert);
     }
 
-    public Function<Insert, Insert> applyPatchForFieldZonedDateTime(String field, ValuePatch<ZonedDateTime> valuePatch) {
-        return valuePatch.mapNotKeptToOptional(optionalValue -> applyPatchForField(field, convertToUDTOptional(optionalValue)))
-            .orElse(Function.identity());
+    public BiFunction<VacationPatch, Insert, Insert> applyPatchForFieldZonedDateTime(String field, Function<VacationPatch, ValuePatch<ZonedDateTime>> getter) {
+        return (vacation, insert) -> 
+            getter.apply(vacation)
+                .mapNotKeptToOptional(optionalValue -> applyPatchForField(field, convertToUDTOptional(optionalValue), insert))
+                .orElse(insert);
     }
 
-    private <T> Function<Insert, Insert> applyPatchForField(String field, Optional<T> value) {
-        return insert -> insert.value(field, value.orElse(null));
+    private <T> Insert applyPatchForField(String field, Optional<T> value, Insert insert) {
+        return insert.value(field, value.orElse(null));
     }
 
     private Optional<UDTValue> convertToUDTOptional(Optional<ZonedDateTime> zonedDateTimeOptional) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org