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