io.github.cwdesautels.monad.ImmutableFailure Maven / Gradle / Ivy
package io.github.cwdesautels.monad;
import io.github.cwdesautels.annotation.Nullable;
import java.util.Objects;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link Failure}.
*
* Use the builder to create immutable instances:
* {@code ImmutableFailure.builder()}.
*/
@Generated(from = "Failure", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
final class ImmutableFailure implements Failure {
private final @Nullable Throwable cause;
private ImmutableFailure(@Nullable Throwable cause) {
this.cause = cause;
}
/**
* @return The value of the {@code cause} attribute
*/
@Override
public @Nullable Throwable getCause() {
return cause;
}
/**
* Copy the current immutable object by setting a value for the {@link Failure#getCause() cause} 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 cause (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableFailure withCause(@Nullable Throwable value) {
if (this.cause == value) return this;
return new ImmutableFailure<>(value);
}
/**
* This instance is equal to all instances of {@code ImmutableFailure} 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 ImmutableFailure>
&& equalTo((ImmutableFailure>) another);
}
private boolean equalTo(ImmutableFailure> another) {
return Objects.equals(cause, another.cause);
}
/**
* Computes a hash code from attributes: {@code cause}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(cause);
return h;
}
/**
* Prints the immutable value {@code Failure} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Failure{"
+ "cause=" + cause
+ "}";
}
/**
* Creates an immutable copy of a {@link Failure} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param generic parameter T
* @param instance The instance to copy
* @return A copied immutable Failure instance
*/
public static ImmutableFailure copyOf(Failure instance) {
if (instance instanceof ImmutableFailure>) {
return (ImmutableFailure) instance;
}
return ImmutableFailure.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableFailure ImmutableFailure}.
*
* ImmutableFailure.<T>builder()
* .cause(Throwable | null) // nullable {@link Failure#getCause() cause}
* .build();
*
* @param generic parameter T
* @return A new ImmutableFailure builder
*/
public static ImmutableFailure.Builder builder() {
return new ImmutableFailure.Builder<>();
}
/**
* Builds instances of type {@link ImmutableFailure ImmutableFailure}.
* 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 = "Failure", generator = "Immutables")
public static final class Builder {
private Throwable cause;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code io.github.cwdesautels.monad.Try} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(Try instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code io.github.cwdesautels.monad.Failure} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(Failure instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
@SuppressWarnings("unchecked")
private void from(Object object) {
long bits = 0;
if (object instanceof Try>) {
Try instance = (Try) object;
if ((bits & 0x1L) == 0) {
@Nullable Throwable causeValue = instance.getCause();
if (causeValue != null) {
cause(causeValue);
}
bits |= 0x1L;
}
}
if (object instanceof Failure>) {
Failure instance = (Failure) object;
if ((bits & 0x1L) == 0) {
@Nullable Throwable causeValue = instance.getCause();
if (causeValue != null) {
cause(causeValue);
}
bits |= 0x1L;
}
}
}
/**
* Initializes the value for the {@link Failure#getCause() cause} attribute.
* @param cause The value for cause (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
public final Builder cause(@Nullable Throwable cause) {
this.cause = cause;
return this;
}
/**
* Builds a new {@link ImmutableFailure ImmutableFailure}.
* @return An immutable instance of Failure
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableFailure build() {
return new ImmutableFailure<>(cause);
}
}
}