
net.yudichev.jiotty.common.async.backoff.BackOffConfig Maven / Gradle / Ivy
package net.yudichev.jiotty.common.async.backoff;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Doubles;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.time.Duration;
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;
/**
* Immutable implementation of {@link BackOffProvider.BaseBackOffConfig}.
*
* Use the builder to create immutable instances:
* {@code BackOffConfig.builder()}.
*/
@Generated(from = "BackOffProvider.BaseBackOffConfig", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class BackOffConfig
implements BackOffProvider.BaseBackOffConfig {
private final Duration maxInterval;
private final Duration initialInterval;
private final Duration maxElapsedTime;
private final double multiplier;
private final double randomizationFactor;
private BackOffConfig(BackOffConfig.Builder builder) {
if (builder.maxInterval != null) {
initShim.setMaxInterval(builder.maxInterval);
}
if (builder.initialInterval != null) {
initShim.setInitialInterval(builder.initialInterval);
}
if (builder.maxElapsedTime != null) {
initShim.setMaxElapsedTime(builder.maxElapsedTime);
}
if (builder.multiplierIsSet()) {
initShim.setMultiplier(builder.multiplier);
}
if (builder.randomizationFactorIsSet()) {
initShim.setRandomizationFactor(builder.randomizationFactor);
}
this.maxInterval = initShim.maxInterval();
this.initialInterval = initShim.initialInterval();
this.maxElapsedTime = initShim.maxElapsedTime();
this.multiplier = initShim.multiplier();
this.randomizationFactor = initShim.randomizationFactor();
this.initShim = null;
}
private BackOffConfig(
Duration maxInterval,
Duration initialInterval,
Duration maxElapsedTime,
double multiplier,
double randomizationFactor) {
this.maxInterval = maxInterval;
this.initialInterval = initialInterval;
this.maxElapsedTime = maxElapsedTime;
this.multiplier = multiplier;
this.randomizationFactor = randomizationFactor;
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 = "BackOffProvider.BaseBackOffConfig", generator = "Immutables")
private final class InitShim {
private byte maxIntervalBuildStage = STAGE_UNINITIALIZED;
private Duration maxInterval;
Duration maxInterval() {
if (maxIntervalBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (maxIntervalBuildStage == STAGE_UNINITIALIZED) {
maxIntervalBuildStage = STAGE_INITIALIZING;
this.maxInterval = Objects.requireNonNull(maxIntervalInitialize(), "maxInterval");
maxIntervalBuildStage = STAGE_INITIALIZED;
}
return this.maxInterval;
}
void setMaxInterval(Duration maxInterval) {
this.maxInterval = maxInterval;
maxIntervalBuildStage = STAGE_INITIALIZED;
}
private byte initialIntervalBuildStage = STAGE_UNINITIALIZED;
private Duration initialInterval;
Duration initialInterval() {
if (initialIntervalBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (initialIntervalBuildStage == STAGE_UNINITIALIZED) {
initialIntervalBuildStage = STAGE_INITIALIZING;
this.initialInterval = Objects.requireNonNull(initialIntervalInitialize(), "initialInterval");
initialIntervalBuildStage = STAGE_INITIALIZED;
}
return this.initialInterval;
}
void setInitialInterval(Duration initialInterval) {
this.initialInterval = initialInterval;
initialIntervalBuildStage = STAGE_INITIALIZED;
}
private byte maxElapsedTimeBuildStage = STAGE_UNINITIALIZED;
private Duration maxElapsedTime;
Duration maxElapsedTime() {
if (maxElapsedTimeBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (maxElapsedTimeBuildStage == STAGE_UNINITIALIZED) {
maxElapsedTimeBuildStage = STAGE_INITIALIZING;
this.maxElapsedTime = Objects.requireNonNull(maxElapsedTimeInitialize(), "maxElapsedTime");
maxElapsedTimeBuildStage = STAGE_INITIALIZED;
}
return this.maxElapsedTime;
}
void setMaxElapsedTime(Duration maxElapsedTime) {
this.maxElapsedTime = maxElapsedTime;
maxElapsedTimeBuildStage = STAGE_INITIALIZED;
}
private byte multiplierBuildStage = STAGE_UNINITIALIZED;
private double multiplier;
double multiplier() {
if (multiplierBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (multiplierBuildStage == STAGE_UNINITIALIZED) {
multiplierBuildStage = STAGE_INITIALIZING;
this.multiplier = multiplierInitialize();
multiplierBuildStage = STAGE_INITIALIZED;
}
return this.multiplier;
}
void setMultiplier(double multiplier) {
this.multiplier = multiplier;
multiplierBuildStage = STAGE_INITIALIZED;
}
private byte randomizationFactorBuildStage = STAGE_UNINITIALIZED;
private double randomizationFactor;
double randomizationFactor() {
if (randomizationFactorBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (randomizationFactorBuildStage == STAGE_UNINITIALIZED) {
randomizationFactorBuildStage = STAGE_INITIALIZING;
this.randomizationFactor = randomizationFactorInitialize();
randomizationFactorBuildStage = STAGE_INITIALIZED;
}
return this.randomizationFactor;
}
void setRandomizationFactor(double randomizationFactor) {
this.randomizationFactor = randomizationFactor;
randomizationFactorBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (maxIntervalBuildStage == STAGE_INITIALIZING) attributes.add("maxInterval");
if (initialIntervalBuildStage == STAGE_INITIALIZING) attributes.add("initialInterval");
if (maxElapsedTimeBuildStage == STAGE_INITIALIZING) attributes.add("maxElapsedTime");
if (multiplierBuildStage == STAGE_INITIALIZING) attributes.add("multiplier");
if (randomizationFactorBuildStage == STAGE_INITIALIZING) attributes.add("randomizationFactor");
return "Cannot build BackOffConfig, attribute initializers form cycle " + attributes;
}
}
private Duration maxIntervalInitialize() {
return BackOffProvider.BaseBackOffConfig.super.maxInterval();
}
private Duration initialIntervalInitialize() {
return BackOffProvider.BaseBackOffConfig.super.initialInterval();
}
private Duration maxElapsedTimeInitialize() {
return BackOffProvider.BaseBackOffConfig.super.maxElapsedTime();
}
private double multiplierInitialize() {
return BackOffProvider.BaseBackOffConfig.super.multiplier();
}
private double randomizationFactorInitialize() {
return BackOffProvider.BaseBackOffConfig.super.randomizationFactor();
}
/**
* @return The value of the {@code maxInterval} attribute
*/
@Override
public Duration maxInterval() {
InitShim shim = this.initShim;
return shim != null
? shim.maxInterval()
: this.maxInterval;
}
/**
* @return The value of the {@code initialInterval} attribute
*/
@Override
public Duration initialInterval() {
InitShim shim = this.initShim;
return shim != null
? shim.initialInterval()
: this.initialInterval;
}
/**
* @return The value of the {@code maxElapsedTime} attribute
*/
@Override
public Duration maxElapsedTime() {
InitShim shim = this.initShim;
return shim != null
? shim.maxElapsedTime()
: this.maxElapsedTime;
}
/**
* @return The value of the {@code multiplier} attribute
*/
@Override
public double multiplier() {
InitShim shim = this.initShim;
return shim != null
? shim.multiplier()
: this.multiplier;
}
/**
* @return The value of the {@code randomizationFactor} attribute
*/
@Override
public double randomizationFactor() {
InitShim shim = this.initShim;
return shim != null
? shim.randomizationFactor()
: this.randomizationFactor;
}
/**
* Copy the current immutable object by setting a value for the {@link BackOffConfig#maxInterval() maxInterval} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for maxInterval
* @return A modified copy of the {@code this} object
*/
public final BackOffConfig withMaxInterval(Duration value) {
if (this.maxInterval == value) return this;
Duration newValue = Objects.requireNonNull(value, "maxInterval");
return new BackOffConfig(newValue, this.initialInterval, this.maxElapsedTime, this.multiplier, this.randomizationFactor);
}
/**
* Copy the current immutable object by setting a value for the {@link BackOffConfig#initialInterval() initialInterval} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for initialInterval
* @return A modified copy of the {@code this} object
*/
public final BackOffConfig withInitialInterval(Duration value) {
if (this.initialInterval == value) return this;
Duration newValue = Objects.requireNonNull(value, "initialInterval");
return new BackOffConfig(this.maxInterval, newValue, this.maxElapsedTime, this.multiplier, this.randomizationFactor);
}
/**
* Copy the current immutable object by setting a value for the {@link BackOffConfig#maxElapsedTime() maxElapsedTime} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for maxElapsedTime
* @return A modified copy of the {@code this} object
*/
public final BackOffConfig withMaxElapsedTime(Duration value) {
if (this.maxElapsedTime == value) return this;
Duration newValue = Objects.requireNonNull(value, "maxElapsedTime");
return new BackOffConfig(this.maxInterval, this.initialInterval, newValue, this.multiplier, this.randomizationFactor);
}
/**
* Copy the current immutable object by setting a value for the {@link BackOffConfig#multiplier() multiplier} attribute.
* A value strict bits equality used to prevent copying of the same value by returning {@code this}.
* @param value A new value for multiplier
* @return A modified copy of the {@code this} object
*/
public final BackOffConfig withMultiplier(double value) {
if (Double.doubleToLongBits(this.multiplier) == Double.doubleToLongBits(value)) return this;
return new BackOffConfig(this.maxInterval, this.initialInterval, this.maxElapsedTime, value, this.randomizationFactor);
}
/**
* Copy the current immutable object by setting a value for the {@link BackOffConfig#randomizationFactor() randomizationFactor} attribute.
* A value strict bits equality used to prevent copying of the same value by returning {@code this}.
* @param value A new value for randomizationFactor
* @return A modified copy of the {@code this} object
*/
public final BackOffConfig withRandomizationFactor(double value) {
if (Double.doubleToLongBits(this.randomizationFactor) == Double.doubleToLongBits(value)) return this;
return new BackOffConfig(this.maxInterval, this.initialInterval, this.maxElapsedTime, this.multiplier, value);
}
/**
* This instance is equal to all instances of {@code BackOffConfig} 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 BackOffConfig
&& equalTo((BackOffConfig) another);
}
private boolean equalTo(BackOffConfig another) {
return maxInterval.equals(another.maxInterval)
&& initialInterval.equals(another.initialInterval)
&& maxElapsedTime.equals(another.maxElapsedTime)
&& Double.doubleToLongBits(multiplier) == Double.doubleToLongBits(another.multiplier)
&& Double.doubleToLongBits(randomizationFactor) == Double.doubleToLongBits(another.randomizationFactor);
}
/**
* Computes a hash code from attributes: {@code maxInterval}, {@code initialInterval}, {@code maxElapsedTime}, {@code multiplier}, {@code randomizationFactor}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + maxInterval.hashCode();
h += (h << 5) + initialInterval.hashCode();
h += (h << 5) + maxElapsedTime.hashCode();
h += (h << 5) + Doubles.hashCode(multiplier);
h += (h << 5) + Doubles.hashCode(randomizationFactor);
return h;
}
/**
* Prints the immutable value {@code BackOffConfig} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("BackOffConfig")
.omitNullValues()
.add("maxInterval", maxInterval)
.add("initialInterval", initialInterval)
.add("maxElapsedTime", maxElapsedTime)
.add("multiplier", multiplier)
.add("randomizationFactor", randomizationFactor)
.toString();
}
/**
* Creates an immutable copy of a {@link BackOffProvider.BaseBackOffConfig} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance The instance to copy
* @return A copied immutable BackOffConfig instance
*/
static BackOffConfig copyOf(BackOffProvider.BaseBackOffConfig instance) {
if (instance instanceof BackOffConfig) {
return (BackOffConfig) instance;
}
return BackOffConfig.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link BackOffConfig BackOffConfig}.
*
* BackOffConfig.builder()
* .setMaxInterval(java.time.Duration) // optional {@link BackOffConfig#maxInterval() maxInterval}
* .setInitialInterval(java.time.Duration) // optional {@link BackOffConfig#initialInterval() initialInterval}
* .setMaxElapsedTime(java.time.Duration) // optional {@link BackOffConfig#maxElapsedTime() maxElapsedTime}
* .setMultiplier(double) // optional {@link BackOffConfig#multiplier() multiplier}
* .setRandomizationFactor(double) // optional {@link BackOffConfig#randomizationFactor() randomizationFactor}
* .build();
*
* @return A new BackOffConfig builder
*/
public static BackOffConfig.Builder builder() {
return new BackOffConfig.Builder();
}
/**
* Builds instances of type {@link BackOffConfig BackOffConfig}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "BackOffProvider.BaseBackOffConfig", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long OPT_BIT_MULTIPLIER = 0x1L;
private static final long OPT_BIT_RANDOMIZATION_FACTOR = 0x2L;
private long optBits;
private @Nullable Duration maxInterval;
private @Nullable Duration initialInterval;
private @Nullable Duration maxElapsedTime;
private double multiplier;
private double randomizationFactor;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code BackOffConfig} 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
*/
public final Builder from(BackOffConfig instance) {
return from((BackOffProvider.BaseBackOffConfig) instance);
}
/**
* Copy abstract value type {@code BaseBackOffConfig} instance into builder.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
final Builder from(BackOffProvider.BaseBackOffConfig instance) {
Objects.requireNonNull(instance, "instance");
setMaxInterval(instance.maxInterval());
setInitialInterval(instance.initialInterval());
setMaxElapsedTime(instance.maxElapsedTime());
setMultiplier(instance.multiplier());
setRandomizationFactor(instance.randomizationFactor());
return this;
}
/**
* Initializes the value for the {@link BackOffConfig#maxInterval() maxInterval} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BackOffConfig#maxInterval() maxInterval}.
* @param maxInterval The value for maxInterval
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setMaxInterval(Duration maxInterval) {
this.maxInterval = Objects.requireNonNull(maxInterval, "maxInterval");
return this;
}
/**
* Initializes the value for the {@link BackOffConfig#initialInterval() initialInterval} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BackOffConfig#initialInterval() initialInterval}.
* @param initialInterval The value for initialInterval
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setInitialInterval(Duration initialInterval) {
this.initialInterval = Objects.requireNonNull(initialInterval, "initialInterval");
return this;
}
/**
* Initializes the value for the {@link BackOffConfig#maxElapsedTime() maxElapsedTime} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BackOffConfig#maxElapsedTime() maxElapsedTime}.
* @param maxElapsedTime The value for maxElapsedTime
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setMaxElapsedTime(Duration maxElapsedTime) {
this.maxElapsedTime = Objects.requireNonNull(maxElapsedTime, "maxElapsedTime");
return this;
}
/**
* Initializes the value for the {@link BackOffConfig#multiplier() multiplier} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BackOffConfig#multiplier() multiplier}.
* @param multiplier The value for multiplier
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setMultiplier(double multiplier) {
this.multiplier = multiplier;
optBits |= OPT_BIT_MULTIPLIER;
return this;
}
/**
* Initializes the value for the {@link BackOffConfig#randomizationFactor() randomizationFactor} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link BackOffConfig#randomizationFactor() randomizationFactor}.
* @param randomizationFactor The value for randomizationFactor
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder setRandomizationFactor(double randomizationFactor) {
this.randomizationFactor = randomizationFactor;
optBits |= OPT_BIT_RANDOMIZATION_FACTOR;
return this;
}
/**
* Builds a new {@link BackOffConfig BackOffConfig}.
* @return An immutable instance of BackOffConfig
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public BackOffConfig build() {
return new BackOffConfig(this);
}
private boolean multiplierIsSet() {
return (optBits & OPT_BIT_MULTIPLIER) != 0;
}
private boolean randomizationFactorIsSet() {
return (optBits & OPT_BIT_RANDOMIZATION_FACTOR) != 0;
}
}
}