io.github.cwdesautels.monad.ImmutableLeft 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 Left}.
*
* Use the builder to create immutable instances:
* {@code ImmutableLeft.builder()}.
*/
@Generated(from = "Left", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
final class ImmutableLeft implements Left {
private final @Nullable L left;
private ImmutableLeft(@Nullable L left) {
this.left = left;
}
/**
* @return The value of the {@code left} attribute
*/
@Override
public @Nullable L getLeft() {
return left;
}
/**
* Copy the current immutable object by setting a value for the {@link Left#getLeft() left} 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 left (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableLeft withLeft(@Nullable L value) {
if (this.left == value) return this;
return new ImmutableLeft<>(value);
}
/**
* This instance is equal to all instances of {@code ImmutableLeft} 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 ImmutableLeft, ?>
&& equalTo((ImmutableLeft, ?>) another);
}
private boolean equalTo(ImmutableLeft, ?> another) {
return Objects.equals(left, another.left);
}
/**
* Computes a hash code from attributes: {@code left}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(left);
return h;
}
/**
* Prints the immutable value {@code Left} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Left{"
+ "left=" + left
+ "}";
}
/**
* Creates an immutable copy of a {@link Left} 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 L
* @param generic parameter R
* @param instance The instance to copy
* @return A copied immutable Left instance
*/
public static ImmutableLeft copyOf(Left instance) {
if (instance instanceof ImmutableLeft, ?>) {
return (ImmutableLeft) instance;
}
return ImmutableLeft.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableLeft ImmutableLeft}.
*
* ImmutableLeft.<L, R>builder()
* .left(L | null) // nullable {@link Left#getLeft() left}
* .build();
*
* @param generic parameter L
* @param generic parameter R
* @return A new ImmutableLeft builder
*/
public static ImmutableLeft.Builder builder() {
return new ImmutableLeft.Builder<>();
}
/**
* Builds instances of type {@link ImmutableLeft ImmutableLeft}.
* 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 = "Left", generator = "Immutables")
public static final class Builder {
private L left;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Left} 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(Left instance) {
Objects.requireNonNull(instance, "instance");
@Nullable L leftValue = instance.getLeft();
if (leftValue != null) {
left(leftValue);
}
return this;
}
/**
* Initializes the value for the {@link Left#getLeft() left} attribute.
* @param left The value for left (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
public final Builder left(@Nullable L left) {
this.left = left;
return this;
}
/**
* Builds a new {@link ImmutableLeft ImmutableLeft}.
* @return An immutable instance of Left
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableLeft build() {
return new ImmutableLeft<>(left);
}
}
}