1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-08 21:31: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;
import java.util.Comparator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -174,4 +175,88 @@ public final class UserId implements CharSequence {
|| (!valueIsNull && !otherValueIsNull
&& (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);
}
}
}