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