org.interledger.ildcp.IldcpRequestBuilder Maven / Gradle / Ivy
Show all versions of ildcp-core Show documentation
package org.interledger.ildcp;
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.time.Instant;
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 IldcpRequest.AbstractIldcpRequest AbstractIldcpRequest}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code IldcpRequestBuilder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "IldcpRequest.AbstractIldcpRequest", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@NotThreadSafe
public final class IldcpRequestBuilder {
private @Nullable UnsignedLong amount;
private @Nullable Instant expiresAt;
/**
* Creates a builder for {@link IldcpRequest.AbstractIldcpRequest AbstractIldcpRequest} instances.
*
* new IldcpRequestBuilder()
* .amount(com.google.common.primitives.UnsignedLong) // optional {@link IldcpRequest.AbstractIldcpRequest#getAmount() amount}
* .expiresAt(java.time.Instant) // optional {@link IldcpRequest.AbstractIldcpRequest#getExpiresAt() expiresAt}
* .build();
*
*/
public IldcpRequestBuilder() {
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.ildcp.IldcpRequest} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpRequestBuilder from(IldcpRequest instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.ildcp.IldcpRequest.AbstractIldcpRequest} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpRequestBuilder from(IldcpRequest.AbstractIldcpRequest instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
@Var long bits = 0;
if (object instanceof IldcpRequest) {
IldcpRequest instance = (IldcpRequest) object;
if ((bits & 0x1L) == 0) {
amount(instance.getAmount());
bits |= 0x1L;
}
if ((bits & 0x2L) == 0) {
expiresAt(instance.getExpiresAt());
bits |= 0x2L;
}
}
if (object instanceof IldcpRequest.AbstractIldcpRequest) {
IldcpRequest.AbstractIldcpRequest instance = (IldcpRequest.AbstractIldcpRequest) object;
if ((bits & 0x1L) == 0) {
amount(instance.getAmount());
bits |= 0x1L;
}
if ((bits & 0x2L) == 0) {
expiresAt(instance.getExpiresAt());
bits |= 0x2L;
}
}
}
/**
* Initializes the value for the {@link IldcpRequest.AbstractIldcpRequest#getAmount() amount} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link IldcpRequest.AbstractIldcpRequest#getAmount() amount}.
* @param amount The value for amount
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpRequestBuilder amount(UnsignedLong amount) {
this.amount = Objects.requireNonNull(amount, "amount");
return this;
}
/**
* Initializes the value for the {@link IldcpRequest.AbstractIldcpRequest#getExpiresAt() expiresAt} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link IldcpRequest.AbstractIldcpRequest#getExpiresAt() expiresAt}.
* @param expiresAt The value for expiresAt
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpRequestBuilder expiresAt(Instant expiresAt) {
this.expiresAt = Objects.requireNonNull(expiresAt, "expiresAt");
return this;
}
/**
* Builds a new {@link IldcpRequest.AbstractIldcpRequest AbstractIldcpRequest}.
* @return An immutable instance of IldcpRequest
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public IldcpRequest.AbstractIldcpRequest build() {
return new IldcpRequestBuilder.ImmutableIldcpRequest(this);
}
/**
* Immutable implementation of {@link IldcpRequest.AbstractIldcpRequest}.
*
* Use the builder to create immutable instances:
* {@code new IldcpRequestBuilder()}.
*/
@Generated(from = "IldcpRequest.AbstractIldcpRequest", generator = "Immutables")
@Immutable
@CheckReturnValue
private static final class ImmutableIldcpRequest extends IldcpRequest.AbstractIldcpRequest {
private final UnsignedLong amount;
private final Instant expiresAt;
private ImmutableIldcpRequest(IldcpRequestBuilder builder) {
if (builder.amount != null) {
initShim.amount(builder.amount);
}
if (builder.expiresAt != null) {
initShim.expiresAt(builder.expiresAt);
}
this.amount = initShim.getAmount();
this.expiresAt = initShim.getExpiresAt();
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 = "IldcpRequest.AbstractIldcpRequest", generator = "Immutables")
private final class InitShim {
private byte amountBuildStage = STAGE_UNINITIALIZED;
private UnsignedLong amount;
UnsignedLong getAmount() {
if (amountBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (amountBuildStage == STAGE_UNINITIALIZED) {
amountBuildStage = STAGE_INITIALIZING;
this.amount = Objects.requireNonNull(ImmutableIldcpRequest.super.getAmount(), "amount");
amountBuildStage = STAGE_INITIALIZED;
}
return this.amount;
}
void amount(UnsignedLong amount) {
this.amount = amount;
amountBuildStage = STAGE_INITIALIZED;
}
private byte expiresAtBuildStage = STAGE_UNINITIALIZED;
private Instant expiresAt;
Instant getExpiresAt() {
if (expiresAtBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (expiresAtBuildStage == STAGE_UNINITIALIZED) {
expiresAtBuildStage = STAGE_INITIALIZING;
this.expiresAt = Objects.requireNonNull(ImmutableIldcpRequest.super.getExpiresAt(), "expiresAt");
expiresAtBuildStage = STAGE_INITIALIZED;
}
return this.expiresAt;
}
void expiresAt(Instant expiresAt) {
this.expiresAt = expiresAt;
expiresAtBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (amountBuildStage == STAGE_INITIALIZING) attributes.add("amount");
if (expiresAtBuildStage == STAGE_INITIALIZING) attributes.add("expiresAt");
return "Cannot build IldcpRequest, attribute initializers form cycle " + attributes;
}
}
/**
* @return The value of the {@code amount} attribute
*/
@Override
public UnsignedLong getAmount() {
InitShim shim = this.initShim;
return shim != null
? shim.getAmount()
: this.amount;
}
/**
* @return The value of the {@code expiresAt} attribute
*/
@Override
public Instant getExpiresAt() {
InitShim shim = this.initShim;
return shim != null
? shim.getExpiresAt()
: this.expiresAt;
}
/**
* This instance is equal to all instances of {@code ImmutableIldcpRequest} 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 IldcpRequestBuilder.ImmutableIldcpRequest
&& equalTo((IldcpRequestBuilder.ImmutableIldcpRequest) another);
}
private boolean equalTo(IldcpRequestBuilder.ImmutableIldcpRequest another) {
return amount.equals(another.amount)
&& expiresAt.equals(another.expiresAt);
}
/**
* Computes a hash code from attributes: {@code amount}, {@code expiresAt}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + amount.hashCode();
h += (h << 5) + expiresAt.hashCode();
return h;
}
/**
* Prints the immutable value {@code IldcpRequest} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("IldcpRequest")
.omitNullValues()
.add("amount", amount)
.add("expiresAt", expiresAt)
.toString();
}
}
}