
io.resys.hdes.ast.api.nodes.ImmutableInstanceMethodExpression Maven / Gradle / Ivy
package io.resys.hdes.ast.api.nodes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
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 ExpressionNode.InstanceMethodExpression}.
*
* Use the builder to create immutable instances:
* {@code ImmutableInstanceMethodExpression.builder()}.
*/
@Generated(from = "ExpressionNode.InstanceMethodExpression", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableInstanceMethodExpression
implements ExpressionNode.InstanceMethodExpression {
private final HdesNode.Token token;
private final InvocationNode.SimpleInvocation name;
private final List values;
private final @Nullable HdesNode next;
private ImmutableInstanceMethodExpression(
HdesNode.Token token,
InvocationNode.SimpleInvocation name,
List values,
@Nullable HdesNode next) {
this.token = token;
this.name = name;
this.values = values;
this.next = next;
}
/**
* @return The value of the {@code token} attribute
*/
@Override
public HdesNode.Token getToken() {
return token;
}
/**
* @return The value of the {@code name} attribute
*/
@Override
public InvocationNode.SimpleInvocation getName() {
return name;
}
/**
* @return The value of the {@code values} attribute
*/
@Override
public List getValues() {
return values;
}
/**
* @return The value of the {@code next} attribute
*/
@Override
public Optional getNext() {
return Optional.ofNullable(next);
}
/**
* Copy the current immutable object by setting a value for the {@link ExpressionNode.InstanceMethodExpression#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 ImmutableInstanceMethodExpression withToken(HdesNode.Token value) {
if (this.token == value) return this;
HdesNode.Token newValue = Objects.requireNonNull(value, "token");
return new ImmutableInstanceMethodExpression(newValue, this.name, this.values, this.next);
}
/**
* Copy the current immutable object by setting a value for the {@link ExpressionNode.InstanceMethodExpression#getName() name} 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 name
* @return A modified copy of the {@code this} object
*/
public final ImmutableInstanceMethodExpression withName(InvocationNode.SimpleInvocation value) {
if (this.name == value) return this;
InvocationNode.SimpleInvocation newValue = Objects.requireNonNull(value, "name");
return new ImmutableInstanceMethodExpression(this.token, newValue, this.values, this.next);
}
/**
* Copy the current immutable object with elements that replace the content of {@link ExpressionNode.InstanceMethodExpression#getValues() values}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableInstanceMethodExpression withValues(HdesNode... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableInstanceMethodExpression(this.token, this.name, newValue, this.next);
}
/**
* Copy the current immutable object with elements that replace the content of {@link ExpressionNode.InstanceMethodExpression#getValues() values}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of values elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableInstanceMethodExpression withValues(Iterable extends HdesNode> elements) {
if (this.values == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableInstanceMethodExpression(this.token, this.name, newValue, this.next);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link ExpressionNode.InstanceMethodExpression#getNext() next} attribute.
* @param value The value for next
* @return A modified copy of {@code this} object
*/
public final ImmutableInstanceMethodExpression withNext(HdesNode value) {
@Nullable HdesNode newValue = Objects.requireNonNull(value, "next");
if (this.next == newValue) return this;
return new ImmutableInstanceMethodExpression(this.token, this.name, this.values, newValue);
}
/**
* Copy the current immutable object by setting an optional value for the {@link ExpressionNode.InstanceMethodExpression#getNext() next} 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 next
* @return A modified copy of {@code this} object
*/
@SuppressWarnings("unchecked") // safe covariant cast
public final ImmutableInstanceMethodExpression withNext(Optional extends HdesNode> optional) {
@Nullable HdesNode value = optional.orElse(null);
if (this.next == value) return this;
return new ImmutableInstanceMethodExpression(this.token, this.name, this.values, value);
}
/**
* This instance is equal to all instances of {@code ImmutableInstanceMethodExpression} 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 ImmutableInstanceMethodExpression
&& equalTo((ImmutableInstanceMethodExpression) another);
}
private boolean equalTo(ImmutableInstanceMethodExpression another) {
return token.equals(another.token)
&& name.equals(another.name)
&& values.equals(another.values)
&& Objects.equals(next, another.next);
}
/**
* Computes a hash code from attributes: {@code token}, {@code name}, {@code values}, {@code next}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + token.hashCode();
h += (h << 5) + name.hashCode();
h += (h << 5) + values.hashCode();
h += (h << 5) + Objects.hashCode(next);
return h;
}
/**
* Prints the immutable value {@code InstanceMethodExpression} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("InstanceMethodExpression{");
builder.append("token=").append(token);
builder.append(", ");
builder.append("name=").append(name);
builder.append(", ");
builder.append("values=").append(values);
if (next != null) {
builder.append(", ");
builder.append("next=").append(next);
}
return builder.append("}").toString();
}
/**
* Creates an immutable copy of a {@link ExpressionNode.InstanceMethodExpression} 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 InstanceMethodExpression instance
*/
public static ImmutableInstanceMethodExpression copyOf(ExpressionNode.InstanceMethodExpression instance) {
if (instance instanceof ImmutableInstanceMethodExpression) {
return (ImmutableInstanceMethodExpression) instance;
}
return ImmutableInstanceMethodExpression.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableInstanceMethodExpression ImmutableInstanceMethodExpression}.
*
* ImmutableInstanceMethodExpression.builder()
* .token(io.resys.hdes.ast.api.nodes.HdesNode.Token) // required {@link ExpressionNode.InstanceMethodExpression#getToken() token}
* .name(io.resys.hdes.ast.api.nodes.InvocationNode.SimpleInvocation) // required {@link ExpressionNode.InstanceMethodExpression#getName() name}
* .addValues|addAllValues(io.resys.hdes.ast.api.nodes.HdesNode) // {@link ExpressionNode.InstanceMethodExpression#getValues() values} elements
* .next(io.resys.hdes.ast.api.nodes.HdesNode) // optional {@link ExpressionNode.InstanceMethodExpression#getNext() next}
* .build();
*
* @return A new ImmutableInstanceMethodExpression builder
*/
public static ImmutableInstanceMethodExpression.Builder builder() {
return new ImmutableInstanceMethodExpression.Builder();
}
/**
* Builds instances of type {@link ImmutableInstanceMethodExpression ImmutableInstanceMethodExpression}.
* 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 = "ExpressionNode.InstanceMethodExpression", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_TOKEN = 0x1L;
private static final long INIT_BIT_NAME = 0x2L;
private long initBits = 0x3L;
private @Nullable HdesNode.Token token;
private @Nullable InvocationNode.SimpleInvocation name;
private List values = new ArrayList();
private @Nullable HdesNode next;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code io.resys.hdes.ast.api.nodes.ExpressionNode.InstanceMethodExpression} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(ExpressionNode.InstanceMethodExpression 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 ExpressionNode.InstanceMethodExpression) {
ExpressionNode.InstanceMethodExpression instance = (ExpressionNode.InstanceMethodExpression) object;
Optional nextOptional = instance.getNext();
if (nextOptional.isPresent()) {
next(nextOptional);
}
name(instance.getName());
addAllValues(instance.getValues());
}
if (object instanceof HdesNode) {
HdesNode instance = (HdesNode) object;
token(instance.getToken());
}
}
/**
* Initializes the value for the {@link ExpressionNode.InstanceMethodExpression#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 ExpressionNode.InstanceMethodExpression#getName() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
public final Builder name(InvocationNode.SimpleInvocation name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Adds one element to {@link ExpressionNode.InstanceMethodExpression#getValues() values} list.
* @param element A values element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addValues(HdesNode element) {
this.values.add(Objects.requireNonNull(element, "values element"));
return this;
}
/**
* Adds elements to {@link ExpressionNode.InstanceMethodExpression#getValues() values} list.
* @param elements An array of values elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addValues(HdesNode... elements) {
for (HdesNode element : elements) {
this.values.add(Objects.requireNonNull(element, "values element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link ExpressionNode.InstanceMethodExpression#getValues() values} list.
* @param elements An iterable of values elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder values(Iterable extends HdesNode> elements) {
this.values.clear();
return addAllValues(elements);
}
/**
* Adds elements to {@link ExpressionNode.InstanceMethodExpression#getValues() values} list.
* @param elements An iterable of values elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllValues(Iterable extends HdesNode> elements) {
for (HdesNode element : elements) {
this.values.add(Objects.requireNonNull(element, "values element"));
}
return this;
}
/**
* Initializes the optional value {@link ExpressionNode.InstanceMethodExpression#getNext() next} to next.
* @param next The value for next
* @return {@code this} builder for chained invocation
*/
public final Builder next(HdesNode next) {
this.next = Objects.requireNonNull(next, "next");
return this;
}
/**
* Initializes the optional value {@link ExpressionNode.InstanceMethodExpression#getNext() next} to next.
* @param next The value for next
* @return {@code this} builder for use in a chained invocation
*/
public final Builder next(Optional extends HdesNode> next) {
this.next = next.orElse(null);
return this;
}
/**
* Builds a new {@link ImmutableInstanceMethodExpression ImmutableInstanceMethodExpression}.
* @return An immutable instance of InstanceMethodExpression
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableInstanceMethodExpression build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableInstanceMethodExpression(token, name, createUnmodifiableList(true, values), next);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_TOKEN) != 0) attributes.add("token");
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
return "Cannot build InstanceMethodExpression, some of required attributes are not set " + attributes;
}
}
private static List createSafeList(Iterable extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList list;
if (iterable instanceof Collection>) {
int size = ((Collection>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList<>();
} else {
list = new ArrayList<>();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) Objects.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static List createUnmodifiableList(boolean clone, List list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList<>(list));
} else {
if (list instanceof ArrayList>) {
((ArrayList>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
}