com.io7m.coffeepick.runtime.parser.spi.ParsedRuntime Maven / Gradle / Ivy
Show all versions of com.io7m.coffeepick.runtime.parser.spi Show documentation
package com.io7m.coffeepick.runtime.parser.spi;
import com.io7m.coffeepick.runtime.RuntimeDescription;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* A parsed runtime
*/
@SuppressWarnings({"all"})
public final class ParsedRuntime
implements ParserResultType.ParsedRuntimeType {
private final RuntimeDescription runtime;
private ParsedRuntime(RuntimeDescription runtime) {
this.runtime = Objects.requireNonNull(runtime, "runtime");
}
private ParsedRuntime(ParsedRuntime original, RuntimeDescription runtime) {
this.runtime = runtime;
}
/**
* @return The actual runtime value
*/
@Override
public RuntimeDescription runtime() {
return runtime;
}
/**
* Copy the current immutable object by setting a value for the {@link ParserResultType.ParsedRuntimeType#runtime() runtime} 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 runtime
* @return A modified copy of the {@code this} object
*/
public final ParsedRuntime withRuntime(RuntimeDescription value) {
if (this.runtime == value) return this;
RuntimeDescription newValue = Objects.requireNonNull(value, "runtime");
return new ParsedRuntime(this, newValue);
}
/**
* This instance is equal to all instances of {@code ParsedRuntime} 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 ParsedRuntime
&& equalTo((ParsedRuntime) another);
}
private boolean equalTo(ParsedRuntime another) {
return runtime.equals(another.runtime);
}
/**
* Computes a hash code from attributes: {@code runtime}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + runtime.hashCode();
return h;
}
/**
* Prints the immutable value {@code ParsedRuntime} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "ParsedRuntime{"
+ "runtime=" + runtime
+ "}";
}
/**
* Construct a new immutable {@code ParsedRuntime} instance.
* @param runtime The value for the {@code runtime} attribute
* @return An immutable ParsedRuntime instance
*/
public static ParsedRuntime of(RuntimeDescription runtime) {
return new ParsedRuntime(runtime);
}
/**
* Creates an immutable copy of a {@link ParserResultType.ParsedRuntimeType} 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 ParsedRuntime instance
*/
public static ParsedRuntime copyOf(ParserResultType.ParsedRuntimeType instance) {
if (instance instanceof ParsedRuntime) {
return (ParsedRuntime) instance;
}
return ParsedRuntime.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ParsedRuntime ParsedRuntime}.
*
* ParsedRuntime.builder()
* .setRuntime(com.io7m.coffeepick.runtime.RuntimeDescription) // required {@link ParserResultType.ParsedRuntimeType#runtime() runtime}
* .build();
*
* @return A new ParsedRuntime builder
*/
public static ParsedRuntime.Builder builder() {
return new ParsedRuntime.Builder();
}
/**
* Builds instances of type {@link ParsedRuntime ParsedRuntime}.
* 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_RUNTIME = 0x1L;
private long initBits = 0x1L;
private RuntimeDescription runtime;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code ParsedRuntimeType} 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(ParserResultType.ParsedRuntimeType instance) {
Objects.requireNonNull(instance, "instance");
setRuntime(instance.runtime());
return this;
}
/**
* Initializes the value for the {@link ParserResultType.ParsedRuntimeType#runtime() runtime} attribute.
* @param runtime The value for runtime
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setRuntime(RuntimeDescription runtime) {
this.runtime = Objects.requireNonNull(runtime, "runtime");
initBits &= ~INIT_BIT_RUNTIME;
return this;
}
/**
* Builds a new {@link ParsedRuntime ParsedRuntime}.
* @return An immutable instance of ParsedRuntime
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ParsedRuntime build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ParsedRuntime(null, runtime);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_RUNTIME) != 0) attributes.add("runtime");
return "Cannot build ParsedRuntime, some of required attributes are not set " + attributes;
}
}
}