org.interledger.crypto.ImmutableKeyMetadata 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 KeyMetadata.AbstractKeyMetadata}.
*
* Use the builder to create immutable instances:
* {@code ImmutableKeyMetadata.builder()}.
*/
@Generated(from = "KeyMetadata.AbstractKeyMetadata", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableKeyMetadata extends KeyMetadata.AbstractKeyMetadata {
private final String platformIdentifier;
private final String keyringIdentifier;
private final String keyIdentifier;
private final String keyVersion;
private ImmutableKeyMetadata(
String platformIdentifier,
String keyringIdentifier,
String keyIdentifier,
String keyVersion) {
this.platformIdentifier = platformIdentifier;
this.keyringIdentifier = keyringIdentifier;
this.keyIdentifier = keyIdentifier;
this.keyVersion = keyVersion;
}
/**
* The unique identifier of the platform that can decode this secret.
* @return
*/
@Override
public String platformIdentifier() {
return platformIdentifier;
}
/**
* The unique identifier of the keyring that holds the the private-key used to encrypt this encoded secret.
* @return
*/
@Override
public String keyringIdentifier() {
return keyringIdentifier;
}
/**
* The unique identifier for the private-key used to encrypt this encoded secret.
* @return
*/
@Override
public String keyIdentifier() {
return keyIdentifier;
}
/**
* The version of the encryption key used to encrypt this secret.
* @return
*/
@Override
public String keyVersion() {
return keyVersion;
}
/**
* Copy the current immutable object by setting a value for the {@link KeyMetadata.AbstractKeyMetadata#platformIdentifier() platformIdentifier} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for platformIdentifier
* @return A modified copy of the {@code this} object
*/
public final ImmutableKeyMetadata withPlatformIdentifier(String value) {
String newValue = Objects.requireNonNull(value, "platformIdentifier");
if (this.platformIdentifier.equals(newValue)) return this;
return validate(new ImmutableKeyMetadata(newValue, this.keyringIdentifier, this.keyIdentifier, this.keyVersion));
}
/**
* Copy the current immutable object by setting a value for the {@link KeyMetadata.AbstractKeyMetadata#keyringIdentifier() keyringIdentifier} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for keyringIdentifier
* @return A modified copy of the {@code this} object
*/
public final ImmutableKeyMetadata withKeyringIdentifier(String value) {
String newValue = Objects.requireNonNull(value, "keyringIdentifier");
if (this.keyringIdentifier.equals(newValue)) return this;
return validate(new ImmutableKeyMetadata(this.platformIdentifier, newValue, this.keyIdentifier, this.keyVersion));
}
/**
* Copy the current immutable object by setting a value for the {@link KeyMetadata.AbstractKeyMetadata#keyIdentifier() keyIdentifier} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for keyIdentifier
* @return A modified copy of the {@code this} object
*/
public final ImmutableKeyMetadata withKeyIdentifier(String value) {
String newValue = Objects.requireNonNull(value, "keyIdentifier");
if (this.keyIdentifier.equals(newValue)) return this;
return validate(new ImmutableKeyMetadata(this.platformIdentifier, this.keyringIdentifier, newValue, this.keyVersion));
}
/**
* Copy the current immutable object by setting a value for the {@link KeyMetadata.AbstractKeyMetadata#keyVersion() keyVersion} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for keyVersion
* @return A modified copy of the {@code this} object
*/
public final ImmutableKeyMetadata withKeyVersion(String value) {
String newValue = Objects.requireNonNull(value, "keyVersion");
if (this.keyVersion.equals(newValue)) return this;
return validate(new ImmutableKeyMetadata(this.platformIdentifier, this.keyringIdentifier, this.keyIdentifier, newValue));
}
/**
* This instance is equal to all instances of {@code ImmutableKeyMetadata} 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 ImmutableKeyMetadata
&& equalTo((ImmutableKeyMetadata) another);
}
private boolean equalTo(ImmutableKeyMetadata another) {
return platformIdentifier.equals(another.platformIdentifier)
&& keyringIdentifier.equals(another.keyringIdentifier)
&& keyIdentifier.equals(another.keyIdentifier)
&& keyVersion.equals(another.keyVersion);
}
/**
* Computes a hash code from attributes: {@code platformIdentifier}, {@code keyringIdentifier}, {@code keyIdentifier}, {@code keyVersion}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + platformIdentifier.hashCode();
h += (h << 5) + keyringIdentifier.hashCode();
h += (h << 5) + keyIdentifier.hashCode();
h += (h << 5) + keyVersion.hashCode();
return h;
}
/**
* Prints the immutable value {@code KeyMetadata} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("KeyMetadata")
.omitNullValues()
.add("platformIdentifier", platformIdentifier)
.add("keyringIdentifier", keyringIdentifier)
.add("keyIdentifier", keyIdentifier)
.add("keyVersion", keyVersion)
.toString();
}
private static ImmutableKeyMetadata validate(ImmutableKeyMetadata instance) {
instance.doChecks();
return instance;
}
/**
* Creates an immutable copy of a {@link KeyMetadata.AbstractKeyMetadata} 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 KeyMetadata instance
*/
public static ImmutableKeyMetadata copyOf(KeyMetadata.AbstractKeyMetadata instance) {
if (instance instanceof ImmutableKeyMetadata) {
return (ImmutableKeyMetadata) instance;
}
return ImmutableKeyMetadata.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableKeyMetadata ImmutableKeyMetadata}.
*
* ImmutableKeyMetadata.builder()
* .platformIdentifier(String) // required {@link KeyMetadata.AbstractKeyMetadata#platformIdentifier() platformIdentifier}
* .keyringIdentifier(String) // required {@link KeyMetadata.AbstractKeyMetadata#keyringIdentifier() keyringIdentifier}
* .keyIdentifier(String) // required {@link KeyMetadata.AbstractKeyMetadata#keyIdentifier() keyIdentifier}
* .keyVersion(String) // required {@link KeyMetadata.AbstractKeyMetadata#keyVersion() keyVersion}
* .build();
*
* @return A new ImmutableKeyMetadata builder
*/
public static ImmutableKeyMetadata.Builder builder() {
return new ImmutableKeyMetadata.Builder();
}
/**
* Builds instances of type {@link ImmutableKeyMetadata ImmutableKeyMetadata}.
* 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 = "KeyMetadata.AbstractKeyMetadata", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_PLATFORM_IDENTIFIER = 0x1L;
private static final long INIT_BIT_KEYRING_IDENTIFIER = 0x2L;
private static final long INIT_BIT_KEY_IDENTIFIER = 0x4L;
private static final long INIT_BIT_KEY_VERSION = 0x8L;
private long initBits = 0xfL;
private @Nullable String platformIdentifier;
private @Nullable String keyringIdentifier;
private @Nullable String keyIdentifier;
private @Nullable String keyVersion;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.crypto.KeyMetadata} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(KeyMetadata instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.crypto.KeyMetadata.AbstractKeyMetadata} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(KeyMetadata.AbstractKeyMetadata instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof KeyMetadata) {
KeyMetadata instance = (KeyMetadata) object;
keyIdentifier(instance.keyIdentifier());
keyVersion(instance.keyVersion());
platformIdentifier(instance.platformIdentifier());
keyringIdentifier(instance.keyringIdentifier());
}
}
/**
* Initializes the value for the {@link KeyMetadata.AbstractKeyMetadata#platformIdentifier() platformIdentifier} attribute.
* @param platformIdentifier The value for platformIdentifier
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder platformIdentifier(String platformIdentifier) {
this.platformIdentifier = Objects.requireNonNull(platformIdentifier, "platformIdentifier");
initBits &= ~INIT_BIT_PLATFORM_IDENTIFIER;
return this;
}
/**
* Initializes the value for the {@link KeyMetadata.AbstractKeyMetadata#keyringIdentifier() keyringIdentifier} attribute.
* @param keyringIdentifier The value for keyringIdentifier
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder keyringIdentifier(String keyringIdentifier) {
this.keyringIdentifier = Objects.requireNonNull(keyringIdentifier, "keyringIdentifier");
initBits &= ~INIT_BIT_KEYRING_IDENTIFIER;
return this;
}
/**
* Initializes the value for the {@link KeyMetadata.AbstractKeyMetadata#keyIdentifier() keyIdentifier} attribute.
* @param keyIdentifier The value for keyIdentifier
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder keyIdentifier(String keyIdentifier) {
this.keyIdentifier = Objects.requireNonNull(keyIdentifier, "keyIdentifier");
initBits &= ~INIT_BIT_KEY_IDENTIFIER;
return this;
}
/**
* Initializes the value for the {@link KeyMetadata.AbstractKeyMetadata#keyVersion() keyVersion} attribute.
* @param keyVersion The value for keyVersion
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder keyVersion(String keyVersion) {
this.keyVersion = Objects.requireNonNull(keyVersion, "keyVersion");
initBits &= ~INIT_BIT_KEY_VERSION;
return this;
}
/**
* Builds a new {@link ImmutableKeyMetadata ImmutableKeyMetadata}.
* @return An immutable instance of KeyMetadata
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableKeyMetadata build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableKeyMetadata.validate(new ImmutableKeyMetadata(platformIdentifier, keyringIdentifier, keyIdentifier, keyVersion));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_PLATFORM_IDENTIFIER) != 0) attributes.add("platformIdentifier");
if ((initBits & INIT_BIT_KEYRING_IDENTIFIER) != 0) attributes.add("keyringIdentifier");
if ((initBits & INIT_BIT_KEY_IDENTIFIER) != 0) attributes.add("keyIdentifier");
if ((initBits & INIT_BIT_KEY_VERSION) != 0) attributes.add("keyVersion");
return "Cannot build KeyMetadata, some of required attributes are not set " + attributes;
}
}
}