All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.immutables.fixture.jdkonly.ImmutableDefaultArray Maven / Gradle / Ivy

There is a newer version: 2.6.0.alpha0
Show newest version
package org.immutables.fixture.jdkonly;

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 @Nullable int[] props; private ImmutableDefaultArray(ImmutableDefaultArray.Builder builder) { if (builder.prop != null) { initShim.prop(builder.prop); } if (builder.propsIsSet()) { initShim.props(builder.props); } this.prop = initShim.prop(); this.props = initShim.props(); this.initShim = null; } private ImmutableDefaultArray(int[] prop, @Nullable int[] props) { this.prop = prop; this.props = props; 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; int[] prop = ImmutableDefaultArray.super.prop(); this.prop = prop.clone(); propStage = STAGE_INITIALIZED; } return this.prop; } void prop(int[] prop) { this.prop = prop; propStage = STAGE_INITIALIZED; } private int[] props; private int propsStage; int[] props() { if (propsStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (propsStage == STAGE_UNINITIALIZED) { propsStage = STAGE_INITIALIZING; @Nullable int[] props = ImmutableDefaultArray.super.props(); this.props = props; propsStage = STAGE_INITIALIZED; } return this.props; } void props(int[] props) { this.props = props; propsStage = STAGE_INITIALIZED; } private String formatInitCycleMessage() { ArrayList attributes = new ArrayList(); if (propStage == STAGE_INITIALIZING) attributes.add("prop"); if (propsStage == STAGE_INITIALIZING) attributes.add("props"); 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() : this.prop; } /** * @return A cloned {@code props} array */ @Override @Nullable int[] props() { InitShim shim = this.initShim; return shim != null ? shim.props() : this.props; } /** * 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.props); } /** * Copy the current immutable object with elements that replace the content of {@link DefaultArray#props() props}. * The array is cloned before being saved as attribute values. * @param elements The non-null elements for props * @return A modified copy of {@code this} object */ public final ImmutableDefaultArray withProps(@Nullable int... elements) { @Nullable int[] newValue = elements == null ? null : elements.clone(); 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) && Arrays.equals(props, another.props); } /** * Computes a hash code from attributes: {@code prop}, {@code props}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + Arrays.hashCode(prop); h = h * 17 + Arrays.hashCode(props); 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) + ", props=" + Arrays.toString(props) + "}"; } /** * 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_PROPS = 0x1L; private long optBits; private @Nullable int[] prop; private @Nullable int[] props; 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. * @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()); @Nullable int[] propsValue = instance.props(); if (propsValue != null) { props(propsValue); } 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; } /** * Initializes the value for the {@link DefaultArray#props() props} attribute. *

If not set, this attribute will have a default value as defined by {@link DefaultArray#props() props}. * @param props The elements for props * @return {@code this} builder for use in a chained invocation */ public final Builder props(int... props) { this.props = props; optBits |= OPT_BIT_PROPS; 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 propsIsSet() { return (optBits & OPT_BIT_PROPS) != 0; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy