1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-29 15:52:08 +01:00

Add UserId.compare(uid1, uid2, comparator) along with some default comparators

This commit is contained in:
Paul Schaub 2022-12-01 16:02:45 +01:00
parent b07e0c2be5
commit bfcfaa04c4
2 changed files with 147 additions and 3 deletions

View file

@ -4,6 +4,7 @@
package org.pgpainless.key.util; package org.pgpainless.key.util;
import java.util.Comparator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -174,4 +175,88 @@ public final class UserId implements CharSequence {
|| (!valueIsNull && !otherValueIsNull || (!valueIsNull && !otherValueIsNull
&& (ignoreCase ? value.equalsIgnoreCase(otherValue) : value.equals(otherValue))); && (ignoreCase ? value.equalsIgnoreCase(otherValue) : value.equals(otherValue)));
} }
public static int compare(@Nullable UserId o1, @Nullable UserId o2, @Nonnull Comparator<UserId> comparator) {
return comparator.compare(o1, o2);
}
public static class DefaultComparator implements Comparator<UserId> {
@Override
public int compare(UserId o1, UserId o2) {
if (o1 == o2) {
return 0;
}
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 1;
}
NullSafeStringComparator c = new NullSafeStringComparator();
int cName = c.compare(o1.getName(), o2.getName());
if (cName != 0) {
return cName;
}
int cComment = c.compare(o1.getComment(), o2.getComment());
if (cComment != 0) {
return cComment;
}
return c.compare(o1.getEmail(), o2.getEmail());
}
}
public static class DefaultIgnoreCaseComparator implements Comparator<UserId> {
@Override
public int compare(UserId o1, UserId o2) {
if (o1 == o2) {
return 0;
}
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 1;
}
NullSafeStringComparator c = new NullSafeStringComparator();
int cName = c.compare(lower(o1.getName()), lower(o2.getName()));
if (cName != 0) {
return cName;
}
int cComment = c.compare(lower(o1.getComment()), lower(o2.getComment()));
if (cComment != 0) {
return cComment;
}
return c.compare(lower(o1.getEmail()), lower(o2.getEmail()));
}
private static String lower(String string) {
return string == null ? null : string.toLowerCase();
}
}
private static class NullSafeStringComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
// noinspection StringEquality
if (o1 == o2) {
return 0;
}
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 1;
}
return o1.compareTo(o2);
}
}
} }

View file

@ -4,13 +4,15 @@
package org.pgpainless.key; package org.pgpainless.key;
import org.junit.jupiter.api.Test;
import org.pgpainless.key.util.UserId;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.Comparator;
import org.junit.jupiter.api.Test;
import org.pgpainless.key.util.UserId;
public class UserIdTest { public class UserIdTest {
@Test @Test
@ -212,4 +214,61 @@ public class UserIdTest {
assertEquals('<', id.charAt(0)); assertEquals('<', id.charAt(0));
assertEquals('>', id.charAt(id.length() - 1)); assertEquals('>', id.charAt(id.length() - 1));
} }
@Test
public void defaultCompareTest() {
UserId id1 = UserId.onlyEmail("alice@pgpainless.org");
UserId id2 = UserId.onlyEmail("alice@gnupg.org");
UserId id3 = UserId.nameAndEmail("Alice", "alice@pgpainless.org");
UserId id3_ = UserId.nameAndEmail("Alice", "alice@pgpainless.org");
UserId id4 = UserId.newBuilder().withName("Alice").build();
UserId id5 = UserId.newBuilder().withName("Alice").withComment("Work Mail").withEmail("alice@pgpainless.org").build();
assertEquals(id3.hashCode(), id3_.hashCode());
assertNotEquals(id2.hashCode(), id3.hashCode());
Comparator<UserId> c = new UserId.DefaultComparator();
assertEquals(0, UserId.compare(null, null, c));
assertEquals(0, UserId.compare(id1, id1, c));
assertNotEquals(0, UserId.compare(id1, null, c));
assertNotEquals(0, UserId.compare(null, id1, c));
assertNotEquals(0, UserId.compare(id1, id2, c));
assertNotEquals(0, UserId.compare(id2, id1, c));
assertNotEquals(0, UserId.compare(id1, id3, c));
assertNotEquals(0, UserId.compare(id1, id4, c));
assertNotEquals(0, UserId.compare(id4, id1, c));
assertNotEquals(0, UserId.compare(id2, id3, c));
assertNotEquals(0, UserId.compare(id1, id5, c));
assertNotEquals(0, UserId.compare(id5, id1, c));
assertNotEquals(0, UserId.compare(id3, id5, c));
assertNotEquals(0, UserId.compare(id5, id3, c));
assertEquals(0, UserId.compare(id3, id3, c));
assertEquals(0, UserId.compare(id3, id3_, c));
}
@Test
public void defaultIgnoreCaseCompareTest() {
UserId id1 = UserId.nameAndEmail("Alice", "alice@pgpainless.org");
UserId id2 = UserId.nameAndEmail("alice", "alice@pgpainless.org");
UserId id3 = UserId.nameAndEmail("Alice", "Alice@Pgpainless.Org");
UserId id4 = UserId.newBuilder().withName("Alice").withComment("Work Email").withEmail("Alice@Pgpainless.Org").build();
UserId id5 = UserId.newBuilder().withName("alice").withComment("work email").withEmail("alice@pgpainless.org").build();
UserId id6 = UserId.nameAndEmail("Bob", "bob@pgpainless.org");
Comparator<UserId> c = new UserId.DefaultIgnoreCaseComparator();
assertEquals(0, UserId.compare(id1, id2, c));
assertEquals(0, UserId.compare(id1, id3, c));
assertEquals(0, UserId.compare(id2, id3, c));
assertEquals(0, UserId.compare(null, null, c));
assertEquals(0, UserId.compare(id1, id1, c));
assertEquals(0, UserId.compare(id4, id4, c));
assertEquals(0, UserId.compare(id4, id5, c));
assertEquals(0, UserId.compare(id5, id4, c));
assertNotEquals(0, UserId.compare(null, id1, c));
assertNotEquals(0, UserId.compare(id1, null, c));
assertNotEquals(0, UserId.compare(id1, id4, c));
assertNotEquals(0, UserId.compare(id4, id1, c));
assertNotEquals(0, UserId.compare(id1, id6, c));
assertNotEquals(0, UserId.compare(id6, id1, c));
}
} }