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 2019/11/14 02:09:42 UTC

[james-project] 25/32: [REFACTORING] StandardMailboxMetaDataComparator should rely on Java 8

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 f9850ee4486a50cccdbb61690fd86d74c0ae47ce
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Nov 8 12:44:58 2019 +0700

    [REFACTORING] StandardMailboxMetaDataComparator should rely on Java 8
---
 .../mailbox/StandardMailboxMetaDataComparator.java | 66 ----------------------
 .../james/mailbox/model/MailboxMetaData.java       |  9 ++-
 .../james/mailbox/store/StoreMailboxManager.java   |  3 +-
 3 files changed, 7 insertions(+), 71 deletions(-)

diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/StandardMailboxMetaDataComparator.java b/mailbox/api/src/main/java/org/apache/james/mailbox/StandardMailboxMetaDataComparator.java
deleted file mode 100644
index 7aa0ea4..0000000
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/StandardMailboxMetaDataComparator.java
+++ /dev/null
@@ -1,66 +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.mailbox;
-
-import java.io.Serializable;
-import java.util.Comparator;
-
-import org.apache.james.mailbox.model.MailboxConstants;
-import org.apache.james.mailbox.model.MailboxMetaData;
-
-/**
- * Orders by name with INBOX first.
- */
-public class StandardMailboxMetaDataComparator implements Comparator<MailboxMetaData>, Serializable {
-
-    private static final long serialVersionUID = 1L;
-
-    /**
-     * Static comparison.
-     * 
-     * @param one
-     *            possibly null
-     * @param two
-     *            possibly null
-     * @return {@link Comparator#compare(Object, Object)}
-     */
-    public static int order(MailboxMetaData one, MailboxMetaData two) {
-        final String nameTwo = two.getPath().getName();
-        final int result;
-        final String nameOne = one.getPath().getName();
-        if (MailboxConstants.INBOX.equals(nameOne)) {
-            result = MailboxConstants.INBOX.equals(nameTwo) ? 0 : -1;
-        } else if (MailboxConstants.INBOX.equals(nameTwo)) {
-            result = 1;
-        } else if (nameOne == null) {
-            result = nameTwo == null ? 0 : 1;
-        } else if (nameTwo == null) {
-            result = -1;
-        } else {
-            result = nameOne.compareTo(nameTwo);
-        }
-        return result;
-    }
-
-    @Override
-    public int compare(MailboxMetaData one, MailboxMetaData two) {
-        return order(one, two);
-    }
-
-}
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxMetaData.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxMetaData.java
index 7dc49c0..0ec6c0f 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxMetaData.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxMetaData.java
@@ -19,10 +19,9 @@
 
 package org.apache.james.mailbox.model;
 
+import java.util.Comparator;
 import java.util.Objects;
 
-import org.apache.james.mailbox.StandardMailboxMetaDataComparator;
-
 import com.google.common.base.MoreObjects;
 
 /**
@@ -65,6 +64,10 @@ public class MailboxMetaData implements Comparable<MailboxMetaData> {
         return new MailboxMetaData(path, mailboxId, delimiter, Children.CHILDREN_ALLOWED_BUT_UNKNOWN, Selectability.NONE);
     }
 
+    public static  final Comparator<MailboxMetaData> COMPARATOR = Comparator
+        .<MailboxMetaData, Boolean>comparing(metadata -> metadata.getPath().isInbox()).reversed()
+        .thenComparing(metadata -> metadata.getPath().getName());
+
     private final MailboxPath path;
     private final char delimiter;
     private final Children inferiors;
@@ -141,6 +144,6 @@ public class MailboxMetaData implements Comparable<MailboxMetaData> {
 
     @Override
     public int compareTo(MailboxMetaData o) {
-        return StandardMailboxMetaDataComparator.order(this, o);
+        return COMPARATOR.compare(this, o);
     }
 }
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
index b40eb95..86f3ebe 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
@@ -40,7 +40,6 @@ import org.apache.james.mailbox.MailboxPathLocker.LockAwareExecution;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.MetadataWithMailboxId;
-import org.apache.james.mailbox.StandardMailboxMetaDataComparator;
 import org.apache.james.mailbox.events.EventBus;
 import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
 import org.apache.james.mailbox.exception.HasEmptyMailboxNameInHierarchyException;
@@ -589,7 +588,7 @@ public class StoreMailboxManager implements MailboxManager {
             .stream()
             .filter(mailboxQuery::matches)
             .map(mailbox -> toMailboxMetadata(session, mailboxes, mailbox))
-            .sorted(new StandardMailboxMetaDataComparator())
+            .sorted(MailboxMetaData.COMPARATOR)
             .collect(Guavate.toImmutableList());
     }
 


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