Merge pull request #992 from k9mail/transitive-uidreversecomparator
Make UidReverseComparator transitive, order null elements to right
This commit is contained in:
commit
fe822db1c6
3 changed files with 243 additions and 23 deletions
|
@ -7,7 +7,6 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
|
@ -134,28 +133,6 @@ public class MessagingController implements Runnable {
|
|||
private static final String PENDING_COMMAND_MARK_ALL_AS_READ = "com.fsck.k9.MessagingController.markAllAsRead";
|
||||
private static final String PENDING_COMMAND_EXPUNGE = "com.fsck.k9.MessagingController.expunge";
|
||||
|
||||
public static class UidReverseComparator implements Comparator<Message> {
|
||||
@Override
|
||||
public int compare(Message o1, Message o2) {
|
||||
if (o1 == null || o2 == null || o1.getUid() == null || o2.getUid() == null) {
|
||||
return 0;
|
||||
}
|
||||
int id1, id2;
|
||||
try {
|
||||
id1 = Integer.parseInt(o1.getUid());
|
||||
id2 = Integer.parseInt(o2.getUid());
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
//reversed intentionally.
|
||||
if (id1 < id2)
|
||||
return 1;
|
||||
if (id1 > id2)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum number of unsynced messages to store at once
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.fsck.k9.controller;
|
||||
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import com.fsck.k9.mail.Message;
|
||||
|
||||
|
||||
class UidReverseComparator implements Comparator<Message> {
|
||||
@Override
|
||||
public int compare(Message messageLeft, Message messageRight) {
|
||||
Long uidLeft = getUidForMessage(messageLeft);
|
||||
Long uidRight = getUidForMessage(messageRight);
|
||||
|
||||
if (uidLeft == null && uidRight == null) {
|
||||
return 0;
|
||||
} else if (uidLeft == null) {
|
||||
return 1;
|
||||
} else if (uidRight == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// reverse order
|
||||
return uidRight.compareTo(uidLeft);
|
||||
}
|
||||
|
||||
private Long getUidForMessage(Message message) {
|
||||
try {
|
||||
return Long.parseLong(message.getUid());
|
||||
} catch (NullPointerException | NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,209 @@
|
|||
package com.fsck.k9.controller;
|
||||
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.fsck.k9.mail.Message;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public class UidReverseComparatorTest {
|
||||
private UidReverseComparator comparator;
|
||||
|
||||
|
||||
@Before
|
||||
public void onBefore() {
|
||||
comparator = new UidReverseComparator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withTwoNullArguments_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = null;
|
||||
Message messageRight = null;
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are null", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withNullArgumentAndMessageWithNullUid_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = null;
|
||||
Message messageRight = createMessageWithNullUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withMessageWithNullUidAndNullArgument_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = createMessageWithNullUid();
|
||||
Message messageRight = null;
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withTwoMessagesWithNullUid_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = createMessageWithNullUid();
|
||||
Message messageRight = createMessageWithNullUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are a message with a null UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withNullArgumentAndMessageWithInvalidUid_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = null;
|
||||
Message messageRight = createMessageWithInvalidUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withMessageWithInvalidUidAndNullArgument_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = createMessageWithInvalidUid();
|
||||
Message messageRight = null;
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withTwoMessagesWithInvalidUid_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = createMessageWithInvalidUid();
|
||||
Message messageRight = createMessageWithInvalidUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are a message with an invalid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withMessageWithNullUidAndMessageWithInvalidUid_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = createMessageWithNullUid();
|
||||
Message messageRight = createMessageWithInvalidUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withMessageWithInvalidUidAndMessageWithNullUid_shouldReturnZero() throws Exception {
|
||||
Message messageLeft = createMessageWithInvalidUid();
|
||||
Message messageRight = createMessageWithNullUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withLeftNullArgument_shouldReturnPositive() throws Exception {
|
||||
Message messageLeft = null;
|
||||
Message messageRight = createMessageWithUid(1);
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertTrue("result must be > 0 when left argument is null", result > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withLeftMessageWithNullUid_shouldReturnPositive() throws Exception {
|
||||
Message messageLeft = createMessageWithNullUid();
|
||||
Message messageRight = createMessageWithUid(1);
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertTrue("result must be > 0 when left argument is message with null UID", result > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withLeftMessageWithInvalidUid_shouldReturnPositive() throws Exception {
|
||||
Message messageLeft = createMessageWithInvalidUid();
|
||||
Message messageRight = createMessageWithUid(1);
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertTrue("result must be > 0 when left argument is message with invalid UID", result > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withRightNullArgument_shouldReturnNegative() throws Exception {
|
||||
Message messageLeft = createMessageWithUid(1);
|
||||
Message messageRight = null;
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertTrue("result must be < 0 when right argument is null", result < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withRightMessageWithNullUid_shouldReturnNegative() throws Exception {
|
||||
Message messageLeft = createMessageWithUid(1);
|
||||
Message messageRight = createMessageWithNullUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertTrue("result must be < 0 when right argument is message with null UID", result < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_withRightMessageWithInvalidUid_shouldReturnNegative() throws Exception {
|
||||
Message messageLeft = createMessageWithUid(1);
|
||||
Message messageRight = createMessageWithInvalidUid();
|
||||
|
||||
int result = comparator.compare(messageLeft, messageRight);
|
||||
|
||||
assertTrue("result must be < 0 when right argument is message with invalid UID", result < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare_twoMessages_shouldReturnOrderByUid() throws Exception {
|
||||
Message messageSmall = createMessageWithUid(5);
|
||||
Message messageLarge = createMessageWithUid(15);
|
||||
|
||||
int resultOne = comparator.compare(messageSmall, messageLarge);
|
||||
int resultTwo = comparator.compare(messageLarge, messageSmall);
|
||||
|
||||
assertTrue("result must be > 0 when right message has larger UID than left message", resultOne > 0);
|
||||
assertTrue("result must be < 0 when left message has larger UID than right message", resultTwo < 0);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Message createMessageWithUid(int uid) {
|
||||
return createMessageWithUidString(Integer.toString(uid));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Message createMessageWithNullUid() {
|
||||
return createMessageWithUidString(null);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Message createMessageWithInvalidUid() {
|
||||
return createMessageWithUidString("invalid");
|
||||
}
|
||||
|
||||
private Message createMessageWithUidString(String uid) {
|
||||
Message message = mock(Message.class);
|
||||
when(message.getUid()).thenReturn(uid);
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue