diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java index 3f5cc98b..25ee5a33 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java @@ -10,6 +10,8 @@ import java.util.List; import org.bouncycastle.openpgp.PGPKeyRing; import org.pgpainless.PGPainless; +import javax.annotation.Nonnull; + public abstract class SelectUserId { protected abstract boolean accept(String userId); @@ -42,35 +44,37 @@ public abstract class SelectUserId { return null; } - public static SelectUserId containsSubstring(String query) { + public static SelectUserId containsSubstring(@Nonnull CharSequence query) { return new SelectUserId() { @Override protected boolean accept(String userId) { - return userId.contains(query); + return userId.contains(query.toString()); } }; } - public static SelectUserId exactMatch(String query) { + public static SelectUserId exactMatch(@Nonnull CharSequence query) { return new SelectUserId() { @Override protected boolean accept(String userId) { - return userId.equals(query); + return userId.equals(query.toString()); } }; } - public static SelectUserId startsWith(String substring) { + public static SelectUserId startsWith(@Nonnull CharSequence substring) { + String string = substring.toString(); return new SelectUserId() { @Override protected boolean accept(String userId) { - return userId.startsWith(substring); + return userId.startsWith(string); } }; } - public static SelectUserId containsEmailAddress(String email) { - return containsSubstring(email.matches("^<.+>$") ? email : '<' + email + '>'); + public static SelectUserId containsEmailAddress(@Nonnull CharSequence email) { + String string = email.toString(); + return containsSubstring(string.matches("^<.+>$") ? string : '<' + string + '>'); } public static SelectUserId validUserId(PGPKeyRing keyRing) { @@ -116,4 +120,11 @@ public abstract class SelectUserId { } }; } + + public static SelectUserId byEmail(CharSequence email) { + return SelectUserId.or( + SelectUserId.exactMatch(email), + SelectUserId.containsEmailAddress(email) + ); + } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java index 6e7ea39c..3a49247c 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java @@ -19,6 +19,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class SelectUserIdTest { @@ -111,4 +112,16 @@ public class SelectUserIdTest { PGPainless.inspectKeyRing(secretKeys).getUserIds() )); } + + @Test + public void testByEmail() { + SelectUserId containsEmailAddress = SelectUserId.containsEmailAddress("alice@pgpainless.org"); + assertTrue(containsEmailAddress.accept("")); + assertTrue(containsEmailAddress.accept("Alice ")); + + SelectUserId byEmail = SelectUserId.byEmail("alice@pgpainless.org"); + assertTrue(byEmail.accept("alice@pgpainless.org")); + assertTrue(byEmail.accept("")); + assertTrue(byEmail.accept("Alice ")); + } }