
io.resys.hdes.ast.api.nodes.ImmutableServicePromise Maven / Gradle / Ivy
package io.resys.hdes.ast.api.nodes;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
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 ServiceNode.ServicePromise}.
*
* Use the builder to create immutable instances:
* {@code ImmutableServicePromise.builder()}.
*/
@Generated(from = "ServiceNode.ServicePromise", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableServicePromise implements ServiceNode.ServicePromise {
private final HdesNode.Token token;
private final @Nullable ExpressionNode.ExpressionBody timeout;
private ImmutableServicePromise(
HdesNode.Token token,
@Nullable ExpressionNode.ExpressionBody timeout) {
this.token = token;
this.timeout = timeout;
}
/**
* @return The value of the {@code token} attribute
*/
@Override
public HdesNode.Token getToken() {
return token;
}
/**
* @return The value of the {@code timeout} attribute
*/
@Override
public Optional getTimeout() {
return Optional.ofNullable(timeout);
}
/**
* Copy the current immutable object by setting a value for the {@link ServiceNode.ServicePromise#getToken() token} 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 token
* @return A modified copy of the {@code this} object
*/
public final ImmutableServicePromise withToken(HdesNode.Token value) {
if (this.token == value) return this;
HdesNode.Token newValue = Objects.requireNonNull(value, "token");
return new ImmutableServicePromise(newValue, this.timeout);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link ServiceNode.ServicePromise#getTimeout() timeout} attribute.
* @param value The value for timeout
* @return A modified copy of {@code this} object
*/
public final ImmutableServicePromise withTimeout(ExpressionNode.ExpressionBody value) {
@Nullable ExpressionNode.ExpressionBody newValue = Objects.requireNonNull(value, "timeout");
if (this.timeout == newValue) return this;
return new ImmutableServicePromise(this.token, newValue);
}
/**
* Copy the current immutable object by setting an optional value for the {@link ServiceNode.ServicePromise#getTimeout() timeout} attribute.
* A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}.
* @param optional A value for timeout
* @return A modified copy of {@code this} object
*/
@SuppressWarnings("unchecked") // safe covariant cast
public final ImmutableServicePromise withTimeout(Optional extends ExpressionNode.ExpressionBody> optional) {
@Nullable ExpressionNode.ExpressionBody value = optional.orElse(null);
if (this.timeout == value) return this;
return new ImmutableServicePromise(this.token, value);
}
/**
* This instance is equal to all instances of {@code ImmutableServicePromise} 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 ImmutableServicePromise
&& equalTo((ImmutableServicePromise) another);
}
private boolean equalTo(ImmutableServicePromise another) {
return token.equals(another.token)
&& Objects.equals(timeout, another.timeout);
}
/**
* Computes a hash code from attributes: {@code token}, {@code timeout}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + token.hashCode();
h += (h << 5) + Objects.hashCode(timeout);
return h;
}
/**
* Prints the immutable value {@code ServicePromise} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("ServicePromise{");
builder.append("token=").append(token);
if (timeout != null) {
builder.append(", ");
builder.append("timeout=").append(timeout);
}
return builder.append("}").toString();
}
/**
* Creates an immutable copy of a {@link ServiceNode.ServicePromise} 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 ServicePromise instance
*/
public static ImmutableServicePromise copyOf(ServiceNode.ServicePromise instance) {
if (instance instanceof ImmutableServicePromise) {
return (ImmutableServicePromise) instance;
}
return ImmutableServicePromise.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableServicePromise ImmutableServicePromise}.
*
* ImmutableServicePromise.builder()
* .token(io.resys.hdes.ast.api.nodes.HdesNode.Token) // required {@link ServiceNode.ServicePromise#getToken() token}
* .timeout(io.resys.hdes.ast.api.nodes.ExpressionNode.ExpressionBody) // optional {@link ServiceNode.ServicePromise#getTimeout() timeout}
* .build();
*
* @return A new ImmutableServicePromise builder
*/
public static ImmutableServicePromise.Builder builder() {
return new ImmutableServicePromise.Builder();
}
/**
* Builds instances of type {@link ImmutableServicePromise ImmutableServicePromise}.
* 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 = "ServiceNode.ServicePromise", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_TOKEN = 0x1L;
private long initBits = 0x1L;
private @Nullable HdesNode.Token token;
private @Nullable ExpressionNode.ExpressionBody timeout;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code io.resys.hdes.ast.api.nodes.HdesNode} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(HdesNode instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code io.resys.hdes.ast.api.nodes.ServiceNode.ServicePromise} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(ServiceNode.ServicePromise instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof HdesNode) {
HdesNode instance = (HdesNode) object;
token(instance.getToken());
}
if (object instanceof ServiceNode.ServicePromise) {
ServiceNode.ServicePromise instance = (ServiceNode.ServicePromise) object;
Optional timeoutOptional = instance.getTimeout();
if (timeoutOptional.isPresent()) {
timeout(timeoutOptional);
}
}
}
/**
* Initializes the value for the {@link ServiceNode.ServicePromise#getToken() token} attribute.
* @param token The value for token
* @return {@code this} builder for use in a chained invocation
*/
public final Builder token(HdesNode.Token token) {
this.token = Objects.requireNonNull(token, "token");
initBits &= ~INIT_BIT_TOKEN;
return this;
}
/**
* Initializes the optional value {@link ServiceNode.ServicePromise#getTimeout() timeout} to timeout.
* @param timeout The value for timeout
* @return {@code this} builder for chained invocation
*/
public final Builder timeout(ExpressionNode.ExpressionBody timeout) {
this.timeout = Objects.requireNonNull(timeout, "timeout");
return this;
}
/**
* Initializes the optional value {@link ServiceNode.ServicePromise#getTimeout() timeout} to timeout.
* @param timeout The value for timeout
* @return {@code this} builder for use in a chained invocation
*/
public final Builder timeout(Optional extends ExpressionNode.ExpressionBody> timeout) {
this.timeout = timeout.orElse(null);
return this;
}
/**
* Builds a new {@link ImmutableServicePromise ImmutableServicePromise}.
* @return An immutable instance of ServicePromise
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableServicePromise build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableServicePromise(token, timeout);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_TOKEN) != 0) attributes.add("token");
return "Cannot build ServicePromise, some of required attributes are not set " + attributes;
}
}
}