org.immutables.fixture.ImmutableHasNullable Maven / Gradle / Ivy
package org.immutables.fixture;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link HasNullable}.
*
* Use the builder to create immutable instances:
* {@code ImmutableHasNullable.builder()}.
* Use the static factory method to create immutable instances:
* {@code ImmutableHasNullable.of()}.
* Use the static factory method to get the default singleton instance:
* {@code ImmutableHasNullable.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "HasNullable"})
@Immutable
public final class ImmutableHasNullable extends HasNullable {
private final @Nullable Integer in;
private final @Nullable String def;
private final @Nullable String der;
private ImmutableHasNullable() {
this.in = null;
this.def = initShim.def();
this.der = initShim.der();
this.initShim = null;
}
private ImmutableHasNullable(@Nullable Integer in) {
this.in = in;
this.def = initShim.def();
this.der = initShim.der();
this.initShim = null;
}
private ImmutableHasNullable(ImmutableHasNullable.Builder builder) {
this.in = builder.in;
if (builder.defIsSet()) {
initShim.def(builder.def);
}
this.def = initShim.def();
this.der = initShim.der();
this.initShim = null;
}
private ImmutableHasNullable(@Nullable Integer in, @Nullable String def) {
this.in = in;
this.def = def;
initShim.def(this.def);
this.der = initShim.der();
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private String def;
private int defStage;
String def() {
if (defStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (defStage == STAGE_UNINITIALIZED) {
defStage = STAGE_INITIALIZING;
this.def = ImmutableHasNullable.super.def();
defStage = STAGE_INITIALIZED;
}
return this.def;
}
void def(String def) {
this.def = def;
defStage = STAGE_INITIALIZED;
}
private String der;
private int derStage;
String der() {
if (derStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (derStage == STAGE_UNINITIALIZED) {
derStage = STAGE_INITIALIZING;
this.der = ImmutableHasNullable.super.der();
derStage = STAGE_INITIALIZED;
}
return this.der;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();
if (defStage == STAGE_INITIALIZING) attributes.add("def");
if (derStage == STAGE_INITIALIZING) attributes.add("der");
return "Cannot build HasNullable, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code in} attribute
*/
@Override
public @Nullable Integer in() {
return in;
}
/**
* @return The value of the {@code def} attribute
*/
@Override
public @Nullable String def() {
InitShim shim = this.initShim;
return shim != null
? shim.def()
: this.def;
}
/**
* @return The computed-at-construction value of the {@code der} attribute
*/
@Override
public @Nullable String der() {
InitShim shim = this.initShim;
return shim != null
? shim.der()
: this.der;
}
/**
* Copy the current immutable object by setting a value for the {@link HasNullable#in() in} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param in A new value for in (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableHasNullable withIn(@Nullable Integer in) {
if (Objects.equal(this.in, in)) return this;
return validate(new ImmutableHasNullable(in, this.def));
}
/**
* Copy the current immutable object by setting a value for the {@link HasNullable#def() def} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param def A new value for def (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableHasNullable withDef(@Nullable String def) {
if (Objects.equal(this.def, def)) return this;
return validate(new ImmutableHasNullable(this.in, def));
}
/**
* This instance is equal to all instances of {@code ImmutableHasNullable} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableHasNullable
&& equalTo((ImmutableHasNullable) another);
}
private boolean equalTo(ImmutableHasNullable another) {
return Objects.equal(in, another.in)
&& Objects.equal(def, another.def)
&& Objects.equal(der, another.der);
}
/**
* Computes a hash code from attributes: {@code in}, {@code def}, {@code der}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + Objects.hashCode(in);
h = h * 17 + Objects.hashCode(def);
h = h * 17 + Objects.hashCode(der);
return h;
}
/**
* Prints the immutable value {@code HasNullable} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("HasNullable")
.omitNullValues()
.add("in", in)
.add("def", def)
.add("der", der)
.toString();
}
private static final ImmutableHasNullable INSTANCE = validate(new ImmutableHasNullable());
/**
* Returns the default immutable singleton value of {@code HasNullable}
* @return An immutable instance of HasNullable
*/
public static ImmutableHasNullable of() {
return INSTANCE;
}
/**
* Construct a new immutable {@code HasNullable} instance.
* @param in The value for the {@code in} attribute
* @return An immutable HasNullable instance
*/
public static ImmutableHasNullable of(@Nullable Integer in) {
return validate(new ImmutableHasNullable(in));
}
private static ImmutableHasNullable validate(ImmutableHasNullable instance) {
return INSTANCE != null && INSTANCE.equalTo(instance) ? INSTANCE : instance;
}
/**
* Creates an immutable copy of a {@link HasNullable} 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 HasNullable instance
*/
public static ImmutableHasNullable copyOf(HasNullable instance) {
if (instance instanceof ImmutableHasNullable) {
return (ImmutableHasNullable) instance;
}
return ImmutableHasNullable.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableHasNullable ImmutableHasNullable}.
* @return A new ImmutableHasNullable builder
*/
public static ImmutableHasNullable.Builder builder() {
return new ImmutableHasNullable.Builder();
}
/**
* Builds instances of type {@link ImmutableHasNullable ImmutableHasNullable}.
* 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.
*/
@NotThreadSafe
public static final class Builder {
private static final long OPT_BIT_DEF = 0x1L;
private long optBits;
private @Nullable Integer in;
private @Nullable String def;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code HasNullable} 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(HasNullable instance) {
Preconditions.checkNotNull(instance, "instance");
@Nullable Integer inValue = instance.in();
if (inValue != null) {
in(inValue);
}
@Nullable String defValue = instance.def();
if (defValue != null) {
def(defValue);
}
return this;
}
/**
* Initializes the value for the {@link HasNullable#in() in} attribute.
* @param in The value for in (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
public final Builder in(@Nullable Integer in) {
this.in = in;
return this;
}
/**
* Initializes the value for the {@link HasNullable#def() def} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link HasNullable#def() def}.
* @param def The value for def (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
public final Builder def(@Nullable String def) {
this.def = def;
optBits |= OPT_BIT_DEF;
return this;
}
/**
* Builds a new {@link ImmutableHasNullable ImmutableHasNullable}.
* @return An immutable instance of HasNullable
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableHasNullable build() {
return ImmutableHasNullable.validate(new ImmutableHasNullable(this));
}
private boolean defIsSet() {
return (optBits & OPT_BIT_DEF) != 0;
}
}
}