mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 22:32:07 +01:00
Add SelectUserId.byEmail()
This commit is contained in:
parent
245376d7d0
commit
a0e9c1f555
2 changed files with 32 additions and 8 deletions
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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("<alice@pgpainless.org>"));
|
||||
assertTrue(containsEmailAddress.accept("Alice <alice@pgpainless.org>"));
|
||||
|
||||
SelectUserId byEmail = SelectUserId.byEmail("alice@pgpainless.org");
|
||||
assertTrue(byEmail.accept("alice@pgpainless.org"));
|
||||
assertTrue(byEmail.accept("<alice@pgpainless.org>"));
|
||||
assertTrue(byEmail.accept("Alice <alice@pgpainless.org>"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue