org.interledger.stream.PrepareAmountsBuilder 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.UnsignedLong;
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;
/**
* Builds instances of type {@link PrepareAmounts PrepareAmounts}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code PrepareAmountsBuilder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "PrepareAmounts", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@NotThreadSafe
public final class PrepareAmountsBuilder {
private static final long INIT_BIT_AMOUNT_TO_SEND = 0x1L;
private static final long INIT_BIT_MINIMUM_AMOUNT_TO_ACCEPT = 0x2L;
private long initBits = 0x3L;
private @Nullable UnsignedLong amountToSend;
private @Nullable UnsignedLong minimumAmountToAccept;
/**
* Creates a builder for {@link PrepareAmounts PrepareAmounts} instances.
*
* new PrepareAmountsBuilder()
* .amountToSend(com.google.common.primitives.UnsignedLong) // required {@link PrepareAmounts#getAmountToSend() amountToSend}
* .minimumAmountToAccept(com.google.common.primitives.UnsignedLong) // required {@link PrepareAmounts#getMinimumAmountToAccept() minimumAmountToAccept}
* .build();
*
*/
public PrepareAmountsBuilder() {
}
/**
* Fill a builder with attribute values from the provided {@code PrepareAmounts} 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 PrepareAmountsBuilder from(PrepareAmounts instance) {
Objects.requireNonNull(instance, "instance");
amountToSend(instance.getAmountToSend());
minimumAmountToAccept(instance.getMinimumAmountToAccept());
return this;
}
/**
* Initializes the value for the {@link PrepareAmounts#getAmountToSend() amountToSend} attribute.
* @param amountToSend The value for amountToSend
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final PrepareAmountsBuilder amountToSend(UnsignedLong amountToSend) {
this.amountToSend = Objects.requireNonNull(amountToSend, "amountToSend");
initBits &= ~INIT_BIT_AMOUNT_TO_SEND;
return this;
}
/**
* Initializes the value for the {@link PrepareAmounts#getMinimumAmountToAccept() minimumAmountToAccept} attribute.
* @param minimumAmountToAccept The value for minimumAmountToAccept
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final PrepareAmountsBuilder minimumAmountToAccept(UnsignedLong minimumAmountToAccept) {
this.minimumAmountToAccept = Objects.requireNonNull(minimumAmountToAccept, "minimumAmountToAccept");
initBits &= ~INIT_BIT_MINIMUM_AMOUNT_TO_ACCEPT;
return this;
}
/**
* Builds a new {@link PrepareAmounts PrepareAmounts}.
* @return An immutable instance of PrepareAmounts
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public PrepareAmounts build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new PrepareAmountsBuilder.ImmutablePrepareAmounts(this);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_AMOUNT_TO_SEND) != 0) attributes.add("amountToSend");
if ((initBits & INIT_BIT_MINIMUM_AMOUNT_TO_ACCEPT) != 0) attributes.add("minimumAmountToAccept");
return "Cannot build PrepareAmounts, some of required attributes are not set " + attributes;
}
/**
* Immutable implementation of {@link PrepareAmounts}.
*
* Use the builder to create immutable instances:
* {@code new PrepareAmountsBuilder()}.
*/
@Generated(from = "PrepareAmounts", generator = "Immutables")
@Immutable
@CheckReturnValue
private static final class ImmutablePrepareAmounts implements PrepareAmounts {
private final UnsignedLong amountToSend;
private final UnsignedLong minimumAmountToAccept;
private ImmutablePrepareAmounts(PrepareAmountsBuilder builder) {
this.amountToSend = builder.amountToSend;
this.minimumAmountToAccept = builder.minimumAmountToAccept;
}
/**
* @return The value of the {@code amountToSend} attribute
*/
@Override
public UnsignedLong getAmountToSend() {
return amountToSend;
}
/**
* @return The value of the {@code minimumAmountToAccept} attribute
*/
@Override
public UnsignedLong getMinimumAmountToAccept() {
return minimumAmountToAccept;
}
/**
* This instance is equal to all instances of {@code ImmutablePrepareAmounts} 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 PrepareAmountsBuilder.ImmutablePrepareAmounts
&& equalTo((PrepareAmountsBuilder.ImmutablePrepareAmounts) another);
}
private boolean equalTo(PrepareAmountsBuilder.ImmutablePrepareAmounts another) {
return amountToSend.equals(another.amountToSend)
&& minimumAmountToAccept.equals(another.minimumAmountToAccept);
}
/**
* Computes a hash code from attributes: {@code amountToSend}, {@code minimumAmountToAccept}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + amountToSend.hashCode();
h += (h << 5) + minimumAmountToAccept.hashCode();
return h;
}
/**
* Prints the immutable value {@code PrepareAmounts} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("PrepareAmounts")
.omitNullValues()
.add("amountToSend", amountToSend)
.add("minimumAmountToAccept", minimumAmountToAccept)
.toString();
}
}
}