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