org.immutables.fixture.jdkonly.ImmutableDefaultArray Maven / Gradle / Ivy
package org.immutables.fixture.jdkonly;
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.Ints;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
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 DefaultArray}.
*
* Use the builder to create immutable instances:
* {@code ImmutableDefaultArray.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "DefaultArray"})
@Immutable
public final class ImmutableDefaultArray extends DefaultArray {
private final int[] prop;
private final ImmutableSet ints;
private ImmutableDefaultArray(ImmutableDefaultArray.Builder builder) {
if (builder.prop != null) {
initShim.prop(builder.prop);
}
if (builder.intsIsSet()) {
initShim.ints(builder.ints.build());
}
this.prop = initShim.prop();
this.ints = initShim.ints();
this.initShim = null;
}
private ImmutableDefaultArray(int[] prop, ImmutableSet ints) {
this.prop = prop;
this.ints = ints;
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 int[] prop;
private int propStage;
int[] prop() {
if (propStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (propStage == STAGE_UNINITIALIZED) {
propStage = STAGE_INITIALIZING;
this.prop = ImmutableDefaultArray.super.prop().clone();
propStage = STAGE_INITIALIZED;
}
return this.prop;
}
void prop(int[] prop) {
this.prop = prop;
propStage = STAGE_INITIALIZED;
}
private ImmutableSet ints;
private int intsStage;
ImmutableSet ints() {
if (intsStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (intsStage == STAGE_UNINITIALIZED) {
intsStage = STAGE_INITIALIZING;
this.ints = Objects.requireNonNull(ImmutableDefaultArray.super.ints(), "ints");
intsStage = STAGE_INITIALIZED;
}
return this.ints;
}
void ints(ImmutableSet ints) {
this.ints = ints;
intsStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
ArrayList attributes = new ArrayList();
if (propStage == STAGE_INITIALIZING) attributes.add("prop");
if (intsStage == STAGE_INITIALIZING) attributes.add("ints");
return "Cannot build DefaultArray, attribute initializers form cycle" + attributes;
}
}
/**
* @return A cloned {@code prop} array
*/
@Override
int[] prop() {
InitShim shim = this.initShim;
return shim != null
? shim.prop().clone()
: this.prop.clone();
}
/**
* @return The value of the {@code ints} attribute
*/
@Override
ImmutableSet ints() {
InitShim shim = this.initShim;
return shim != null
? shim.ints()
: this.ints;
}
/**
* Copy the current immutable object with elements that replace the content of {@link DefaultArray#prop() prop}.
* The array is cloned before being saved as attribute values.
* @param elements The non-null elements for prop
* @return A modified copy of {@code this} object
*/
public final ImmutableDefaultArray withProp(int... elements) {
int[] newValue = elements.clone();
return new ImmutableDefaultArray(newValue, this.ints);
}
/**
* Copy the current immutable object with elements that replace the content of {@link DefaultArray#ints() ints}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableDefaultArray withInts(int... elements) {
ImmutableSet newValue = ImmutableSet.copyOf(Ints.asList(elements));
return new ImmutableDefaultArray(this.prop, newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link DefaultArray#ints() ints}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of ints elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableDefaultArray withInts(Iterable elements) {
if (this.ints == elements) return this;
ImmutableSet newValue = ImmutableSet.copyOf(elements);
return new ImmutableDefaultArray(this.prop, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableDefaultArray} 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 ImmutableDefaultArray
&& equalTo((ImmutableDefaultArray) another);
}
private boolean equalTo(ImmutableDefaultArray another) {
return Arrays.equals(prop, another.prop)
&& ints.equals(another.ints);
}
/**
* Computes a hash code from attributes: {@code prop}, {@code ints}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + Arrays.hashCode(prop);
h = h * 17 + ints.hashCode();
return h;
}
/**
* Prints the immutable value {@code DefaultArray} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "DefaultArray{"
+ "prop=" + Arrays.toString(prop)
+ ", ints=" + ints
+ "}";
}
/**
* Creates an immutable copy of a {@link DefaultArray} 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 DefaultArray instance
*/
public static ImmutableDefaultArray copyOf(DefaultArray instance) {
if (instance instanceof ImmutableDefaultArray) {
return (ImmutableDefaultArray) instance;
}
return ImmutableDefaultArray.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableDefaultArray ImmutableDefaultArray}.
* @return A new ImmutableDefaultArray builder
*/
public static ImmutableDefaultArray.Builder builder() {
return new ImmutableDefaultArray.Builder();
}
/**
* Builds instances of type {@link ImmutableDefaultArray ImmutableDefaultArray}.
* 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_INTS = 0x1L;
private long optBits;
private @Nullable int[] prop;
private ImmutableSet.Builder ints = ImmutableSet.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code DefaultArray} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(DefaultArray instance) {
Objects.requireNonNull(instance, "instance");
prop(instance.prop());
addAllInts(instance.ints());
return this;
}
/**
* Initializes the value for the {@link DefaultArray#prop() prop} attribute.
* If not set, this attribute will have a default value as defined by {@link DefaultArray#prop() prop}.
* @param prop The elements for prop
* @return {@code this} builder for use in a chained invocation
*/
public final Builder prop(int... prop) {
this.prop = prop.clone();
return this;
}
/**
* Adds one element to {@link DefaultArray#ints() ints} set.
* @param element A ints element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addInts(int element) {
this.ints.add(element);
optBits |= OPT_BIT_INTS;
return this;
}
/**
* Adds elements to {@link DefaultArray#ints() ints} set.
* @param elements An array of ints elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addInts(int... elements) {
this.ints.addAll(Ints.asList(elements));
optBits |= OPT_BIT_INTS;
return this;
}
/**
* Sets or replaces all elements for {@link DefaultArray#ints() ints} set.
* @param elements An iterable of ints elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder ints(Iterable elements) {
this.ints = ImmutableSet.builder();
return addAllInts(elements);
}
/**
* Adds elements to {@link DefaultArray#ints() ints} set.
* @param elements An iterable of ints elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllInts(Iterable elements) {
this.ints.addAll(elements);
optBits |= OPT_BIT_INTS;
return this;
}
/**
* Builds a new {@link ImmutableDefaultArray ImmutableDefaultArray}.
* @return An immutable instance of DefaultArray
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableDefaultArray build() {
return new ImmutableDefaultArray(this);
}
private boolean intsIsSet() {
return (optBits & OPT_BIT_INTS) != 0;
}
}
}