com.neotys.neoload.model.repository.ImmutableCondition Maven / Gradle / Ivy
package com.neotys.neoload.model.repository;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link Condition}.
*
* Use the builder to create immutable instances:
* {@code ImmutableCondition.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "Condition"})
@Deprecated
@Immutable
@CheckReturnValue
public final class ImmutableCondition implements Condition {
private final String operand1;
private final String operand2;
private final Condition.Operator operator;
private ImmutableCondition(
String operand1,
String operand2,
Condition.Operator operator) {
this.operand1 = operand1;
this.operand2 = operand2;
this.operator = operator;
}
/**
* @return The value of the {@code operand1} attribute
*/
@Override
public String getOperand1() {
return operand1;
}
/**
* @return The value of the {@code operand2} attribute
*/
@Override
public String getOperand2() {
return operand2;
}
/**
* @return The value of the {@code operator} attribute
*/
@Override
public Condition.Operator getOperator() {
return operator;
}
/**
* Copy the current immutable object by setting a value for the {@link Condition#getOperand1() operand1} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for operand1
* @return A modified copy of the {@code this} object
*/
public final ImmutableCondition withOperand1(String value) {
if (this.operand1.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "operand1");
return new ImmutableCondition(newValue, this.operand2, this.operator);
}
/**
* Copy the current immutable object by setting a value for the {@link Condition#getOperand2() operand2} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for operand2
* @return A modified copy of the {@code this} object
*/
public final ImmutableCondition withOperand2(String value) {
if (this.operand2.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "operand2");
return new ImmutableCondition(this.operand1, newValue, this.operator);
}
/**
* Copy the current immutable object by setting a value for the {@link Condition#getOperator() operator} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for operator
* @return A modified copy of the {@code this} object
*/
public final ImmutableCondition withOperator(Condition.Operator value) {
if (this.operator == value) return this;
Condition.Operator newValue = Objects.requireNonNull(value, "operator");
return new ImmutableCondition(this.operand1, this.operand2, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableCondition} 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 ImmutableCondition
&& equalTo((ImmutableCondition) another);
}
private boolean equalTo(ImmutableCondition another) {
return operand1.equals(another.operand1)
&& operand2.equals(another.operand2)
&& operator.equals(another.operator);
}
/**
* Computes a hash code from attributes: {@code operand1}, {@code operand2}, {@code operator}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + operand1.hashCode();
h += (h << 5) + operand2.hashCode();
h += (h << 5) + operator.hashCode();
return h;
}
/**
* Prints the immutable value {@code Condition} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Condition")
.omitNullValues()
.add("operand1", operand1)
.add("operand2", operand2)
.add("operator", operator)
.toString();
}
/**
* Creates an immutable copy of a {@link Condition} 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 Condition instance
*/
public static ImmutableCondition copyOf(Condition instance) {
if (instance instanceof ImmutableCondition) {
return (ImmutableCondition) instance;
}
return ImmutableCondition.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableCondition ImmutableCondition}.
* @return A new ImmutableCondition builder
*/
public static ImmutableCondition.Builder builder() {
return new ImmutableCondition.Builder();
}
/**
* Builds instances of type {@link ImmutableCondition ImmutableCondition}.
* 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.
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_OPERAND1 = 0x1L;
private static final long INIT_BIT_OPERAND2 = 0x2L;
private static final long INIT_BIT_OPERATOR = 0x4L;
private long initBits = 0x7L;
private @Nullable String operand1;
private @Nullable String operand2;
private @Nullable Condition.Operator operator;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Condition} 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 Builder from(Condition instance) {
Objects.requireNonNull(instance, "instance");
operand1(instance.getOperand1());
operand2(instance.getOperand2());
operator(instance.getOperator());
return this;
}
/**
* Initializes the value for the {@link Condition#getOperand1() operand1} attribute.
* @param operand1 The value for operand1
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder operand1(String operand1) {
this.operand1 = Objects.requireNonNull(operand1, "operand1");
initBits &= ~INIT_BIT_OPERAND1;
return this;
}
/**
* Initializes the value for the {@link Condition#getOperand2() operand2} attribute.
* @param operand2 The value for operand2
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder operand2(String operand2) {
this.operand2 = Objects.requireNonNull(operand2, "operand2");
initBits &= ~INIT_BIT_OPERAND2;
return this;
}
/**
* Initializes the value for the {@link Condition#getOperator() operator} attribute.
* @param operator The value for operator
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder operator(Condition.Operator operator) {
this.operator = Objects.requireNonNull(operator, "operator");
initBits &= ~INIT_BIT_OPERATOR;
return this;
}
/**
* Builds a new {@link ImmutableCondition ImmutableCondition}.
* @return An immutable instance of Condition
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableCondition build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableCondition(operand1, operand2, operator);
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_OPERAND1) != 0) attributes.add("operand1");
if ((initBits & INIT_BIT_OPERAND2) != 0) attributes.add("operand2");
if ((initBits & INIT_BIT_OPERATOR) != 0) attributes.add("operator");
return "Cannot build Condition, some of required attributes are not set " + attributes;
}
}
}