
org.interledger.spsp.server.model.ImmutableSpspServerSettings Maven / Gradle / Ivy
Show all versions of spsp-server Show documentation
package org.interledger.spsp.server.model;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import com.google.common.primitives.Booleans;
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;
import org.interledger.core.InterledgerAddress;
import org.interledger.crypto.CryptoKeys;
/**
* Immutable implementation of {@link SpspServerSettings}.
*
* Use the builder to create immutable instances:
* {@code ImmutableSpspServerSettings.builder()}.
*/
@Generated(from = "SpspServerSettings", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableSpspServerSettings implements SpspServerSettings {
private final InterledgerAddress operatorAddress;
private final boolean isRequire32ByteSharedSecrets;
private final CryptoKeys keys;
private final ParentAccountSettings parentAccountSettings;
private ImmutableSpspServerSettings(ImmutableSpspServerSettings.Builder builder) {
this.parentAccountSettings = builder.parentAccountSettings;
if (builder.operatorAddress != null) {
initShim.operatorAddress(builder.operatorAddress);
}
if (builder.isRequire32ByteSharedSecretsIsSet()) {
initShim.isRequire32ByteSharedSecrets(builder.isRequire32ByteSharedSecrets);
}
if (builder.keys != null) {
initShim.keys(builder.keys);
}
this.operatorAddress = initShim.operatorAddress();
this.isRequire32ByteSharedSecrets = initShim.isRequire32ByteSharedSecrets();
this.keys = initShim.keys();
this.initShim = null;
}
private ImmutableSpspServerSettings(
InterledgerAddress operatorAddress,
boolean isRequire32ByteSharedSecrets,
CryptoKeys keys,
ParentAccountSettings parentAccountSettings) {
this.operatorAddress = operatorAddress;
this.isRequire32ByteSharedSecrets = isRequire32ByteSharedSecrets;
this.keys = keys;
this.parentAccountSettings = parentAccountSettings;
this.initShim = null;
}
private static final byte STAGE_INITIALIZING = -1;
private static final byte STAGE_UNINITIALIZED = 0;
private static final byte STAGE_INITIALIZED = 1;
@SuppressWarnings("Immutable")
private transient volatile InitShim initShim = new InitShim();
@Generated(from = "SpspServerSettings", generator = "Immutables")
private final class InitShim {
private byte operatorAddressBuildStage = STAGE_UNINITIALIZED;
private InterledgerAddress operatorAddress;
InterledgerAddress operatorAddress() {
if (operatorAddressBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (operatorAddressBuildStage == STAGE_UNINITIALIZED) {
operatorAddressBuildStage = STAGE_INITIALIZING;
this.operatorAddress = Objects.requireNonNull(operatorAddressInitialize(), "operatorAddress");
operatorAddressBuildStage = STAGE_INITIALIZED;
}
return this.operatorAddress;
}
void operatorAddress(InterledgerAddress operatorAddress) {
this.operatorAddress = operatorAddress;
operatorAddressBuildStage = STAGE_INITIALIZED;
}
private byte isRequire32ByteSharedSecretsBuildStage = STAGE_UNINITIALIZED;
private boolean isRequire32ByteSharedSecrets;
boolean isRequire32ByteSharedSecrets() {
if (isRequire32ByteSharedSecretsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (isRequire32ByteSharedSecretsBuildStage == STAGE_UNINITIALIZED) {
isRequire32ByteSharedSecretsBuildStage = STAGE_INITIALIZING;
this.isRequire32ByteSharedSecrets = isRequire32ByteSharedSecretsInitialize();
isRequire32ByteSharedSecretsBuildStage = STAGE_INITIALIZED;
}
return this.isRequire32ByteSharedSecrets;
}
void isRequire32ByteSharedSecrets(boolean isRequire32ByteSharedSecrets) {
this.isRequire32ByteSharedSecrets = isRequire32ByteSharedSecrets;
isRequire32ByteSharedSecretsBuildStage = STAGE_INITIALIZED;
}
private byte keysBuildStage = STAGE_UNINITIALIZED;
private CryptoKeys keys;
CryptoKeys keys() {
if (keysBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (keysBuildStage == STAGE_UNINITIALIZED) {
keysBuildStage = STAGE_INITIALIZING;
this.keys = Objects.requireNonNull(keysInitialize(), "keys");
keysBuildStage = STAGE_INITIALIZED;
}
return this.keys;
}
void keys(CryptoKeys keys) {
this.keys = keys;
keysBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (operatorAddressBuildStage == STAGE_INITIALIZING) attributes.add("operatorAddress");
if (isRequire32ByteSharedSecretsBuildStage == STAGE_INITIALIZING) attributes.add("isRequire32ByteSharedSecrets");
if (keysBuildStage == STAGE_INITIALIZING) attributes.add("keys");
return "Cannot build SpspServerSettings, attribute initializers form cycle " + attributes;
}
}
private InterledgerAddress operatorAddressInitialize() {
return SpspServerSettings.super.operatorAddress();
}
private boolean isRequire32ByteSharedSecretsInitialize() {
return SpspServerSettings.super.isRequire32ByteSharedSecrets();
}
private CryptoKeys keysInitialize() {
return SpspServerSettings.super.keys();
}
/**
* The ILP Address of this connector. Note that the Connector's initial properties may not specify an address, in
* which case the default will be {@link Link#SELF}. In this case the Connector will use IL-DCP to obtain its
* operating address.
* @return The ILP address of this connector.
*/
@Override
public InterledgerAddress operatorAddress() {
InitShim shim = this.initShim;
return shim != null
? shim.operatorAddress()
: this.operatorAddress;
}
/**
* Flag to control if shared secrets must be 32 bytes
* @return true if required otherwise anything goes
*/
@Override
public boolean isRequire32ByteSharedSecrets() {
InitShim shim = this.initShim;
return shim != null
? shim.isRequire32ByteSharedSecrets()
: this.isRequire32ByteSharedSecrets;
}
/**
* Keys the connector will use for various core functions.
* @return keys
*/
@Override
public CryptoKeys keys() {
InitShim shim = this.initShim;
return shim != null
? shim.keys()
: this.keys;
}
/**
* The account settings for the parent account of this Receiver.
* @return A {@link ParentAccountSettings}.
*/
@Override
public ParentAccountSettings parentAccountSettings() {
return parentAccountSettings;
}
/**
* Copy the current immutable object by setting a value for the {@link SpspServerSettings#operatorAddress() operatorAddress} 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 operatorAddress
* @return A modified copy of the {@code this} object
*/
public final ImmutableSpspServerSettings withOperatorAddress(InterledgerAddress value) {
if (this.operatorAddress == value) return this;
InterledgerAddress newValue = Objects.requireNonNull(value, "operatorAddress");
return validate(new ImmutableSpspServerSettings(newValue, this.isRequire32ByteSharedSecrets, this.keys, this.parentAccountSettings));
}
/**
* Copy the current immutable object by setting a value for the {@link SpspServerSettings#isRequire32ByteSharedSecrets() isRequire32ByteSharedSecrets} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for isRequire32ByteSharedSecrets
* @return A modified copy of the {@code this} object
*/
public final ImmutableSpspServerSettings withIsRequire32ByteSharedSecrets(boolean value) {
if (this.isRequire32ByteSharedSecrets == value) return this;
return validate(new ImmutableSpspServerSettings(this.operatorAddress, value, this.keys, this.parentAccountSettings));
}
/**
* Copy the current immutable object by setting a value for the {@link SpspServerSettings#keys() keys} 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 keys
* @return A modified copy of the {@code this} object
*/
public final ImmutableSpspServerSettings withKeys(CryptoKeys value) {
if (this.keys == value) return this;
CryptoKeys newValue = Objects.requireNonNull(value, "keys");
return validate(new ImmutableSpspServerSettings(this.operatorAddress, this.isRequire32ByteSharedSecrets, newValue, this.parentAccountSettings));
}
/**
* Copy the current immutable object by setting a value for the {@link SpspServerSettings#parentAccountSettings() parentAccountSettings} 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 parentAccountSettings
* @return A modified copy of the {@code this} object
*/
public final ImmutableSpspServerSettings withParentAccountSettings(ParentAccountSettings value) {
if (this.parentAccountSettings == value) return this;
ParentAccountSettings newValue = Objects.requireNonNull(value, "parentAccountSettings");
return validate(new ImmutableSpspServerSettings(this.operatorAddress, this.isRequire32ByteSharedSecrets, this.keys, newValue));
}
/**
* This instance is equal to all instances of {@code ImmutableSpspServerSettings} that have equal attribute values.
* As instances of the {@code ImmutableSpspServerSettings} class are interned, the {@code equals} method is implemented
* as an efficient reference equality check.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
return this == another;
}
private boolean equalTo(ImmutableSpspServerSettings another) {
return operatorAddress.equals(another.operatorAddress)
&& isRequire32ByteSharedSecrets == another.isRequire32ByteSharedSecrets
&& keys.equals(another.keys)
&& parentAccountSettings.equals(another.parentAccountSettings);
}
/**
* Computes a hash code from attributes: {@code operatorAddress}, {@code isRequire32ByteSharedSecrets}, {@code keys}, {@code parentAccountSettings}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + operatorAddress.hashCode();
h += (h << 5) + Booleans.hashCode(isRequire32ByteSharedSecrets);
h += (h << 5) + keys.hashCode();
h += (h << 5) + parentAccountSettings.hashCode();
return h;
}
/**
* Prints the immutable value {@code SpspServerSettings} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("SpspServerSettings")
.omitNullValues()
.add("operatorAddress", operatorAddress)
.add("isRequire32ByteSharedSecrets", isRequire32ByteSharedSecrets)
.add("keys", keys)
.add("parentAccountSettings", parentAccountSettings)
.toString();
}
@Generated(from = "SpspServerSettings", generator = "Immutables")
private static class InternProxy {
final ImmutableSpspServerSettings instance;
InternProxy(ImmutableSpspServerSettings instance) {
this.instance = instance;
}
@Override
public boolean equals(@Nullable Object another) {
return another != null && instance.equalTo(((InternProxy) another).instance);
}
@Override
public int hashCode() {
return instance.hashCode();
}
}
private static final Interner INTERNER = Interners.newStrongInterner();
private static ImmutableSpspServerSettings validate(ImmutableSpspServerSettings instance) {
return INTERNER.intern(new InternProxy(instance)).instance;
}
/**
* Creates an immutable copy of a {@link SpspServerSettings} 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 SpspServerSettings instance
*/
public static ImmutableSpspServerSettings copyOf(SpspServerSettings instance) {
if (instance instanceof ImmutableSpspServerSettings) {
return (ImmutableSpspServerSettings) instance;
}
return ImmutableSpspServerSettings.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableSpspServerSettings ImmutableSpspServerSettings}.
*
* ImmutableSpspServerSettings.builder()
* .operatorAddress(org.interledger.core.InterledgerAddress) // optional {@link SpspServerSettings#operatorAddress() operatorAddress}
* .isRequire32ByteSharedSecrets(boolean) // optional {@link SpspServerSettings#isRequire32ByteSharedSecrets() isRequire32ByteSharedSecrets}
* .keys(org.interledger.crypto.CryptoKeys) // optional {@link SpspServerSettings#keys() keys}
* .parentAccountSettings(org.interledger.spsp.server.model.ParentAccountSettings) // required {@link SpspServerSettings#parentAccountSettings() parentAccountSettings}
* .build();
*
* @return A new ImmutableSpspServerSettings builder
*/
public static ImmutableSpspServerSettings.Builder builder() {
return new ImmutableSpspServerSettings.Builder();
}
/**
* Builds instances of type {@link ImmutableSpspServerSettings ImmutableSpspServerSettings}.
* 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 = "SpspServerSettings", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_PARENT_ACCOUNT_SETTINGS = 0x1L;
private static final long OPT_BIT_IS_REQUIRE32_BYTE_SHARED_SECRETS = 0x1L;
private long initBits = 0x1L;
private long optBits;
private @Nullable InterledgerAddress operatorAddress;
private boolean isRequire32ByteSharedSecrets;
private @Nullable CryptoKeys keys;
private @Nullable ParentAccountSettings parentAccountSettings;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ModifiableSpspServerSettings} 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(ModifiableSpspServerSettings instance) {
Objects.requireNonNull(instance, "instance");
operatorAddress(instance.operatorAddress());
isRequire32ByteSharedSecrets(instance.isRequire32ByteSharedSecrets());
keys(instance.keys());
if (instance.parentAccountSettingsIsSet()) {
parentAccountSettings(instance.parentAccountSettings());
}
return this;
}
/**
* Fill a builder with attribute values from the provided {@code SpspServerSettings} 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(SpspServerSettings instance) {
Objects.requireNonNull(instance, "instance");
if (instance instanceof ModifiableSpspServerSettings) {
return from((ModifiableSpspServerSettings) instance);
}
operatorAddress(instance.operatorAddress());
isRequire32ByteSharedSecrets(instance.isRequire32ByteSharedSecrets());
keys(instance.keys());
parentAccountSettings(instance.parentAccountSettings());
return this;
}
/**
* Initializes the value for the {@link SpspServerSettings#operatorAddress() operatorAddress} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link SpspServerSettings#operatorAddress() operatorAddress}.
* @param operatorAddress The value for operatorAddress
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder operatorAddress(InterledgerAddress operatorAddress) {
this.operatorAddress = Objects.requireNonNull(operatorAddress, "operatorAddress");
return this;
}
/**
* Initializes the value for the {@link SpspServerSettings#isRequire32ByteSharedSecrets() isRequire32ByteSharedSecrets} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link SpspServerSettings#isRequire32ByteSharedSecrets() isRequire32ByteSharedSecrets}.
* @param isRequire32ByteSharedSecrets The value for isRequire32ByteSharedSecrets
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder isRequire32ByteSharedSecrets(boolean isRequire32ByteSharedSecrets) {
this.isRequire32ByteSharedSecrets = isRequire32ByteSharedSecrets;
optBits |= OPT_BIT_IS_REQUIRE32_BYTE_SHARED_SECRETS;
return this;
}
/**
* Initializes the value for the {@link SpspServerSettings#keys() keys} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link SpspServerSettings#keys() keys}.
* @param keys The value for keys
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder keys(CryptoKeys keys) {
this.keys = Objects.requireNonNull(keys, "keys");
return this;
}
/**
* Initializes the value for the {@link SpspServerSettings#parentAccountSettings() parentAccountSettings} attribute.
* @param parentAccountSettings The value for parentAccountSettings
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder parentAccountSettings(ParentAccountSettings parentAccountSettings) {
this.parentAccountSettings = Objects.requireNonNull(parentAccountSettings, "parentAccountSettings");
initBits &= ~INIT_BIT_PARENT_ACCOUNT_SETTINGS;
return this;
}
/**
* Builds a new {@link ImmutableSpspServerSettings ImmutableSpspServerSettings}.
* @return An immutable instance of SpspServerSettings
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableSpspServerSettings build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableSpspServerSettings.validate(new ImmutableSpspServerSettings(this));
}
private boolean isRequire32ByteSharedSecretsIsSet() {
return (optBits & OPT_BIT_IS_REQUIRE32_BYTE_SHARED_SECRETS) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_PARENT_ACCOUNT_SETTINGS) != 0) attributes.add("parentAccountSettings");
return "Cannot build SpspServerSettings, some of required attributes are not set " + attributes;
}
}
}