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 ro...@apache.org on 2019/08/01 07:22:24 UTC

[james-project] 02/09: JAMES-2813 Migrate TaskExecutionDetails to Scala case class

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

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

commit 9723959b6e98b7068977ef7e8d62ac7d0454c329
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Mon Jul 29 15:41:05 2019 +0200

    JAMES-2813 Migrate TaskExecutionDetails to Scala case class
---
 .../org/apache/james/task/MemoryTaskManager.java   |   2 +-
 .../apache/james/task/TaskExecutionDetails.java    | 192 ---------------------
 .../apache/james/task/TaskExecutionDetails.scala   | 146 ++++++++++++++++
 .../TaskExecutionDetailsProjection.scala           |  10 +-
 4 files changed, 152 insertions(+), 198 deletions(-)

diff --git a/server/task/src/main/java/org/apache/james/task/MemoryTaskManager.java b/server/task/src/main/java/org/apache/james/task/MemoryTaskManager.java
index 6b4574f..2ac480e 100644
--- a/server/task/src/main/java/org/apache/james/task/MemoryTaskManager.java
+++ b/server/task/src/main/java/org/apache/james/task/MemoryTaskManager.java
@@ -46,7 +46,7 @@ public class MemoryTaskManager implements TaskManager {
 
         @Override
         public void started() {
-            updater.accept(TaskExecutionDetails::start);
+            updater.accept(TaskExecutionDetails::started);
         }
 
         @Override
diff --git a/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java b/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java
deleted file mode 100644
index a75b9e7..0000000
--- a/server/task/src/main/java/org/apache/james/task/TaskExecutionDetails.java
+++ /dev/null
@@ -1,192 +0,0 @@
-/****************************************************************
- * 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.task;
-
-import java.time.ZonedDateTime;
-import java.util.Optional;
-
-public class TaskExecutionDetails {
-
-    public interface AdditionalInformation {
-
-    }
-
-    public static TaskExecutionDetails from(Task task, TaskId id) {
-        return new TaskExecutionDetails(
-            id,
-            task,
-            TaskManager.Status.WAITING,
-            Optional.of(ZonedDateTime.now()),
-            Optional.empty(),
-            Optional.empty(),
-            Optional.empty(),
-            Optional.empty());
-    }
-
-    private final TaskId taskId;
-    private final Task task;
-    private final TaskManager.Status status;
-    private final Optional<ZonedDateTime> submitDate;
-    private final Optional<ZonedDateTime> startedDate;
-    private final Optional<ZonedDateTime> completedDate;
-    private final Optional<ZonedDateTime> canceledDate;
-    private final Optional<ZonedDateTime> failedDate;
-
-    public TaskExecutionDetails(TaskId taskId, Task task, TaskManager.Status status,
-                                Optional<ZonedDateTime> submitDate, Optional<ZonedDateTime> startedDate,
-                                Optional<ZonedDateTime> completedDate, Optional<ZonedDateTime> canceledDate,
-                                Optional<ZonedDateTime> failedDate) {
-        this.taskId = taskId;
-        this.task = task;
-        this.status = status;
-        this.submitDate = submitDate;
-        this.startedDate = startedDate;
-        this.completedDate = completedDate;
-        this.canceledDate = canceledDate;
-        this.failedDate = failedDate;
-    }
-
-    public TaskId getTaskId() {
-        return taskId;
-    }
-
-    public String getType() {
-        return task.type();
-    }
-
-    public TaskManager.Status getStatus() {
-        return status;
-    }
-
-    public Optional<AdditionalInformation> getAdditionalInformation() {
-        return task.details();
-    }
-
-    public Optional<ZonedDateTime> getSubmitDate() {
-        return submitDate;
-    }
-
-    public Optional<ZonedDateTime> getStartedDate() {
-        return startedDate;
-    }
-
-    public Optional<ZonedDateTime> getCompletedDate() {
-        return completedDate;
-    }
-
-    public Optional<ZonedDateTime> getCanceledDate() {
-        return canceledDate;
-    }
-
-    public Optional<ZonedDateTime> getFailedDate() {
-        return failedDate;
-    }
-
-    public TaskExecutionDetails start() {
-        switch (status) {
-            case WAITING:
-                return new TaskExecutionDetails(
-                    taskId,
-                    task,
-                    TaskManager.Status.IN_PROGRESS,
-                    submitDate,
-                    Optional.of(ZonedDateTime.now()),
-                    Optional.empty(),
-                    Optional.empty(),
-                    Optional.empty());
-            default:
-                return this;
-        }
-    }
-
-    public TaskExecutionDetails completed() {
-        switch (status) {
-            case IN_PROGRESS:
-            case CANCEL_REQUESTED:
-            case WAITING:
-                return new TaskExecutionDetails(
-                    taskId,
-                    task,
-                    TaskManager.Status.COMPLETED,
-                    submitDate,
-                    startedDate,
-                    Optional.of(ZonedDateTime.now()),
-                    Optional.empty(),
-                    Optional.empty());
-            default:
-                return this;
-        }
-    }
-
-    public TaskExecutionDetails failed() {
-        switch (status) {
-            case IN_PROGRESS:
-            case CANCEL_REQUESTED:
-                return new TaskExecutionDetails(
-                    taskId,
-                    task,
-                    TaskManager.Status.FAILED,
-                    submitDate,
-                    startedDate,
-                    Optional.empty(),
-                    Optional.empty(),
-                    Optional.of(ZonedDateTime.now()));
-            default:
-                return this;
-        }
-    }
-
-    public TaskExecutionDetails cancelRequested() {
-        switch (status) {
-            case IN_PROGRESS:
-            case WAITING:
-                return new TaskExecutionDetails(
-                    taskId,
-                    task,
-                    TaskManager.Status.CANCEL_REQUESTED,
-                    submitDate,
-                    startedDate,
-                    Optional.empty(),
-                    Optional.of(ZonedDateTime.now()),
-                    Optional.empty());
-            default:
-                return this;
-        }
-    }
-
-    public TaskExecutionDetails cancelEffectively() {
-        switch (status) {
-            case CANCEL_REQUESTED:
-            case IN_PROGRESS:
-            case WAITING:
-                return new TaskExecutionDetails(
-                    taskId,
-                    task,
-                    TaskManager.Status.CANCELLED,
-                    submitDate,
-                    startedDate,
-                    Optional.empty(),
-                    Optional.of(ZonedDateTime.now()),
-                    Optional.empty());
-            default:
-                return this;
-        }
-    }
-}
diff --git a/server/task/src/main/scala/org/apache/james/task/TaskExecutionDetails.scala b/server/task/src/main/scala/org/apache/james/task/TaskExecutionDetails.scala
new file mode 100644
index 0000000..082694c
--- /dev/null
+++ b/server/task/src/main/scala/org/apache/james/task/TaskExecutionDetails.scala
@@ -0,0 +1,146 @@
+/** **************************************************************
+ * 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.task
+
+import java.time.ZonedDateTime
+import java.util.{Objects, Optional}
+
+import org.apache.james.task.TaskManager.Status._
+
+import com.google.common.base.MoreObjects
+
+object TaskExecutionDetails {
+
+  trait AdditionalInformation {}
+
+  def from(task: Task, id: TaskId) = new TaskExecutionDetails(id, task.`type`, () => task.details, WAITING, submitDate = Optional.of(ZonedDateTime.now))
+}
+
+class TaskExecutionDetails(val taskId: TaskId,
+                           private val `type`: String,
+                           private val additionalInformation: () => Optional[TaskExecutionDetails.AdditionalInformation],
+                           private val status: TaskManager.Status,
+                           private val submitDate: Optional[ZonedDateTime] = Optional.empty(),
+                           private val startedDate: Optional[ZonedDateTime] = Optional.empty(),
+                           private val completedDate: Optional[ZonedDateTime] = Optional.empty(),
+                           private val canceledDate: Optional[ZonedDateTime] = Optional.empty(),
+                           private val failedDate: Optional[ZonedDateTime] = Optional.empty()) {
+  def getTaskId: TaskId = taskId
+
+  def getType: String = `type`
+
+  def getStatus: TaskManager.Status = status
+
+  def getAdditionalInformation: Optional[TaskExecutionDetails.AdditionalInformation] = additionalInformation()
+
+  def getSubmitDate: Optional[ZonedDateTime] = submitDate
+
+  def getStartedDate: Optional[ZonedDateTime] = startedDate
+
+  def getCompletedDate: Optional[ZonedDateTime] = completedDate
+
+  def getCanceledDate: Optional[ZonedDateTime] = canceledDate
+
+  def getFailedDate: Optional[ZonedDateTime] = failedDate
+
+  def started: TaskExecutionDetails = status match {
+    case WAITING => start
+    case _ => this
+  }
+
+  def completed: TaskExecutionDetails = status match {
+    case IN_PROGRESS => complete
+    case CANCEL_REQUESTED => complete
+    case WAITING => complete
+    case _ => this
+  }
+
+  def failed: TaskExecutionDetails = status match {
+    case IN_PROGRESS => fail
+    case CANCEL_REQUESTED => fail
+    case _ => this
+  }
+
+  def cancelRequested: TaskExecutionDetails = status match {
+    case IN_PROGRESS => requestCancel
+    case WAITING => requestCancel
+    case _ => this
+  }
+
+  def cancelEffectively: TaskExecutionDetails = status match {
+    case CANCEL_REQUESTED => cancel
+    case IN_PROGRESS => cancel
+    case WAITING => cancel
+    case _ => this
+  }
+
+  def canEqual(other: Any): Boolean = other.isInstanceOf[TaskExecutionDetails]
+
+  override def equals(other: Any): Boolean = other match {
+    case that: TaskExecutionDetails =>
+      (that canEqual this) &&
+        Objects.equals(taskId, that.taskId) &&
+        Objects.equals(`type`, that.`type`) &&
+        Objects.equals(additionalInformation(), that.additionalInformation()) &&
+        Objects.equals(status, that.status) &&
+        Objects.equals(submitDate, that.submitDate) &&
+        Objects.equals(startedDate, that.startedDate) &&
+        Objects.equals(completedDate, that.completedDate) &&
+        Objects.equals(canceledDate, that.canceledDate) &&
+        Objects.equals(failedDate, that.failedDate)
+    case _ => false
+  }
+
+  override def hashCode(): Int =
+    Objects.hash(taskId, `type`, additionalInformation(), status, submitDate, startedDate, completedDate, canceledDate, failedDate)
+
+  override def toString: String =
+    MoreObjects.toStringHelper(this)
+      .add("taskId", taskId)
+      .add("type", `type`)
+      .add("", additionalInformation())
+      .add("", status)
+      .add("", submitDate)
+      .add("", startedDate)
+      .add("", completedDate)
+      .add("", canceledDate)
+      .add("", failedDate)
+      .toString
+
+  private def start = new TaskExecutionDetails(taskId, `type`, additionalInformation, IN_PROGRESS,
+    submitDate = submitDate,
+    startedDate = Optional.of(ZonedDateTime.now))
+  private def complete = new TaskExecutionDetails(taskId, `type`, additionalInformation, TaskManager.Status.COMPLETED,
+    submitDate = submitDate,
+    startedDate = startedDate,
+    completedDate = Optional.of(ZonedDateTime.now))
+  private def fail = new TaskExecutionDetails(taskId, `type`, additionalInformation, TaskManager.Status.FAILED,
+    submitDate = submitDate,
+    startedDate = startedDate,
+    failedDate = Optional.of(ZonedDateTime.now))
+  private def requestCancel = new TaskExecutionDetails(taskId, `type`, additionalInformation, TaskManager.Status.CANCEL_REQUESTED,
+    submitDate = submitDate,
+    startedDate = startedDate,
+    canceledDate = Optional.of(ZonedDateTime.now))
+  private def cancel = new TaskExecutionDetails(taskId, `type`, additionalInformation, TaskManager.Status.CANCELLED,
+    submitDate = submitDate,
+    startedDate = startedDate,
+    canceledDate = Optional.of(ZonedDateTime.now))
+}
diff --git a/server/task/src/main/scala/org/apache/james/task/eventsourcing/TaskExecutionDetailsProjection.scala b/server/task/src/main/scala/org/apache/james/task/eventsourcing/TaskExecutionDetailsProjection.scala
index 32ab114..18c65dc 100644
--- a/server/task/src/main/scala/org/apache/james/task/eventsourcing/TaskExecutionDetailsProjection.scala
+++ b/server/task/src/main/scala/org/apache/james/task/eventsourcing/TaskExecutionDetailsProjection.scala
@@ -28,15 +28,15 @@ trait TaskExecutionDetailsProjection {
     case created: Created =>
       update(created.getAggregateId.taskId, TaskExecutionDetails.from(created.task, created.aggregateId.taskId))
     case cancelRequested: CancelRequested =>
-      update(cancelRequested.aggregateId.taskId)(_.cancelRequested())
+      update(cancelRequested.aggregateId.taskId)(_.cancelRequested)
     case started: Started =>
-      update(started.aggregateId.taskId)(_.start())
+      update(started.aggregateId.taskId)(_.started)
     case completed: Completed =>
-      update(completed.aggregateId.taskId)(_.completed())
+      update(completed.aggregateId.taskId)(_.completed)
     case failed: Failed =>
-      update(failed.aggregateId.taskId)(_.failed())
+      update(failed.aggregateId.taskId)(_.failed)
     case canceled: Cancelled =>
-      update(canceled.aggregateId.taskId)(_.cancelEffectively())
+      update(canceled.aggregateId.taskId)(_.cancelEffectively)
   }
 
   private def update(taskId: TaskId)(updater: TaskExecutionDetails => TaskExecutionDetails): Unit =


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