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