mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-18 02:12:06 +01:00
Clean up UserId.toString() behavior
This commit is contained in:
parent
6913aa3d6d
commit
b0c283e143
3 changed files with 26 additions and 27 deletions
|
@ -5,6 +5,7 @@
|
|||
package org.pgpainless.key.util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class UserId implements CharSequence {
|
||||
public static final class Builder {
|
||||
|
@ -64,18 +65,18 @@ public final class UserId implements CharSequence {
|
|||
private final String email;
|
||||
private long hash = Long.MAX_VALUE;
|
||||
|
||||
private UserId(String name, String comment, String email) {
|
||||
this.name = name;
|
||||
this.comment = comment;
|
||||
this.email = email;
|
||||
private UserId(@Nullable String name, @Nullable String comment, @Nullable String email) {
|
||||
this.name = name == null ? null : name.trim();
|
||||
this.comment = comment == null ? null : comment.trim();
|
||||
this.email = email == null ? null : email.trim();
|
||||
}
|
||||
|
||||
public static UserId onlyEmail(String email) {
|
||||
public static UserId onlyEmail(@Nonnull String email) {
|
||||
checkNotNull("email", email);
|
||||
return new UserId(null, null, email);
|
||||
}
|
||||
|
||||
public static UserId nameAndEmail(String name, String email) {
|
||||
public static UserId nameAndEmail(@Nonnull String name, @Nonnull String email) {
|
||||
checkNotNull("name", name);
|
||||
checkNotNull("email", email);
|
||||
return new UserId(name, null, email);
|
||||
|
@ -118,27 +119,29 @@ public final class UserId implements CharSequence {
|
|||
|
||||
@Override
|
||||
public @Nonnull String toString() {
|
||||
return asString(false);
|
||||
return asString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the object.
|
||||
* @param ignoreEmptyValues Flag which indicates that empty string values should not be outputted.
|
||||
* @return a string representation of the object.
|
||||
*/
|
||||
public String asString(boolean ignoreEmptyValues) {
|
||||
public String asString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (name != null && (!ignoreEmptyValues || !name.isEmpty())) {
|
||||
if (name != null && !name.isEmpty()) {
|
||||
sb.append(name);
|
||||
}
|
||||
if (comment != null && (!ignoreEmptyValues || !comment.isEmpty())) {
|
||||
sb.append(" (").append(comment).append(')');
|
||||
if (comment != null && !comment.isEmpty()) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(' ');
|
||||
}
|
||||
if (email != null && (!ignoreEmptyValues || !email.isEmpty())) {
|
||||
final boolean moreThanJustEmail = sb.length() > 0;
|
||||
if (moreThanJustEmail) sb.append(" <");
|
||||
sb.append(email);
|
||||
if (moreThanJustEmail) sb.append('>');
|
||||
sb.append('(').append(comment).append(')');
|
||||
}
|
||||
if (email != null && !email.isEmpty()) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append('<').append(email).append('>');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
|
|
@ -151,15 +151,13 @@ public class UserIdTest {
|
|||
@Test
|
||||
void testEmailOnlyFormatting() {
|
||||
final UserId userId = UserId.onlyEmail("john.smith@example.com");
|
||||
assertEquals("john.smith@example.com", userId.toString());
|
||||
assertEquals("<john.smith@example.com>", userId.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmptyNameAndValidEmailFormatting() {
|
||||
final UserId userId = UserId.nameAndEmail("", "john.smith@example.com");
|
||||
assertEquals("john.smith@example.com", userId.toString());
|
||||
assertEquals("john.smith@example.com", userId.asString(false));
|
||||
assertEquals("john.smith@example.com", userId.asString(true));
|
||||
assertEquals("<john.smith@example.com>", userId.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -169,9 +167,7 @@ public class UserIdTest {
|
|||
.withName("")
|
||||
.withEmail("john.smith@example.com")
|
||||
.build();
|
||||
assertEquals(" () <john.smith@example.com>", userId.toString());
|
||||
assertEquals(" () <john.smith@example.com>", userId.asString(false));
|
||||
assertEquals("john.smith@example.com", userId.asString(true));
|
||||
assertEquals("<john.smith@example.com>", userId.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -51,9 +51,9 @@ public class GenerateKeyWithAdditionalUserIdTest {
|
|||
JUtils.assertDateEquals(expiration, PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate());
|
||||
|
||||
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
|
||||
assertEquals("primary@user.id", userIds.next());
|
||||
assertEquals("additional@user.id", userIds.next());
|
||||
assertEquals("additional2@user.id", userIds.next());
|
||||
assertEquals("<primary@user.id>", userIds.next());
|
||||
assertEquals("<additional@user.id>", userIds.next());
|
||||
assertEquals("<additional2@user.id>", userIds.next());
|
||||
assertEquals("trimThis@user.id", userIds.next());
|
||||
assertFalse(userIds.hasNext());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue