pgpainless/pgpainless-core/src/main/java/org/pgpainless/key/util/UserId.java

185 lines
5.2 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>, 2021 Flowcrypt a.s.
//
// SPDX-License-Identifier: Apache-2.0
2021-02-21 14:11:09 +01:00
2020-11-13 15:59:28 +01:00
package org.pgpainless.key.util;
import javax.annotation.Nonnull;
public final class UserId implements CharSequence {
2021-02-21 14:11:09 +01:00
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);
}
}
2020-11-13 15:59:28 +01:00
private final String name;
private final String comment;
private final String email;
2021-02-21 15:18:42 +01:00
private long hash = Long.MAX_VALUE;
2020-11-13 15:59:28 +01:00
private UserId(String name, String comment, String email) {
2020-11-13 15:59:28 +01:00
this.name = name;
this.comment = comment;
this.email = email;
}
public static UserId onlyEmail(String email) {
2021-02-21 14:11:09 +01:00
checkNotNull("email", email);
2020-11-13 15:59:28 +01:00
return new UserId(null, null, email);
}
public static UserId nameAndEmail(String name, String email) {
2021-02-21 14:11:09 +01:00
checkNotNull("name", name);
checkNotNull("email", email);
return new UserId(name, null, email);
}
2021-02-21 14:11:09 +01:00
public static Builder newBuilder() {
return new Builder();
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
public Builder toBuilder() {
return new Builder(name, comment, email);
}
2020-11-13 15:59:28 +01:00
2021-02-21 14:11:09 +01:00
public String getName() {
return name;
}
2020-11-13 15:59:28 +01:00
2021-02-21 14:11:09 +01:00
public String getComment() {
return comment;
}
2020-11-13 15:59:28 +01:00
2021-02-21 14:11:09 +01:00
public String getEmail() {
return email;
}
2020-11-13 15:59:28 +01:00
2021-02-21 14:11:09 +01:00
@Override
public int length() {
return toString().length();
}
2020-11-13 15:59:28 +01:00
2021-02-21 14:11:09 +01:00
@Override
public char charAt(int i) {
return toString().charAt(i);
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
@Override
2021-12-28 13:32:50 +01:00
public @Nonnull CharSequence subSequence(int i, int i1) {
2021-02-21 14:11:09 +01:00
return toString().subSequence(i, i1);
}
2020-11-13 15:59:28 +01:00
2021-02-21 14:11:09 +01:00
@Override
public @Nonnull String toString() {
2021-02-21 14:11:09 +01:00
return asString(false);
}
2020-11-13 15:59:28 +01:00
2021-02-21 15:18:42 +01:00
/**
* 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.
*/
2021-02-21 14:11:09 +01:00
public String asString(boolean ignoreEmptyValues) {
StringBuilder sb = new StringBuilder();
if (name != null && (!ignoreEmptyValues || !name.isEmpty())) {
sb.append(name);
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
if (comment != null && (!ignoreEmptyValues || !comment.isEmpty())) {
sb.append(" (").append(comment).append(')');
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
if (email != null && (!ignoreEmptyValues || !email.isEmpty())) {
final boolean moreThanJustEmail = sb.length() > 0;
if (moreThanJustEmail) sb.append(" <");
sb.append(email);
if (moreThanJustEmail) sb.append('>');
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
return sb.toString();
2020-11-13 15:59:28 +01:00
}
@Override
2021-02-21 14:11:09 +01:00
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;
2021-02-21 15:18:42 +01:00
return isEqualComponent(name, other.name, false)
&& isEqualComponent(comment, other.comment, false)
2021-02-21 14:11:09 +01:00
&& isEqualComponent(email, other.email, true);
2020-11-13 15:59:28 +01:00
}
@Override
2021-02-21 14:11:09 +01:00
public int hashCode() {
2021-02-21 15:18:42 +01:00
if (hash != Long.MAX_VALUE) {
return (int) hash;
2021-02-21 14:11:09 +01:00
} else {
int hashCode = 7;
hashCode = 31 * hashCode + (name == null ? 0 : name.hashCode());
hashCode = 31 * hashCode + (comment == null ? 0 : comment.hashCode());
hashCode = 31 * hashCode + (email == null ? 0 : email.toLowerCase().hashCode());
this.hash = hashCode;
return hashCode;
2021-02-21 14:11:09 +01:00
}
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
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)));
2020-11-13 15:59:28 +01:00
}
2021-02-21 14:11:09 +01:00
private static void checkNotNull(String paramName, String value) {
if (value == null) {
throw new IllegalArgumentException(paramName + " must be not null");
}
}
2020-11-13 15:59:28 +01:00
}