io.resys.hdes.ast.api.nodes.ImmutableToken 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 HdesNode.Token}.
*
* Use the builder to create immutable instances:
* {@code ImmutableToken.builder()}.
*/
@Generated(from = "HdesNode.Token", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableToken implements HdesNode.Token {
private final String text;
private final HdesNode.Position start;
private final @Nullable HdesNode.Position end;
private ImmutableToken(
String text,
HdesNode.Position start,
@Nullable HdesNode.Position end) {
this.text = text;
this.start = start;
this.end = end;
}
/**
* @return The value of the {@code text} attribute
*/
@Override
public String getText() {
return text;
}
/**
* @return The value of the {@code start} attribute
*/
@Override
public HdesNode.Position getStart() {
return start;
}
/**
* @return The value of the {@code end} attribute
*/
@Override
public @Nullable HdesNode.Position getEnd() {
return end;
}
/**
* Copy the current immutable object by setting a value for the {@link HdesNode.Token#getText() text} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for text
* @return A modified copy of the {@code this} object
*/
public final ImmutableToken withText(String value) {
String newValue = Objects.requireNonNull(value, "text");
if (this.text.equals(newValue)) return this;
return new ImmutableToken(newValue, this.start, this.end);
}
/**
* Copy the current immutable object by setting a value for the {@link HdesNode.Token#getStart() start} 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 start
* @return A modified copy of the {@code this} object
*/
public final ImmutableToken withStart(HdesNode.Position value) {
if (this.start == value) return this;
HdesNode.Position newValue = Objects.requireNonNull(value, "start");
return new ImmutableToken(this.text, newValue, this.end);
}
/**
* Copy the current immutable object by setting a value for the {@link HdesNode.Token#getEnd() end} 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 end (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableToken withEnd(@Nullable HdesNode.Position value) {
if (this.end == value) return this;
return new ImmutableToken(this.text, this.start, value);
}
/**
* This instance is equal to all instances of {@code ImmutableToken} 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 ImmutableToken
&& equalTo((ImmutableToken) another);
}
private boolean equalTo(ImmutableToken another) {
return text.equals(another.text)
&& start.equals(another.start)
&& Objects.equals(end, another.end);
}
/**
* Computes a hash code from attributes: {@code text}, {@code start}, {@code end}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + text.hashCode();
h += (h << 5) + start.hashCode();
h += (h << 5) + Objects.hashCode(end);
return h;
}
/**
* Prints the immutable value {@code Token} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Token{"
+ "text=" + text
+ ", start=" + start
+ ", end=" + end
+ "}";
}
/**
* Creates an immutable copy of a {@link HdesNode.Token} 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 Token instance
*/
public static ImmutableToken copyOf(HdesNode.Token instance) {
if (instance instanceof ImmutableToken) {
return (ImmutableToken) instance;
}
return ImmutableToken.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableToken ImmutableToken}.
*
* ImmutableToken.builder()
* .text(String) // required {@link HdesNode.Token#getText() text}
* .start(io.resys.hdes.ast.api.nodes.HdesNode.Position) // required {@link HdesNode.Token#getStart() start}
* .end(io.resys.hdes.ast.api.nodes.HdesNode.Position | null) // nullable {@link HdesNode.Token#getEnd() end}
* .build();
*
* @return A new ImmutableToken builder
*/
public static ImmutableToken.Builder builder() {
return new ImmutableToken.Builder();
}
/**
* Builds instances of type {@link ImmutableToken ImmutableToken}.
* 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 = "HdesNode.Token", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_TEXT = 0x1L;
private static final long INIT_BIT_START = 0x2L;
private long initBits = 0x3L;
private @Nullable String text;
private @Nullable HdesNode.Position start;
private @Nullable HdesNode.Position end;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Token} 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(HdesNode.Token instance) {
Objects.requireNonNull(instance, "instance");
text(instance.getText());
start(instance.getStart());
@Nullable HdesNode.Position endValue = instance.getEnd();
if (endValue != null) {
end(endValue);
}
return this;
}
/**
* Initializes the value for the {@link HdesNode.Token#getText() text} attribute.
* @param text The value for text
* @return {@code this} builder for use in a chained invocation
*/
public final Builder text(String text) {
this.text = Objects.requireNonNull(text, "text");
initBits &= ~INIT_BIT_TEXT;
return this;
}
/**
* Initializes the value for the {@link HdesNode.Token#getStart() start} attribute.
* @param start The value for start
* @return {@code this} builder for use in a chained invocation
*/
public final Builder start(HdesNode.Position start) {
this.start = Objects.requireNonNull(start, "start");
initBits &= ~INIT_BIT_START;
return this;
}
/**
* Initializes the value for the {@link HdesNode.Token#getEnd() end} attribute.
* @param end The value for end (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
public final Builder end(@Nullable HdesNode.Position end) {
this.end = end;
return this;
}
/**
* Builds a new {@link ImmutableToken ImmutableToken}.
* @return An immutable instance of Token
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableToken build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableToken(text, start, end);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_TEXT) != 0) attributes.add("text");
if ((initBits & INIT_BIT_START) != 0) attributes.add("start");
return "Cannot build Token, some of required attributes are not set " + attributes;
}
}
}