org.interledger.crypto.ImmutableCryptoKey Maven / Gradle / Ivy
Show all versions of connector-crypto Show documentation
package org.interledger.crypto;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link CryptoKey}.
*
* Use the builder to create immutable instances:
* {@code ImmutableCryptoKey.builder()}.
*/
@Generated(from = "CryptoKey", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableCryptoKey implements CryptoKey {
private final String alias;
private final String version;
private ImmutableCryptoKey(String alias, String version) {
this.alias = alias;
this.version = version;
}
/**
* Key alias
* @return alias
*/
@Override
public String alias() {
return alias;
}
/**
* key version
* @return version
*/
@Override
public String version() {
return version;
}
/**
* Copy the current immutable object by setting a value for the {@link CryptoKey#alias() alias} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for alias
* @return A modified copy of the {@code this} object
*/
public final ImmutableCryptoKey withAlias(String value) {
String newValue = Objects.requireNonNull(value, "alias");
if (this.alias.equals(newValue)) return this;
return new ImmutableCryptoKey(newValue, this.version);
}
/**
* Copy the current immutable object by setting a value for the {@link CryptoKey#version() version} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for version
* @return A modified copy of the {@code this} object
*/
public final ImmutableCryptoKey withVersion(String value) {
String newValue = Objects.requireNonNull(value, "version");
if (this.version.equals(newValue)) return this;
return new ImmutableCryptoKey(this.alias, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableCryptoKey} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableCryptoKey
&& equalTo((ImmutableCryptoKey) another);
}
private boolean equalTo(ImmutableCryptoKey another) {
return alias.equals(another.alias)
&& version.equals(another.version);
}
/**
* Computes a hash code from attributes: {@code alias}, {@code version}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + alias.hashCode();
h += (h << 5) + version.hashCode();
return h;
}
/**
* Prints the immutable value {@code CryptoKey} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("CryptoKey")
.omitNullValues()
.add("alias", alias)
.add("version", version)
.toString();
}
/**
* Creates an immutable copy of a {@link CryptoKey} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance The instance to copy
* @return A copied immutable CryptoKey instance
*/
public static ImmutableCryptoKey copyOf(CryptoKey instance) {
if (instance instanceof ImmutableCryptoKey) {
return (ImmutableCryptoKey) instance;
}
return ImmutableCryptoKey.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableCryptoKey ImmutableCryptoKey}.
*
* ImmutableCryptoKey.builder()
* .alias(String) // required {@link CryptoKey#alias() alias}
* .version(String) // required {@link CryptoKey#version() version}
* .build();
*
* @return A new ImmutableCryptoKey builder
*/
public static ImmutableCryptoKey.Builder builder() {
return new ImmutableCryptoKey.Builder();
}
/**
* Builds instances of type {@link ImmutableCryptoKey ImmutableCryptoKey}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "CryptoKey", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_ALIAS = 0x1L;
private static final long INIT_BIT_VERSION = 0x2L;
private long initBits = 0x3L;
private @Nullable String alias;
private @Nullable String version;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CryptoKey} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(CryptoKey instance) {
Objects.requireNonNull(instance, "instance");
alias(instance.alias());
version(instance.version());
return this;
}
/**
* Initializes the value for the {@link CryptoKey#alias() alias} attribute.
* @param alias The value for alias
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder alias(String alias) {
this.alias = Objects.requireNonNull(alias, "alias");
initBits &= ~INIT_BIT_ALIAS;
return this;
}
/**
* Initializes the value for the {@link CryptoKey#version() version} attribute.
* @param version The value for version
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder version(String version) {
this.version = Objects.requireNonNull(version, "version");
initBits &= ~INIT_BIT_VERSION;
return this;
}
/**
* Builds a new {@link ImmutableCryptoKey ImmutableCryptoKey}.
* @return An immutable instance of CryptoKey
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableCryptoKey build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableCryptoKey(alias, version);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ALIAS) != 0) attributes.add("alias");
if ((initBits & INIT_BIT_VERSION) != 0) attributes.add("version");
return "Cannot build CryptoKey, some of required attributes are not set " + attributes;
}
}
}