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 2020/07/23 07:53:20 UTC

[james-project] 09/18: JAMES-3177 use vavr pattern matching to simplify the event handling code

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 0747959b68159b53a84f444ae4f0d53972bff3d0
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Tue Jun 16 18:07:25 2020 +0200

    JAMES-3177 use vavr pattern matching to simplify the event handling code
---
 .../imap/processor/base/SelectedMailboxImpl.java   | 58 ++++++++++++----------
 1 file changed, 31 insertions(+), 27 deletions(-)

diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java
index f7180a2..91fe286 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java
@@ -19,6 +19,11 @@
 
 package org.apache.james.imap.processor.base;
 
+import static io.vavr.API.$;
+import static io.vavr.API.Case;
+import static io.vavr.API.Match;
+import static io.vavr.Predicates.instanceOf;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -63,6 +68,8 @@ import reactor.core.scheduler.Schedulers;
 public class SelectedMailboxImpl implements SelectedMailbox, MailboxListener {
 
 
+    private static final Void VOID = null;
+
     @VisibleForTesting
     static class ApplicableFlags {
         static ApplicableFlags from(Flags flags) {
@@ -362,37 +369,35 @@ public class SelectedMailboxImpl implements SelectedMailbox, MailboxListener {
     private void mailboxEvent(MailboxEvent mailboxEvent) {
         // Check if the event was for the mailbox we are observing
         if (mailboxEvent.getMailboxId().equals(getMailboxId())) {
-            MailboxSession.SessionId eventSessionId = mailboxEvent.getSessionId();
-            if (mailboxEvent instanceof MessageEvent) {
-                final MessageEvent messageEvent = (MessageEvent) mailboxEvent;
-                if (messageEvent instanceof Added) {
-                    final Collection<MessageUid> uids = ((Added) mailboxEvent).getUids();
-                    handleAddition(uids);
-                } else if (messageEvent instanceof FlagsUpdated) {
-                    FlagsUpdated updated = (FlagsUpdated) messageEvent;
-                    handleFlagsUpdates(mailboxEvent, eventSessionId, (FlagsUpdated) messageEvent, updated);
-                } else if (messageEvent instanceof Expunged) {
-                    handleMailboxExpunge(messageEvent);
-                }
-            } else if (mailboxEvent instanceof MailboxDeletion) {
-                handleMailboxDeletion(eventSessionId);
-            }
+            Match(mailboxEvent).of(
+                Case($(instanceOf(Added.class)),
+                    this::handleAddition),
+                Case($(instanceOf(FlagsUpdated.class)),
+                    this::handleFlagsUpdates),
+                Case($(instanceOf(Expunged.class)),
+                    this::handleMailboxExpunge),
+                Case($(instanceOf(MailboxDeletion.class)),
+                    this::handleMailboxDeletion),
+                Case($(), VOID)
+            );
         }
     }
 
-    private void handleMailboxDeletion(MailboxSession.SessionId eventSessionId) {
-        if (eventSessionId != sessionId) {
+    private Void handleMailboxDeletion(MailboxDeletion mailboxDeletion) {
+        if (mailboxDeletion.getSessionId() != sessionId) {
             isDeletedByOtherSession = true;
         }
+        return VOID;
     }
 
-    private void handleMailboxExpunge(MessageEvent messageEvent) {
+    private Void handleMailboxExpunge(MessageEvent messageEvent) {
         expungedUids.addAll(messageEvent.getUids());
+        return VOID;
     }
 
-    private void handleFlagsUpdates(MailboxEvent mailboxEvent, MailboxSession.SessionId eventSessionId, FlagsUpdated messageEvent, FlagsUpdated updated) {
+    private Void handleFlagsUpdates(FlagsUpdated updated) {
         List<UpdatedFlags> uFlags = updated.getUpdatedFlags();
-        if (sessionId != eventSessionId || !silentFlagChanges) {
+        if (sessionId != updated.getSessionId() || !silentFlagChanges) {
 
             for (UpdatedFlags u : uFlags) {
                 if (interestingFlags(u)) {
@@ -414,28 +419,27 @@ public class SelectedMailboxImpl implements SelectedMailbox, MailboxListener {
                 while (flags.hasNext()) {
                     if (Flag.RECENT.equals(flags.next())) {
                         MailboxId id = sm.getMailboxId();
-                        if (id != null && id.equals(mailboxEvent.getMailboxId())) {
+                        if (id != null && id.equals(updated.getMailboxId())) {
                             sm.addRecent(u.getUid());
                         }
                     }
                 }
-
-
             }
         }
-
-        applicableFlags = updateApplicableFlags(applicableFlags, messageEvent);
+        applicableFlags = updateApplicableFlags(applicableFlags, updated);
+        return VOID;
     }
 
-    private void handleAddition(Collection<MessageUid> uids) {
+    private Void handleAddition(Added added) {
         sizeChanged = true;
         SelectedMailbox sm = session.getSelected();
-        for (MessageUid uid : uids) {
+        for (MessageUid uid : added.getUids()) {
             uidMsnConverter.addUid(uid);
             if (sm != null) {
                 sm.addRecent(uid);
             }
         }
+        return VOID;
     }
 
     @VisibleForTesting


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