mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-10 06:11:08 +01:00
issue #91 Improve class UserId
This commit is contained in:
parent
3e75d325a8
commit
e5aaebe174
4 changed files with 280 additions and 84 deletions
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2020 Paul Schaub.
|
||||
* Copyright 2020 Paul Schaub. Copyright 2021 Flowcrypt a.s.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -13,13 +13,67 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.pgpainless.key.util;
|
||||
|
||||
// @SuppressWarnings("unused")
|
||||
public final class UserId implements CharSequence {
|
||||
public static final class Builder {
|
||||
private String name;
|
||||
private String comment;
|
||||
private String email;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
private Builder(String name, String comment, String email) {
|
||||
this.name = name;
|
||||
this.comment = comment;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Builder withName(String name) {
|
||||
checkNotNull("name", name);
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withComment(String comment) {
|
||||
checkNotNull("comment", comment);
|
||||
this.comment = comment;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withEmail(String email) {
|
||||
checkNotNull("email", email);
|
||||
this.email = email;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder noName() {
|
||||
name = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder noComment() {
|
||||
comment = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder noEmail() {
|
||||
email = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserId build() {
|
||||
return new UserId(name, comment, email);
|
||||
}
|
||||
}
|
||||
|
||||
private final String name;
|
||||
private final String comment;
|
||||
private final String email;
|
||||
private Integer hash;
|
||||
|
||||
private UserId(String name, String comment, String email) {
|
||||
this.name = name;
|
||||
|
|
@ -27,85 +81,36 @@ public final class UserId implements CharSequence {
|
|||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (name != null) {
|
||||
sb.append(name);
|
||||
}
|
||||
if (comment != null) {
|
||||
sb.append(" (").append(comment).append(')');
|
||||
}
|
||||
if (email != null) {
|
||||
sb.append(sb.length() != 0 ? ' ' : "").append(email);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static UserId onlyEmail(String email) {
|
||||
if (email == null) {
|
||||
throw new IllegalArgumentException("Email must not be null.");
|
||||
}
|
||||
checkNotNull("email", email);
|
||||
return new UserId(null, null, email);
|
||||
}
|
||||
|
||||
public static UserId nameAndEmail(String name, String email) {
|
||||
return withName(name).noComment().withEmail(email);
|
||||
checkNotNull("name", name);
|
||||
checkNotNull("email", email);
|
||||
return new UserId(name, null, email);
|
||||
}
|
||||
|
||||
public static WithComment withName(String name) {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("Name must not be null.");
|
||||
}
|
||||
return new WithComment(name);
|
||||
public static Builder newBuilder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class WithComment {
|
||||
|
||||
private final String name;
|
||||
|
||||
public WithComment(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public WithEmail withComment(String comment) {
|
||||
if (comment == null) {
|
||||
throw new IllegalArgumentException("Comment must not be null.");
|
||||
}
|
||||
return new WithEmail(name, comment);
|
||||
}
|
||||
|
||||
public WithEmail noComment() {
|
||||
return new WithEmail(name, null);
|
||||
}
|
||||
|
||||
public UserId build() {
|
||||
return new UserId(name, null, null);
|
||||
}
|
||||
public Builder toBuilder() {
|
||||
return new Builder(name, comment, email);
|
||||
}
|
||||
|
||||
public static class WithEmail {
|
||||
|
||||
private final String name;
|
||||
private final String comment;
|
||||
|
||||
public WithEmail(String name, String comment) {
|
||||
this.name = name;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public UserId withEmail(String email) {
|
||||
if (email == null) {
|
||||
throw new IllegalArgumentException("Email must not be null.");
|
||||
}
|
||||
return new UserId(name, comment, email.matches("^<.+>$") ? email : '<' + email + '>');
|
||||
}
|
||||
|
||||
public UserId noEmail() {
|
||||
return new UserId(name, comment, null);
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
|
|
@ -122,4 +127,63 @@ public final class UserId implements CharSequence {
|
|||
return toString().subSequence(i, i1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return asString(false);
|
||||
}
|
||||
|
||||
public String asString(boolean ignoreEmptyValues) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (name != null && (!ignoreEmptyValues || !name.isEmpty())) {
|
||||
sb.append(name);
|
||||
}
|
||||
if (comment != null && (!ignoreEmptyValues || !comment.isEmpty())) {
|
||||
sb.append(" (").append(comment).append(')');
|
||||
}
|
||||
if (email != null && (!ignoreEmptyValues || !email.isEmpty())) {
|
||||
final boolean moreThanJustEmail = sb.length() > 0;
|
||||
if (moreThanJustEmail) sb.append(" <");
|
||||
sb.append(email);
|
||||
if (moreThanJustEmail) sb.append('>');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) return false;
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof UserId)) return false;
|
||||
final UserId other = (UserId) o;
|
||||
return isEqualComponent(name, other.name, false) && isEqualComponent(comment, other.comment, false)
|
||||
&& isEqualComponent(email, other.email, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
if (hash != null) {
|
||||
return hash;
|
||||
} else {
|
||||
int hash = 7;
|
||||
hash = 31 * hash + (name == null ? 0 : name.hashCode());
|
||||
hash = 31 * hash + (comment == null ? 0 : comment.hashCode());
|
||||
hash = 31 * hash + (email == null ? 0 : email.toLowerCase().hashCode());
|
||||
this.hash = hash;
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isEqualComponent(String value, String otherValue, boolean ignoreCase) {
|
||||
final boolean valueIsNull = (value == null);
|
||||
final boolean otherValueIsNull = (otherValue == null);
|
||||
return (valueIsNull && otherValueIsNull)
|
||||
|| (!valueIsNull && !otherValueIsNull
|
||||
&& (ignoreCase ? value.equalsIgnoreCase(otherValue) : value.equals(otherValue)));
|
||||
}
|
||||
|
||||
private static void checkNotNull(String paramName, String value) {
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException(paramName + " must be not null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue