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:40 UTC

[james-project] 23/32: [REFACTORING] Search comparators 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 83f4e820f72f0869f237e5ba66b3057247dcde24
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Nov 8 12:35:29 2019 +0700

    [REFACTORING] Search comparators should rely on Java 8
---
 .../search/comparator/CombinedComparator.java      | 10 ++--
 .../search/comparator/InternalDateComparator.java  | 36 --------------
 ...SizeComparator.java => MessageComparators.java} | 18 ++++---
 .../search/comparator/MessageIdComparator.java     | 35 --------------
 .../store/search/comparator/ReverseComparator.java | 56 ----------------------
 .../store/search/comparator/UidComparator.java     | 36 --------------
 .../store/search/CombinedComparatorTest.java       | 30 +++++++-----
 7 files changed, 32 insertions(+), 189 deletions(-)

diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
index 3c8d53e..cb44353 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
@@ -51,19 +51,19 @@ public class CombinedComparator implements Comparator<MailboxMessage> {
     private static Comparator<MailboxMessage> toComparator(Sort sort) {
         switch (sort.getSortClause()) {
             case Arrival:
-                return InternalDateComparator.INTERNALDATE;
+                return MessageComparators.INTERNAL_DATE_COMPARATOR;
             case MailboxCc:
                 return HeaderMailboxComparator.CC_COMPARATOR;
             case MailboxFrom:
                 return HeaderMailboxComparator.FROM_COMPARATOR;
             case Size:
-                return SizeComparator.SIZE;
+                return MessageComparators.SIZE_COMPARATOR;
             case BaseSubject:
                 return BaseSubjectComparator.BASESUBJECT;
             case MailboxTo:
                 return HeaderMailboxComparator.TO_COMPARATOR;
             case Uid:
-                return UidComparator.UID;
+                return MessageComparators.UID_COMPARATOR;
             case SentDate:
                 return SentDateComparator.SENTDATE;
             case DisplayFrom:
@@ -71,7 +71,7 @@ public class CombinedComparator implements Comparator<MailboxMessage> {
             case DisplayTo:
                 return HeaderDisplayComparator.TO_COMPARATOR;
             case Id:
-                return MessageIdComparator.MESSAGE_ID_COMPARATOR;
+                return MessageComparators.MESSAGE_ID_COMPARATOR;
             default:
                 throw new NotImplementedException("Combined comparator does not support sort " + sort.getSortClause());
         }
@@ -79,7 +79,7 @@ public class CombinedComparator implements Comparator<MailboxMessage> {
 
     private static Comparator<MailboxMessage> optionalResverse(Comparator<MailboxMessage> comparator, boolean isReverse) {
         if (isReverse) {
-            return new ReverseComparator(comparator);
+            return comparator.reversed();
         }
         return comparator;
     }
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
deleted file mode 100644
index 7db51ca..0000000
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/InternalDateComparator.java
+++ /dev/null
@@ -1,36 +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.store.search.comparator;
-
-import java.util.Comparator;
-
-import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-
-/**
- * {@link Comparator} which compares {@link MailboxMessage}'s with their {@link MailboxMessage#getInternalDate()} value
- */
-public class InternalDateComparator  implements Comparator<MailboxMessage> {
-    public static final Comparator<MailboxMessage> INTERNALDATE = new InternalDateComparator();
-
-    @Override
-    public int compare(MailboxMessage o1, MailboxMessage o2) {
-        return (o1.getInternalDate().compareTo(o2.getInternalDate()));
-    }
-
-}
\ No newline at end of file
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/MessageComparators.java
similarity index 71%
rename from mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
rename to mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/MessageComparators.java
index ccb7180..ab72a6d 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SizeComparator.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/MessageComparators.java
@@ -16,20 +16,18 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
+
 package org.apache.james.mailbox.store.search.comparator;
 
 import java.util.Comparator;
+import java.util.Locale;
 
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 
-/**
- * {@link Comparator} which compares {@link MailboxMessage}'s with their {@link MailboxMessage#getFullContentOctets()} value
- */
-public class SizeComparator implements Comparator<MailboxMessage> {
-    public static final Comparator<MailboxMessage> SIZE = new SizeComparator();
-
-    @Override
-    public int compare(MailboxMessage o1, MailboxMessage o2) {
-        return (int) (o1.getFullContentOctets() - o2.getFullContentOctets());
-    }
+public interface MessageComparators {
+    Comparator<MailboxMessage> UID_COMPARATOR = Comparator.comparing(MailboxMessage::getUid);
+    Comparator<MailboxMessage> SIZE_COMPARATOR = Comparator.comparing(MailboxMessage::getFullContentOctets);
+    Comparator<MailboxMessage> INTERNAL_DATE_COMPARATOR = Comparator.comparing(MailboxMessage::getInternalDate);
+    Comparator<MailboxMessage> MESSAGE_ID_COMPARATOR = Comparator.comparing(mailboxMessage ->
+        mailboxMessage.getMessageId().serialize().toLowerCase(Locale.US));
 }
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/MessageIdComparator.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/MessageIdComparator.java
deleted file mode 100644
index f65064e..0000000
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/MessageIdComparator.java
+++ /dev/null
@@ -1,35 +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.store.search.comparator;
-
-import java.util.Comparator;
-
-import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-
-public class MessageIdComparator implements Comparator<MailboxMessage> {
-    public static final MessageIdComparator MESSAGE_ID_COMPARATOR = new MessageIdComparator();
-
-    @Override
-    public int compare(MailboxMessage mailboxMessage1, MailboxMessage mailboxMessage2) {
-        return mailboxMessage1.getMessageId()
-            .serialize()
-            .compareToIgnoreCase(mailboxMessage2.getMessageId().serialize());
-    }
-}
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
deleted file mode 100644
index 8abc477..0000000
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/ReverseComparator.java
+++ /dev/null
@@ -1,56 +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.store.search.comparator;
-
-import java.util.Comparator;
-
-import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-
-import com.google.common.base.Objects;
-
-/**
- * {@link Comparator} which wraps an other {@link Comparator} and reverse it
- */
-public class ReverseComparator implements Comparator<MailboxMessage> {
-
-    private final Comparator<MailboxMessage> comparator;
-
-    public ReverseComparator(Comparator<MailboxMessage> comparator) {
-        this.comparator = comparator;
-    }
-
-    @Override
-    public int compare(MailboxMessage o1, MailboxMessage o2) {
-        return comparator.compare(o2, o1);
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (o instanceof ReverseComparator) {
-            ReverseComparator that = (ReverseComparator) o;
-            return Objects.equal(this.comparator, that.comparator);
-        }
-        return false;
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hashCode(comparator);
-    }
-}
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
deleted file mode 100644
index 5081e03..0000000
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/comparator/UidComparator.java
+++ /dev/null
@@ -1,36 +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.store.search.comparator;
-
-import java.util.Comparator;
-
-import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-
-/**
- * {@link Comparator} which compares {@link MailboxMessage}'s with their {@link MailboxMessage#getUid()} value
- */
-public class UidComparator implements Comparator<MailboxMessage> {
-    public static final Comparator<MailboxMessage> UID = new UidComparator();
-
-    @Override
-    public int compare(MailboxMessage o1, MailboxMessage o2) {
-        return o1.getUid().compareTo(o2.getUid());
-    }
-
-}
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
index 9277646..2fecfbb 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/CombinedComparatorTest.java
@@ -20,21 +20,23 @@
 package org.apache.james.mailbox.store.search;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import java.util.Comparator;
+
+import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.model.SearchQuery;
 import org.apache.james.mailbox.model.SearchQuery.Sort;
 import org.apache.james.mailbox.model.SearchQuery.Sort.Order;
 import org.apache.james.mailbox.model.SearchQuery.Sort.SortClause;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.search.comparator.BaseSubjectComparator;
 import org.apache.james.mailbox.store.search.comparator.CombinedComparator;
 import org.apache.james.mailbox.store.search.comparator.HeaderDisplayComparator;
 import org.apache.james.mailbox.store.search.comparator.HeaderMailboxComparator;
-import org.apache.james.mailbox.store.search.comparator.InternalDateComparator;
-import org.apache.james.mailbox.store.search.comparator.MessageIdComparator;
-import org.apache.james.mailbox.store.search.comparator.ReverseComparator;
+import org.apache.james.mailbox.store.search.comparator.MessageComparators;
 import org.apache.james.mailbox.store.search.comparator.SentDateComparator;
-import org.apache.james.mailbox.store.search.comparator.SizeComparator;
-import org.apache.james.mailbox.store.search.comparator.UidComparator;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -63,7 +65,7 @@ public class CombinedComparatorTest {
     @Test
     public void createShouldConvertInternalDate() {
         assertThat(CombinedComparator.create(ImmutableList.of(new Sort(SortClause.Arrival))).getComparators())
-            .containsOnly(InternalDateComparator.INTERNALDATE);
+            .containsOnly(MessageComparators.INTERNAL_DATE_COMPARATOR);
     }
 
     @Test
@@ -87,7 +89,7 @@ public class CombinedComparatorTest {
     @Test
     public void createShouldConvertSize() {
         assertThat(CombinedComparator.create(ImmutableList.of(new Sort(SortClause.Size))).getComparators())
-            .containsOnly(SizeComparator.SIZE);
+            .containsOnly(MessageComparators.SIZE_COMPARATOR);
     }
 
     @Test
@@ -99,7 +101,7 @@ public class CombinedComparatorTest {
     @Test
     public void createShouldConvertUid() {
         assertThat(CombinedComparator.create(ImmutableList.of(new Sort(SortClause.Uid))).getComparators())
-            .containsOnly(UidComparator.UID);
+            .containsOnly(MessageComparators.UID_COMPARATOR);
     }
 
     @Test
@@ -123,12 +125,18 @@ public class CombinedComparatorTest {
     @Test
     public void createShouldConvertId() {
         assertThat(CombinedComparator.create(ImmutableList.of(new Sort(SortClause.Id))).getComparators())
-            .containsOnly(MessageIdComparator.MESSAGE_ID_COMPARATOR);
+            .containsOnly(MessageComparators.MESSAGE_ID_COMPARATOR);
     }
 
     @Test
     public void createShouldReverse() {
-        assertThat(CombinedComparator.create(ImmutableList.of(new Sort(SortClause.DisplayFrom, Order.REVERSE))).getComparators())
-            .containsOnly(new ReverseComparator(HeaderDisplayComparator.FROM_COMPARATOR));
+        MailboxMessage message1 = mock(MailboxMessage.class);
+        when(message1.getUid()).thenReturn(MessageUid.of(1));
+        MailboxMessage message2 = mock(MailboxMessage.class);
+        when(message2.getUid()).thenReturn(MessageUid.of(2));
+
+        Comparator<MailboxMessage> comparator = CombinedComparator.create(ImmutableList.of(new Sort(SortClause.Uid, Order.REVERSE)));
+
+        assertThat(comparator.compare(message1, message2)).isGreaterThan(0);
     }
 }


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