org.interledger.link.ImmutableLinkSettings Maven / Gradle / Ivy
Show all versions of link-core Show documentation
package org.interledger.link;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 LinkSettings.AbstractLinkSettings}.
*
* Use the builder to create immutable instances:
* {@code ImmutableLinkSettings.builder()}.
*/
@Generated(from = "LinkSettings.AbstractLinkSettings", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableLinkSettings extends LinkSettings.AbstractLinkSettings {
private final LinkType linkType;
private final ImmutableMap customSettings;
private ImmutableLinkSettings(
LinkType linkType,
ImmutableMap customSettings) {
this.linkType = linkType;
this.customSettings = customSettings;
}
/**
* The type of this ledger link.
* @return A {@link LinkType}
*/
@Override
public LinkType getLinkType() {
return linkType;
}
/**
* Additional, custom settings that any link can define. Redacted to prevent credential leakage in log files.
* @return A {@link Map} with {@link String} keys.
*/
@Override
public ImmutableMap getCustomSettings() {
return customSettings;
}
/**
* Copy the current immutable object by setting a value for the {@link LinkSettings.AbstractLinkSettings#getLinkType() linkType} 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 linkType
* @return A modified copy of the {@code this} object
*/
public final ImmutableLinkSettings withLinkType(LinkType value) {
if (this.linkType == value) return this;
LinkType newValue = Objects.requireNonNull(value, "linkType");
return validate(new ImmutableLinkSettings(newValue, this.customSettings));
}
/**
* Copy the current immutable object by replacing the {@link LinkSettings.AbstractLinkSettings#getCustomSettings() customSettings} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the customSettings map
* @return A modified copy of {@code this} object
*/
public final ImmutableLinkSettings withCustomSettings(Map entries) {
if (this.customSettings == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return validate(new ImmutableLinkSettings(this.linkType, newValue));
}
/**
* This instance is equal to all instances of {@code ImmutableLinkSettings} 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 ImmutableLinkSettings
&& equalTo((ImmutableLinkSettings) another);
}
private boolean equalTo(ImmutableLinkSettings another) {
return linkType.equals(another.linkType)
&& customSettings.equals(another.customSettings);
}
/**
* Computes a hash code from attributes: {@code linkType}, {@code customSettings}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + linkType.hashCode();
h += (h << 5) + customSettings.hashCode();
return h;
}
/**
* Prints the immutable value {@code LinkSettings} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("LinkSettings")
.omitNullValues()
.add("linkType", linkType)
.toString();
}
private static ImmutableLinkSettings validate(ImmutableLinkSettings instance) {
instance = (ImmutableLinkSettings) instance.normalize();
return instance;
}
/**
* Creates an immutable copy of a {@link LinkSettings.AbstractLinkSettings} 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 LinkSettings instance
*/
public static ImmutableLinkSettings copyOf(LinkSettings.AbstractLinkSettings instance) {
if (instance instanceof ImmutableLinkSettings) {
return (ImmutableLinkSettings) instance;
}
return ImmutableLinkSettings.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableLinkSettings ImmutableLinkSettings}.
*
* ImmutableLinkSettings.builder()
* .linkType(org.interledger.link.LinkType) // required {@link LinkSettings.AbstractLinkSettings#getLinkType() linkType}
* .putCustomSettings|putAllCustomSettings(String => Object) // {@link LinkSettings.AbstractLinkSettings#getCustomSettings() customSettings} mappings
* .build();
*
* @return A new ImmutableLinkSettings builder
*/
public static ImmutableLinkSettings.Builder builder() {
return new ImmutableLinkSettings.Builder();
}
/**
* Builds instances of type {@link ImmutableLinkSettings ImmutableLinkSettings}.
* 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 = "LinkSettings.AbstractLinkSettings", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_LINK_TYPE = 0x1L;
private long initBits = 0x1L;
private @Nullable LinkType linkType;
private ImmutableMap.Builder customSettings = ImmutableMap.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.link.LinkSettings} 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(LinkSettings instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.link.LinkSettings.AbstractLinkSettings} 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(LinkSettings.AbstractLinkSettings instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
@Var long bits = 0;
if (object instanceof LinkSettings) {
LinkSettings instance = (LinkSettings) object;
if ((bits & 0x1L) == 0) {
putAllCustomSettings(instance.getCustomSettings());
bits |= 0x1L;
}
linkType(instance.getLinkType());
}
if (object instanceof LinkSettings.AbstractLinkSettings) {
LinkSettings.AbstractLinkSettings instance = (LinkSettings.AbstractLinkSettings) object;
if ((bits & 0x1L) == 0) {
putAllCustomSettings(instance.getCustomSettings());
bits |= 0x1L;
}
}
}
/**
* Initializes the value for the {@link LinkSettings.AbstractLinkSettings#getLinkType() linkType} attribute.
* @param linkType The value for linkType
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder linkType(LinkType linkType) {
this.linkType = Objects.requireNonNull(linkType, "linkType");
initBits &= ~INIT_BIT_LINK_TYPE;
return this;
}
/**
* Put one entry to the {@link LinkSettings.AbstractLinkSettings#getCustomSettings() customSettings} map.
* @param key The key in the customSettings map
* @param value The associated value in the customSettings map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putCustomSettings(String key, Object value) {
this.customSettings.put(key, value);
return this;
}
/**
* Put one entry to the {@link LinkSettings.AbstractLinkSettings#getCustomSettings() customSettings} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putCustomSettings(Map.Entry entry) {
this.customSettings.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link LinkSettings.AbstractLinkSettings#getCustomSettings() customSettings} map. Nulls are not permitted
* @param entries The entries that will be added to the customSettings map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder customSettings(Map entries) {
this.customSettings = ImmutableMap.builder();
return putAllCustomSettings(entries);
}
/**
* Put all mappings from the specified map as entries to {@link LinkSettings.AbstractLinkSettings#getCustomSettings() customSettings} map. Nulls are not permitted
* @param entries The entries that will be added to the customSettings map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllCustomSettings(Map entries) {
this.customSettings.putAll(entries);
return this;
}
/**
* Builds a new {@link ImmutableLinkSettings ImmutableLinkSettings}.
* @return An immutable instance of LinkSettings
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableLinkSettings build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return ImmutableLinkSettings.validate(new ImmutableLinkSettings(linkType, customSettings.build()));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_LINK_TYPE) != 0) attributes.add("linkType");
return "Cannot build LinkSettings, some of required attributes are not set " + attributes;
}
}
}