dz.jtsgen.processor.dsl.parser.TokenBuilder Maven / Gradle / Ivy
Show all versions of jtsgen-processor Show documentation
package dz.jtsgen.processor.dsl.parser;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Generated;
/**
* Immutable implementation of {@link Token}.
*
* Use the builder to create immutable instances:
* {@code TokenBuilder.builder()}.
* Use the static factory method to create immutable instances:
* {@code TokenBuilder.of()}.
*/
@SuppressWarnings({"all"})
@Generated({"Immutables.generator", "Token"})
final class TokenBuilder extends Token {
private final TokenType type;
private final String data;
private final Integer index;
private TokenBuilder(TokenType type, String data, Integer index) {
this.type = Objects.requireNonNull(type, "type");
this.data = Objects.requireNonNull(data, "data");
this.index = Objects.requireNonNull(index, "index");
}
private TokenBuilder(
TokenBuilder original,
TokenType type,
String data,
Integer index) {
this.type = type;
this.data = data;
this.index = index;
}
/**
* @return The value of the {@code type} attribute
*/
@Override
TokenType type() {
return type;
}
/**
* @return The value of the {@code data} attribute
*/
@Override
String data() {
return data;
}
/**
* @return The value of the {@code index} attribute
*/
@Override
Integer index() {
return index;
}
/**
* Copy the current immutable object by setting a value for the {@link Token#type() 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 TokenBuilder withType(TokenType value) {
if (this.type == value) return this;
TokenType newValue = Objects.requireNonNull(value, "type");
return new TokenBuilder(this, newValue, this.data, this.index);
}
/**
* Copy the current immutable object by setting a value for the {@link Token#data() data} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for data
* @return A modified copy of the {@code this} object
*/
public final TokenBuilder withData(String value) {
if (this.data.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "data");
return new TokenBuilder(this, this.type, newValue, this.index);
}
/**
* Copy the current immutable object by setting a value for the {@link Token#index() index} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for index
* @return A modified copy of the {@code this} object
*/
public final TokenBuilder withIndex(Integer value) {
if (this.index.equals(value)) return this;
Integer newValue = Objects.requireNonNull(value, "index");
return new TokenBuilder(this, this.type, this.data, newValue);
}
/**
* This instance is equal to all instances of {@code TokenBuilder} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof TokenBuilder
&& equalTo((TokenBuilder) another);
}
private boolean equalTo(TokenBuilder another) {
return type.equals(another.type)
&& data.equals(another.data)
&& index.equals(another.index);
}
/**
* Computes a hash code from attributes: {@code type}, {@code data}, {@code index}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + type.hashCode();
h += (h << 5) + data.hashCode();
h += (h << 5) + index.hashCode();
return h;
}
/**
* Prints the immutable value {@code Token} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Token{"
+ "type=" + type
+ ", data=" + data
+ ", index=" + index
+ "}";
}
/**
* Construct a new immutable {@code Token} instance.
* @param type The value for the {@code type} attribute
* @param data The value for the {@code data} attribute
* @param index The value for the {@code index} attribute
* @return An immutable Token instance
*/
public static TokenBuilder of(TokenType type, String data, Integer index) {
return new TokenBuilder(type, data, index);
}
/**
* Creates an immutable copy of a {@link 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 TokenBuilder copyOf(Token instance) {
if (instance instanceof TokenBuilder) {
return (TokenBuilder) instance;
}
return TokenBuilder.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link TokenBuilder TokenBuilder}.
* @return A new TokenBuilder builder
*/
public static TokenBuilder.Builder builder() {
return new TokenBuilder.Builder();
}
/**
* Builds instances of type {@link TokenBuilder TokenBuilder}.
* 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.
*/
public static final class Builder {
private static final long INIT_BIT_TYPE = 0x1L;
private static final long INIT_BIT_DATA = 0x2L;
private static final long INIT_BIT_INDEX = 0x4L;
private long initBits = 0x7L;
private TokenType type;
private String data;
private Integer index;
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(Token instance) {
Objects.requireNonNull(instance, "instance");
type(instance.type());
data(instance.data());
index(instance.index());
return this;
}
/**
* Initializes the value for the {@link Token#type() type} attribute.
* @param type The value for type
* @return {@code this} builder for use in a chained invocation
*/
public final Builder type(TokenType type) {
this.type = Objects.requireNonNull(type, "type");
initBits &= ~INIT_BIT_TYPE;
return this;
}
/**
* Initializes the value for the {@link Token#data() data} attribute.
* @param data The value for data
* @return {@code this} builder for use in a chained invocation
*/
public final Builder data(String data) {
this.data = Objects.requireNonNull(data, "data");
initBits &= ~INIT_BIT_DATA;
return this;
}
/**
* Initializes the value for the {@link Token#index() index} attribute.
* @param index The value for index
* @return {@code this} builder for use in a chained invocation
*/
public final Builder index(Integer index) {
this.index = Objects.requireNonNull(index, "index");
initBits &= ~INIT_BIT_INDEX;
return this;
}
/**
* Builds a new {@link TokenBuilder TokenBuilder}.
* @return An immutable instance of Token
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public TokenBuilder build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new TokenBuilder(null, type, data, index);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type");
if ((initBits & INIT_BIT_DATA) != 0) attributes.add("data");
if ((initBits & INIT_BIT_INDEX) != 0) attributes.add("index");
return "Cannot build Token, some of required attributes are not set " + attributes;
}
}
}