org.interledger.crypto.ImmutableCryptoKeys 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 CryptoKeys}.
*
* Use the builder to create immutable instances:
* {@code ImmutableCryptoKeys.builder()}.
*/
@Generated(from = "CryptoKeys", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableCryptoKeys implements CryptoKeys {
private final CryptoKey secret0;
private final CryptoKey accountSettings;
private ImmutableCryptoKeys(CryptoKey secret0, CryptoKey accountSettings) {
this.secret0 = secret0;
this.accountSettings = accountSettings;
}
/**
* Core secret for the connector
* @return key
*/
@Override
public CryptoKey secret0() {
return secret0;
}
/**
* Key for incoming/outgoing shared secrets on account settings
* @return key
*/
@Override
public CryptoKey accountSettings() {
return accountSettings;
}
/**
* Copy the current immutable object by setting a value for the {@link CryptoKeys#secret0() secret0} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for secret0
* @return A modified copy of the {@code this} object
*/
public final ImmutableCryptoKeys withSecret0(CryptoKey value) {
if (this.secret0 == value) return this;
CryptoKey newValue = Objects.requireNonNull(value, "secret0");
return new ImmutableCryptoKeys(newValue, this.accountSettings);
}
/**
* Copy the current immutable object by setting a value for the {@link CryptoKeys#accountSettings() accountSettings} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for accountSettings
* @return A modified copy of the {@code this} object
*/
public final ImmutableCryptoKeys withAccountSettings(CryptoKey value) {
if (this.accountSettings == value) return this;
CryptoKey newValue = Objects.requireNonNull(value, "accountSettings");
return new ImmutableCryptoKeys(this.secret0, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableCryptoKeys} 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 ImmutableCryptoKeys
&& equalTo((ImmutableCryptoKeys) another);
}
private boolean equalTo(ImmutableCryptoKeys another) {
return secret0.equals(another.secret0)
&& accountSettings.equals(another.accountSettings);
}
/**
* Computes a hash code from attributes: {@code secret0}, {@code accountSettings}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + secret0.hashCode();
h += (h << 5) + accountSettings.hashCode();
return h;
}
/**
* Prints the immutable value {@code CryptoKeys} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("CryptoKeys")
.omitNullValues()
.add("secret0", secret0)
.add("accountSettings", accountSettings)
.toString();
}
/**
* Creates an immutable copy of a {@link CryptoKeys} 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 CryptoKeys instance
*/
public static ImmutableCryptoKeys copyOf(CryptoKeys instance) {
if (instance instanceof ImmutableCryptoKeys) {
return (ImmutableCryptoKeys) instance;
}
return ImmutableCryptoKeys.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableCryptoKeys ImmutableCryptoKeys}.
*
* ImmutableCryptoKeys.builder()
* .secret0(org.interledger.crypto.CryptoKey) // required {@link CryptoKeys#secret0() secret0}
* .accountSettings(org.interledger.crypto.CryptoKey) // required {@link CryptoKeys#accountSettings() accountSettings}
* .build();
*
* @return A new ImmutableCryptoKeys builder
*/
public static ImmutableCryptoKeys.Builder builder() {
return new ImmutableCryptoKeys.Builder();
}
/**
* Builds instances of type {@link ImmutableCryptoKeys ImmutableCryptoKeys}.
* 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 = "CryptoKeys", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_SECRET0 = 0x1L;
private static final long INIT_BIT_ACCOUNT_SETTINGS = 0x2L;
private long initBits = 0x3L;
private @Nullable CryptoKey secret0;
private @Nullable CryptoKey accountSettings;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CryptoKeys} 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(CryptoKeys instance) {
Objects.requireNonNull(instance, "instance");
secret0(instance.secret0());
accountSettings(instance.accountSettings());
return this;
}
/**
* Initializes the value for the {@link CryptoKeys#secret0() secret0} attribute.
* @param secret0 The value for secret0
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder secret0(CryptoKey secret0) {
this.secret0 = Objects.requireNonNull(secret0, "secret0");
initBits &= ~INIT_BIT_SECRET0;
return this;
}
/**
* Initializes the value for the {@link CryptoKeys#accountSettings() accountSettings} attribute.
* @param accountSettings The value for accountSettings
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder accountSettings(CryptoKey accountSettings) {
this.accountSettings = Objects.requireNonNull(accountSettings, "accountSettings");
initBits &= ~INIT_BIT_ACCOUNT_SETTINGS;
return this;
}
/**
* Builds a new {@link ImmutableCryptoKeys ImmutableCryptoKeys}.
* @return An immutable instance of CryptoKeys
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableCryptoKeys build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableCryptoKeys(secret0, accountSettings);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_SECRET0) != 0) attributes.add("secret0");
if ((initBits & INIT_BIT_ACCOUNT_SETTINGS) != 0) attributes.add("accountSettings");
return "Cannot build CryptoKeys, some of required attributes are not set " + attributes;
}
}
}