org.interledger.stream.StreamPacketBuilder 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.collect.ImmutableList;
import com.google.common.primitives.Booleans;
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;
import org.interledger.core.InterledgerPacketType;
import org.interledger.stream.frames.StreamFrame;
/**
* Builds instances of type {@link StreamPacket StreamPacket}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code StreamPacketBuilder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "StreamPacket", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@NotThreadSafe
public final class StreamPacketBuilder {
private static final long INIT_BIT_INTERLEDGER_PACKET_TYPE = 0x1L;
private static final long INIT_BIT_SEQUENCE = 0x2L;
private static final long INIT_BIT_PREPARE_AMOUNT = 0x4L;
private long initBits = 0x7L;
private @Nullable InterledgerPacketType interledgerPacketType;
private @Nullable UnsignedLong sequence;
private @Nullable UnsignedLong prepareAmount;
private ImmutableList.Builder frames = ImmutableList.builder();
/**
* Creates a builder for {@link StreamPacket StreamPacket} instances.
*
* new StreamPacketBuilder()
* .interledgerPacketType(org.interledger.core.InterledgerPacketType) // required {@link StreamPacket#interledgerPacketType() interledgerPacketType}
* .sequence(com.google.common.primitives.UnsignedLong) // required {@link StreamPacket#sequence() sequence}
* .prepareAmount(com.google.common.primitives.UnsignedLong) // required {@link StreamPacket#prepareAmount() prepareAmount}
* .addFrames|addAllFrames(org.interledger.stream.frames.StreamFrame) // {@link StreamPacket#frames() frames} elements
* .build();
*
*/
public StreamPacketBuilder() {
}
/**
* Fill a builder with attribute values from the provided {@code StreamPacket} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder from(StreamPacket instance) {
Objects.requireNonNull(instance, "instance");
interledgerPacketType(instance.interledgerPacketType());
sequence(instance.sequence());
prepareAmount(instance.prepareAmount());
addAllFrames(instance.frames());
return this;
}
/**
* Initializes the value for the {@link StreamPacket#interledgerPacketType() interledgerPacketType} attribute.
* @param interledgerPacketType The value for interledgerPacketType
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder interledgerPacketType(InterledgerPacketType interledgerPacketType) {
this.interledgerPacketType = Objects.requireNonNull(interledgerPacketType, "interledgerPacketType");
initBits &= ~INIT_BIT_INTERLEDGER_PACKET_TYPE;
return this;
}
/**
* Initializes the value for the {@link StreamPacket#sequence() sequence} attribute.
* @param sequence The value for sequence
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder sequence(UnsignedLong sequence) {
this.sequence = Objects.requireNonNull(sequence, "sequence");
initBits &= ~INIT_BIT_SEQUENCE;
return this;
}
/**
* Initializes the value for the {@link StreamPacket#prepareAmount() prepareAmount} attribute.
* @param prepareAmount The value for prepareAmount
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder prepareAmount(UnsignedLong prepareAmount) {
this.prepareAmount = Objects.requireNonNull(prepareAmount, "prepareAmount");
initBits &= ~INIT_BIT_PREPARE_AMOUNT;
return this;
}
/**
* Adds one element to {@link StreamPacket#frames() frames} list.
* @param element A frames element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder addFrames(StreamFrame element) {
this.frames.add(element);
return this;
}
/**
* Adds elements to {@link StreamPacket#frames() frames} list.
* @param elements An array of frames elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder addFrames(StreamFrame... elements) {
this.frames.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link StreamPacket#frames() frames} list.
* @param elements An iterable of frames elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder frames(Iterable extends StreamFrame> elements) {
this.frames = ImmutableList.builder();
return addAllFrames(elements);
}
/**
* Adds elements to {@link StreamPacket#frames() frames} list.
* @param elements An iterable of frames elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final StreamPacketBuilder addAllFrames(Iterable extends StreamFrame> elements) {
this.frames.addAll(elements);
return this;
}
/**
* Builds a new {@link StreamPacket StreamPacket}.
* @return An immutable instance of StreamPacket
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public StreamPacket build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new StreamPacketBuilder.ImmutableStreamPacket(this);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_INTERLEDGER_PACKET_TYPE) != 0) attributes.add("interledgerPacketType");
if ((initBits & INIT_BIT_SEQUENCE) != 0) attributes.add("sequence");
if ((initBits & INIT_BIT_PREPARE_AMOUNT) != 0) attributes.add("prepareAmount");
return "Cannot build StreamPacket, some of required attributes are not set " + attributes;
}
/**
* Immutable implementation of {@link StreamPacket}.
*
* Use the builder to create immutable instances:
* {@code new StreamPacketBuilder()}.
*/
@Generated(from = "StreamPacket", generator = "Immutables")
@Immutable
@CheckReturnValue
private static final class ImmutableStreamPacket implements StreamPacket {
private final InterledgerPacketType interledgerPacketType;
private final UnsignedLong sequence;
private transient final boolean sequenceIsSafeForSingleSharedSecret;
private final UnsignedLong prepareAmount;
private final ImmutableList frames;
private ImmutableStreamPacket(StreamPacketBuilder builder) {
this.interledgerPacketType = builder.interledgerPacketType;
this.sequence = builder.sequence;
this.prepareAmount = builder.prepareAmount;
this.frames = builder.frames.build();
this.sequenceIsSafeForSingleSharedSecret = StreamPacket.super.sequenceIsSafeForSingleSharedSecret();
}
/**
* @return The value of the {@code interledgerPacketType} attribute
*/
@Override
public InterledgerPacketType interledgerPacketType() {
return interledgerPacketType;
}
/**
* @return The value of the {@code sequence} attribute
*/
@Override
public UnsignedLong sequence() {
return sequence;
}
/**
* @return The computed-at-construction value of the {@code sequenceIsSafeForSingleSharedSecret} attribute
*/
@Override
public boolean sequenceIsSafeForSingleSharedSecret() {
return sequenceIsSafeForSingleSharedSecret;
}
/**
* @return The value of the {@code prepareAmount} attribute
*/
@Override
public UnsignedLong prepareAmount() {
return prepareAmount;
}
/**
* @return The value of the {@code frames} attribute
*/
@Override
public ImmutableList frames() {
return frames;
}
/**
* This instance is equal to all instances of {@code ImmutableStreamPacket} 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 StreamPacketBuilder.ImmutableStreamPacket
&& equalTo((StreamPacketBuilder.ImmutableStreamPacket) another);
}
private boolean equalTo(StreamPacketBuilder.ImmutableStreamPacket another) {
return interledgerPacketType.equals(another.interledgerPacketType)
&& sequence.equals(another.sequence)
&& sequenceIsSafeForSingleSharedSecret == another.sequenceIsSafeForSingleSharedSecret
&& prepareAmount.equals(another.prepareAmount)
&& frames.equals(another.frames);
}
/**
* Computes a hash code from attributes: {@code interledgerPacketType}, {@code sequence}, {@code sequenceIsSafeForSingleSharedSecret}, {@code prepareAmount}, {@code frames}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + interledgerPacketType.hashCode();
h += (h << 5) + sequence.hashCode();
h += (h << 5) + Booleans.hashCode(sequenceIsSafeForSingleSharedSecret);
h += (h << 5) + prepareAmount.hashCode();
h += (h << 5) + frames.hashCode();
return h;
}
/**
* Prints the immutable value {@code StreamPacket} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("StreamPacket")
.omitNullValues()
.add("interledgerPacketType", interledgerPacketType)
.add("sequence", sequence)
.add("sequenceIsSafeForSingleSharedSecret", sequenceIsSafeForSingleSharedSecret)
.add("prepareAmount", prepareAmount)
.add("frames", frames)
.toString();
}
}
}