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;
|
package org.pgpainless.key.util;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public final class UserId implements CharSequence {
|
public final class UserId implements CharSequence {
|
||||||
public static final class Builder {
|
public static final class Builder {
|
||||||
|
@ -64,18 +65,18 @@ public final class UserId implements CharSequence {
|
||||||
private final String email;
|
private final String email;
|
||||||
private long hash = Long.MAX_VALUE;
|
private long hash = Long.MAX_VALUE;
|
||||||
|
|
||||||
private UserId(String name, String comment, String email) {
|
private UserId(@Nullable String name, @Nullable String comment, @Nullable String email) {
|
||||||
this.name = name;
|
this.name = name == null ? null : name.trim();
|
||||||
this.comment = comment;
|
this.comment = comment == null ? null : comment.trim();
|
||||||
this.email = email;
|
this.email = email == null ? null : email.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserId onlyEmail(String email) {
|
public static UserId onlyEmail(@Nonnull String email) {
|
||||||
checkNotNull("email", email);
|
checkNotNull("email", email);
|
||||||
return new UserId(null, null, 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("name", name);
|
||||||
checkNotNull("email", email);
|
checkNotNull("email", email);
|
||||||
return new UserId(name, null, email);
|
return new UserId(name, null, email);
|
||||||
|
@ -118,27 +119,29 @@ public final class UserId implements CharSequence {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nonnull String toString() {
|
public @Nonnull String toString() {
|
||||||
return asString(false);
|
return asString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string representation of the object.
|
* 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.
|
* @return a string representation of the object.
|
||||||
*/
|
*/
|
||||||
public String asString(boolean ignoreEmptyValues) {
|
public String asString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (name != null && (!ignoreEmptyValues || !name.isEmpty())) {
|
if (name != null && !name.isEmpty()) {
|
||||||
sb.append(name);
|
sb.append(name);
|
||||||
}
|
}
|
||||||
if (comment != null && (!ignoreEmptyValues || !comment.isEmpty())) {
|
if (comment != null && !comment.isEmpty()) {
|
||||||
sb.append(" (").append(comment).append(')');
|
if (sb.length() > 0) {
|
||||||
|
sb.append(' ');
|
||||||
|
}
|
||||||
|
sb.append('(').append(comment).append(')');
|
||||||
}
|
}
|
||||||
if (email != null && (!ignoreEmptyValues || !email.isEmpty())) {
|
if (email != null && !email.isEmpty()) {
|
||||||
final boolean moreThanJustEmail = sb.length() > 0;
|
if (sb.length() > 0) {
|
||||||
if (moreThanJustEmail) sb.append(" <");
|
sb.append(' ');
|
||||||
sb.append(email);
|
}
|
||||||
if (moreThanJustEmail) sb.append('>');
|
sb.append('<').append(email).append('>');
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,15 +151,13 @@ public class UserIdTest {
|
||||||
@Test
|
@Test
|
||||||
void testEmailOnlyFormatting() {
|
void testEmailOnlyFormatting() {
|
||||||
final UserId userId = UserId.onlyEmail("john.smith@example.com");
|
final UserId userId = UserId.onlyEmail("john.smith@example.com");
|
||||||
assertEquals("john.smith@example.com", userId.toString());
|
assertEquals("<john.smith@example.com>", userId.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEmptyNameAndValidEmailFormatting() {
|
void testEmptyNameAndValidEmailFormatting() {
|
||||||
final UserId userId = UserId.nameAndEmail("", "john.smith@example.com");
|
final UserId userId = UserId.nameAndEmail("", "john.smith@example.com");
|
||||||
assertEquals("john.smith@example.com", userId.toString());
|
assertEquals("<john.smith@example.com>", userId.toString());
|
||||||
assertEquals("john.smith@example.com", userId.asString(false));
|
|
||||||
assertEquals("john.smith@example.com", userId.asString(true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -169,9 +167,7 @@ public class UserIdTest {
|
||||||
.withName("")
|
.withName("")
|
||||||
.withEmail("john.smith@example.com")
|
.withEmail("john.smith@example.com")
|
||||||
.build();
|
.build();
|
||||||
assertEquals(" () <john.smith@example.com>", userId.toString());
|
assertEquals("<john.smith@example.com>", userId.toString());
|
||||||
assertEquals(" () <john.smith@example.com>", userId.asString(false));
|
|
||||||
assertEquals("john.smith@example.com", userId.asString(true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -51,9 +51,9 @@ public class GenerateKeyWithAdditionalUserIdTest {
|
||||||
JUtils.assertDateEquals(expiration, PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate());
|
JUtils.assertDateEquals(expiration, PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate());
|
||||||
|
|
||||||
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
|
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
|
||||||
assertEquals("primary@user.id", userIds.next());
|
assertEquals("<primary@user.id>", userIds.next());
|
||||||
assertEquals("additional@user.id", userIds.next());
|
assertEquals("<additional@user.id>", userIds.next());
|
||||||
assertEquals("additional2@user.id", userIds.next());
|
assertEquals("<additional2@user.id>", userIds.next());
|
||||||
assertEquals("trimThis@user.id", userIds.next());
|
assertEquals("trimThis@user.id", userIds.next());
|
||||||
assertFalse(userIds.hasNext());
|
assertFalse(userIds.hasNext());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue