org.interledger.ildcp.IldcpResponsePacketBuilder Maven / Gradle / Ivy
Show all versions of ildcp-core Show documentation
package org.interledger.ildcp;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.Arrays;
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.InterledgerFulfillPacket;
import org.interledger.core.InterledgerFulfillment;
import org.interledger.core.InterledgerPacket;
/**
* Builds instances of type {@link IldcpResponsePacket.AbstractIldcpResponsePacket AbstractIldcpResponsePacket}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code IldcpResponsePacketBuilder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "IldcpResponsePacket.AbstractIldcpResponsePacket", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@NotThreadSafe
public final class IldcpResponsePacketBuilder {
private static final long INIT_BIT_ILDCP_RESPONSE = 0x1L;
private long initBits = 0x1L;
private @Nullable IldcpResponse ildcpResponse;
private @Nullable InterledgerFulfillment fulfillment;
private @Nullable byte[] data;
/**
* Creates a builder for {@link IldcpResponsePacket.AbstractIldcpResponsePacket AbstractIldcpResponsePacket} instances.
*
* new IldcpResponsePacketBuilder()
* .ildcpResponse(org.interledger.ildcp.IldcpResponse) // required {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getIldcpResponse() ildcpResponse}
* .fulfillment(org.interledger.core.InterledgerFulfillment) // optional {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getFulfillment() fulfillment}
* .data(byte) // optional {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getData() data}
* .build();
*
*/
public IldcpResponsePacketBuilder() {
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.core.InterledgerFulfillPacket} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder from(InterledgerFulfillPacket instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.ildcp.IldcpResponsePacket.AbstractIldcpResponsePacket} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder from(IldcpResponsePacket.AbstractIldcpResponsePacket instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.core.InterledgerPacket} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder from(InterledgerPacket instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code org.interledger.ildcp.IldcpResponsePacket} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder from(IldcpResponsePacket instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
@Var long bits = 0;
if (object instanceof InterledgerFulfillPacket) {
InterledgerFulfillPacket instance = (InterledgerFulfillPacket) object;
if ((bits & 0x2L) == 0) {
fulfillment(instance.getFulfillment());
bits |= 0x2L;
}
}
if (object instanceof IldcpResponsePacket.AbstractIldcpResponsePacket) {
IldcpResponsePacket.AbstractIldcpResponsePacket instance = (IldcpResponsePacket.AbstractIldcpResponsePacket) object;
if ((bits & 0x1L) == 0) {
data(instance.getData());
bits |= 0x1L;
}
if ((bits & 0x2L) == 0) {
fulfillment(instance.getFulfillment());
bits |= 0x2L;
}
}
if (object instanceof InterledgerPacket) {
InterledgerPacket instance = (InterledgerPacket) object;
if ((bits & 0x1L) == 0) {
data(instance.getData());
bits |= 0x1L;
}
}
if (object instanceof IldcpResponsePacket) {
IldcpResponsePacket instance = (IldcpResponsePacket) object;
ildcpResponse(instance.getIldcpResponse());
if ((bits & 0x2L) == 0) {
fulfillment(instance.getFulfillment());
bits |= 0x2L;
}
}
}
/**
* Initializes the value for the {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getIldcpResponse() ildcpResponse} attribute.
* @param ildcpResponse The value for ildcpResponse
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder ildcpResponse(IldcpResponse ildcpResponse) {
this.ildcpResponse = Objects.requireNonNull(ildcpResponse, "ildcpResponse");
initBits &= ~INIT_BIT_ILDCP_RESPONSE;
return this;
}
/**
* Initializes the value for the {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getFulfillment() fulfillment} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getFulfillment() fulfillment}.
* @param fulfillment The value for fulfillment
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder fulfillment(InterledgerFulfillment fulfillment) {
this.fulfillment = Objects.requireNonNull(fulfillment, "fulfillment");
return this;
}
/**
* Initializes the value for the {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getData() data} attribute.
*
If not set, this attribute will have a default value as defined by {@link IldcpResponsePacket.AbstractIldcpResponsePacket#getData() data}.
* @param data The elements for data
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final IldcpResponsePacketBuilder data(byte... data) {
this.data = data.clone();
return this;
}
/**
* Builds a new {@link IldcpResponsePacket.AbstractIldcpResponsePacket AbstractIldcpResponsePacket}.
* @return An immutable instance of IldcpResponsePacket
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public IldcpResponsePacket.AbstractIldcpResponsePacket build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new IldcpResponsePacketBuilder.ImmutableIldcpResponsePacket(this);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ILDCP_RESPONSE) != 0) attributes.add("ildcpResponse");
return "Cannot build IldcpResponsePacket, some of required attributes are not set " + attributes;
}
/**
* Immutable implementation of {@link IldcpResponsePacket.AbstractIldcpResponsePacket}.
*
* Use the builder to create immutable instances:
* {@code new IldcpResponsePacketBuilder()}.
*/
@Generated(from = "IldcpResponsePacket.AbstractIldcpResponsePacket", generator = "Immutables")
@Immutable
@CheckReturnValue
private static final class ImmutableIldcpResponsePacket
extends IldcpResponsePacket.AbstractIldcpResponsePacket {
private final IldcpResponse ildcpResponse;
private final InterledgerFulfillment fulfillment;
private final byte[] data;
private ImmutableIldcpResponsePacket(IldcpResponsePacketBuilder builder) {
this.ildcpResponse = builder.ildcpResponse;
if (builder.fulfillment != null) {
initShim.fulfillment(builder.fulfillment);
}
if (builder.data != null) {
initShim.data(builder.data);
}
this.fulfillment = initShim.getFulfillment();
this.data = initShim.getData();
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 = "IldcpResponsePacket.AbstractIldcpResponsePacket", generator = "Immutables")
private final class InitShim {
private byte fulfillmentBuildStage = STAGE_UNINITIALIZED;
private InterledgerFulfillment fulfillment;
InterledgerFulfillment getFulfillment() {
if (fulfillmentBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (fulfillmentBuildStage == STAGE_UNINITIALIZED) {
fulfillmentBuildStage = STAGE_INITIALIZING;
this.fulfillment = Objects.requireNonNull(ImmutableIldcpResponsePacket.super.getFulfillment(), "fulfillment");
fulfillmentBuildStage = STAGE_INITIALIZED;
}
return this.fulfillment;
}
void fulfillment(InterledgerFulfillment fulfillment) {
this.fulfillment = fulfillment;
fulfillmentBuildStage = STAGE_INITIALIZED;
}
private byte dataBuildStage = STAGE_UNINITIALIZED;
private byte[] data;
byte[] getData() {
if (dataBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (dataBuildStage == STAGE_UNINITIALIZED) {
dataBuildStage = STAGE_INITIALIZING;
this.data = ImmutableIldcpResponsePacket.super.getData().clone();
dataBuildStage = STAGE_INITIALIZED;
}
return this.data;
}
void data(byte[] data) {
this.data = data;
dataBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (fulfillmentBuildStage == STAGE_INITIALIZING) attributes.add("fulfillment");
if (dataBuildStage == STAGE_INITIALIZING) attributes.add("data");
return "Cannot build IldcpResponsePacket, attribute initializers form cycle " + attributes;
}
}
/**
* @return The value of the {@code ildcpResponse} attribute
*/
@Override
public IldcpResponse getIldcpResponse() {
return ildcpResponse;
}
/**
* @return The value of the {@code fulfillment} attribute
*/
@Override
public InterledgerFulfillment getFulfillment() {
InitShim shim = this.initShim;
return shim != null
? shim.getFulfillment()
: this.fulfillment;
}
/**
* @return A cloned {@code data} array
*/
@Override
public byte[] getData() {
InitShim shim = this.initShim;
return shim != null
? shim.getData().clone()
: this.data.clone();
}
/**
* This instance is equal to all instances of {@code ImmutableIldcpResponsePacket} 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 IldcpResponsePacketBuilder.ImmutableIldcpResponsePacket
&& equalTo((IldcpResponsePacketBuilder.ImmutableIldcpResponsePacket) another);
}
private boolean equalTo(IldcpResponsePacketBuilder.ImmutableIldcpResponsePacket another) {
return ildcpResponse.equals(another.ildcpResponse)
&& fulfillment.equals(another.fulfillment)
&& Arrays.equals(data, another.data);
}
/**
* Computes a hash code from attributes: {@code ildcpResponse}, {@code fulfillment}, {@code data}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + ildcpResponse.hashCode();
h += (h << 5) + fulfillment.hashCode();
h += (h << 5) + Arrays.hashCode(data);
return h;
}
/**
* Prints the immutable value {@code IldcpResponsePacket} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("IldcpResponsePacket")
.omitNullValues()
.add("ildcpResponse", ildcpResponse)
.add("fulfillment", fulfillment)
.add("data", Arrays.toString(data))
.toString();
}
}
}