com.io7m.coffeepick.runtime.parser.spi.SPIProbeFailure 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.Objects;
import java.util.Optional;
/**
* Probing failed.
*/
@SuppressWarnings({"all"})
public final class SPIProbeFailure
implements SPIProbeResultType.SPIProbeFailureType {
private final Exception error;
private SPIProbeFailure(Exception error) {
this.error = error;
}
/**
* @return The exception encountered, if any
*/
@Override
public Optional error() {
return Optional.ofNullable(error);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link SPIProbeResultType.SPIProbeFailureType#error() error} attribute.
* @param value The value for error
* @return A modified copy of {@code this} object
*/
public final SPIProbeFailure withError(Exception value) {
Exception newValue = Objects.requireNonNull(value, "error");
if (this.error == newValue) return this;
return new SPIProbeFailure(newValue);
}
/**
* Copy the current immutable object by setting an optional value for the {@link SPIProbeResultType.SPIProbeFailureType#error() error} 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 error
* @return A modified copy of {@code this} object
*/
@SuppressWarnings("unchecked") // safe covariant cast
public final SPIProbeFailure withError(Optional extends Exception> optional) {
Exception value = optional.orElse(null);
if (this.error == value) return this;
return new SPIProbeFailure(value);
}
/**
* This instance is equal to all instances of {@code SPIProbeFailure} 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 SPIProbeFailure
&& equalTo((SPIProbeFailure) another);
}
private boolean equalTo(SPIProbeFailure another) {
return Objects.equals(error, another.error);
}
/**
* Computes a hash code from attributes: {@code error}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(error);
return h;
}
/**
* Prints the immutable value {@code SPIProbeFailure} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("SPIProbeFailure{");
if (error != null) {
builder.append("error=").append(error);
}
return builder.append("}").toString();
}
/**
* Creates an immutable copy of a {@link SPIProbeResultType.SPIProbeFailureType} 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 SPIProbeFailure instance
*/
public static SPIProbeFailure copyOf(SPIProbeResultType.SPIProbeFailureType instance) {
if (instance instanceof SPIProbeFailure) {
return (SPIProbeFailure) instance;
}
return SPIProbeFailure.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link SPIProbeFailure SPIProbeFailure}.
*
* SPIProbeFailure.builder()
* .setError(Exception) // optional {@link SPIProbeResultType.SPIProbeFailureType#error() error}
* .build();
*
* @return A new SPIProbeFailure builder
*/
public static SPIProbeFailure.Builder builder() {
return new SPIProbeFailure.Builder();
}
/**
* Builds instances of type {@link SPIProbeFailure SPIProbeFailure}.
* 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 Exception error;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code SPIProbeFailureType} 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.SPIProbeFailureType instance) {
Objects.requireNonNull(instance, "instance");
Optional errorOptional = instance.error();
if (errorOptional.isPresent()) {
setError(errorOptional);
}
return this;
}
/**
* Initializes the optional value {@link SPIProbeResultType.SPIProbeFailureType#error() error} to error.
* @param error The value for error
* @return {@code this} builder for chained invocation
*/
public final Builder setError(Exception error) {
this.error = Objects.requireNonNull(error, "error");
return this;
}
/**
* Initializes the optional value {@link SPIProbeResultType.SPIProbeFailureType#error() error} to error.
* @param error The value for error
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setError(Optional extends Exception> error) {
this.error = error.orElse(null);
return this;
}
/**
* Builds a new {@link SPIProbeFailure SPIProbeFailure}.
* @return An immutable instance of SPIProbeFailure
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public SPIProbeFailure build() {
return new SPIProbeFailure(error);
}
}
}