org.interledger.stream.ImmutableDenomination Maven / Gradle / Ivy
Show all versions of stream-core Show documentation
package org.interledger.stream;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Shorts;
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 Denomination}.
*
* Use the builder to create immutable instances:
* {@code ImmutableDenomination.builder()}.
*/
@Generated(from = "Denomination", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableDenomination implements Denomination {
private final String assetCode;
private final short assetScale;
private ImmutableDenomination(String assetCode, short assetScale) {
this.assetCode = assetCode;
this.assetScale = assetScale;
}
/**
* Currency code or other asset identifier. For example, `USD`, `EUR`, or `BTC`.
* @return A {@link String} containing the asset code.
*/
@Override
public String assetCode() {
return assetCode;
}
/**
*
An asset scale is the difference, in orders of magnitude, between an asset's `standard unit` and a
* corresponding `fractional unit`.
* A standard unit represents the typical unit of account for a particular asset. For example 1 USD in the case of
* U.S. dollars, or 1 BTC in the case of Bitcoin (Note that peers are free to define this value in any way, but
* participants in an Interledger accounting relationship must be sure to use the same value. Thus, it is suggested to
* use typical values when possible).
* A fractional unit represents some unit smaller than its corresponding standard unit, but with greater
* precision. Examples of fractional monetary units include one cent ($0.01 USD), or 1 satoshi (0.00000001 BTC).
* Because Interledger amounts are integers, but most currencies are typically represented as fractional units
* (e.g. cents), this property defines how many Interledger units make up one standard unit of the asset code
* specified above.
* More formally, the asset scale is a non-negative integer (0, 1, 2, …) such that one standard unit equals
* 10^(-scale) of a corresponding fractional unit. If the fractional unit equals the standard unit, then the asset
* scale is 0.
* For example, one "cent" represents an asset scale of 2 in the case of USD; 1 satoshi represents an asset scale
* of 8 in the case of Bitcoin; and 1 drop represents an asset scale of 6 in XRP.
* @return A {@link Short} representing the asset scale.
*/
@Override
public short assetScale() {
return assetScale;
}
/**
* Copy the current immutable object by setting a value for the {@link Denomination#assetCode() assetCode} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for assetCode
* @return A modified copy of the {@code this} object
*/
public final ImmutableDenomination withAssetCode(String value) {
String newValue = Objects.requireNonNull(value, "assetCode");
if (this.assetCode.equals(newValue)) return this;
return new ImmutableDenomination(newValue, this.assetScale);
}
/**
* Copy the current immutable object by setting a value for the {@link Denomination#assetScale() assetScale} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for assetScale
* @return A modified copy of the {@code this} object
*/
public final ImmutableDenomination withAssetScale(short value) {
if (this.assetScale == value) return this;
return new ImmutableDenomination(this.assetCode, value);
}
/**
* This instance is equal to all instances of {@code ImmutableDenomination} 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 ImmutableDenomination
&& equalTo((ImmutableDenomination) another);
}
private boolean equalTo(ImmutableDenomination another) {
return assetCode.equals(another.assetCode)
&& assetScale == another.assetScale;
}
/**
* Computes a hash code from attributes: {@code assetCode}, {@code assetScale}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + assetCode.hashCode();
h += (h << 5) + Shorts.hashCode(assetScale);
return h;
}
/**
* Prints the immutable value {@code Denomination} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Denomination")
.omitNullValues()
.add("assetCode", assetCode)
.add("assetScale", assetScale)
.toString();
}
/**
* Creates an immutable copy of a {@link Denomination} 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 Denomination instance
*/
public static ImmutableDenomination copyOf(Denomination instance) {
if (instance instanceof ImmutableDenomination) {
return (ImmutableDenomination) instance;
}
return ImmutableDenomination.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableDenomination ImmutableDenomination}.
*
* ImmutableDenomination.builder()
* .assetCode(String) // required {@link Denomination#assetCode() assetCode}
* .assetScale(short) // required {@link Denomination#assetScale() assetScale}
* .build();
*
* @return A new ImmutableDenomination builder
*/
public static ImmutableDenomination.Builder builder() {
return new ImmutableDenomination.Builder();
}
/**
* Builds instances of type {@link ImmutableDenomination ImmutableDenomination}.
* 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 = "Denomination", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_ASSET_CODE = 0x1L;
private static final long INIT_BIT_ASSET_SCALE = 0x2L;
private long initBits = 0x3L;
private @Nullable String assetCode;
private short assetScale;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Denomination} 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(Denomination instance) {
Objects.requireNonNull(instance, "instance");
assetCode(instance.assetCode());
assetScale(instance.assetScale());
return this;
}
/**
* Initializes the value for the {@link Denomination#assetCode() assetCode} attribute.
* @param assetCode The value for assetCode
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder assetCode(String assetCode) {
this.assetCode = Objects.requireNonNull(assetCode, "assetCode");
initBits &= ~INIT_BIT_ASSET_CODE;
return this;
}
/**
* Initializes the value for the {@link Denomination#assetScale() assetScale} attribute.
* @param assetScale The value for assetScale
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder assetScale(short assetScale) {
this.assetScale = assetScale;
initBits &= ~INIT_BIT_ASSET_SCALE;
return this;
}
/**
* Builds a new {@link ImmutableDenomination ImmutableDenomination}.
* @return An immutable instance of Denomination
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableDenomination build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableDenomination(assetCode, assetScale);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ASSET_CODE) != 0) attributes.add("assetCode");
if ((initBits & INIT_BIT_ASSET_SCALE) != 0) attributes.add("assetScale");
return "Cannot build Denomination, some of required attributes are not set " + attributes;
}
}
}