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

org.immutables.fixture.generics.ImmutableSecondie Maven / Gradle / Ivy

package org.immutables.fixture.generics;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 Secondie}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableSecondie.builder()}. * Use the static factory method to create immutable instances: * {@code ImmutableSecondie.of()}. */ @SuppressWarnings("all") @ParametersAreNonnullByDefault @Generated({"Immutables.generator", "Secondie"}) @Immutable public final class ImmutableSecondie implements Secondie { private final @Nullable V integer; private final @Nullable ImmutableList list; private final @Nullable ImmutableSet set; private final ImmutableMultimap coll; private final @Nullable List lst; private ImmutableSecondie(@Nullable V integer, @Nullable Iterable list) { this.integer = integer; this.list = list == null ? null : ImmutableList.copyOf(list); this.coll = ImmutableMultimap.of(); this.set = initShim.set(); this.lst = initShim.lst(); this.initShim = null; } private ImmutableSecondie(ImmutableSecondie.Builder builder) { this.integer = builder.integer; this.list = builder.list == null ? null : builder.list.build(); this.coll = builder.coll.build(); if (builder.setIsSet()) { initShim.set(builder.set == null ? null : builder.set.build()); } this.set = initShim.set(); this.lst = initShim.lst(); this.initShim = null; } private ImmutableSecondie( @Nullable V integer, @Nullable ImmutableList list, @Nullable ImmutableSet set, ImmutableMultimap coll) { this.integer = integer; this.list = list; this.set = set; this.coll = coll; initShim.set(this.set); this.lst = initShim.lst(); 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 ImmutableSet set; private int setStage; ImmutableSet set() { if (setStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (setStage == STAGE_UNINITIALIZED) { setStage = STAGE_INITIALIZING; this.set = setInitialize() == null ? null : ImmutableSet.copyOf(setInitialize()); setStage = STAGE_INITIALIZED; } return this.set; } void set(ImmutableSet set) { this.set = set; setStage = STAGE_INITIALIZED; } private List lst; private int lstStage; List lst() { if (lstStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (lstStage == STAGE_UNINITIALIZED) { lstStage = STAGE_INITIALIZING; this.lst = lstInitialize(); lstStage = STAGE_INITIALIZED; } return this.lst; } private String formatInitCycleMessage() { ArrayList attributes = Lists.newArrayList(); if (setStage == STAGE_INITIALIZING) attributes.add("set"); if (lstStage == STAGE_INITIALIZING) attributes.add("lst"); return "Cannot build Secondie, attribute initializers form cycle" + attributes; } } private @Nullable Set setInitialize() { return Secondie.super.set(); } private @Nullable List lstInitialize() { return Secondie.super.lst(); } /** * @return The value of the {@code integer} attribute */ @Override public @Nullable V integer() { return integer; } /** * @return The value of the {@code list} attribute */ @Override public @Nullable ImmutableList list() { return list; } /** * @return The value of the {@code set} attribute */ @Override public @Nullable ImmutableSet set() { InitShim shim = this.initShim; return shim != null ? shim.set() : this.set; } /** * @return The value of the {@code coll} attribute */ @Override public ImmutableMultimap coll() { return coll; } /** * @return The computed-at-construction value of the {@code lst} attribute */ @Override public @Nullable List lst() { InitShim shim = this.initShim; return shim != null ? shim.lst() : this.lst; } /** * Copy the current immutable object by setting a value for the {@link Secondie#integer() integer} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param integer A new value for integer (can be {@code null}) * @return A modified copy of the {@code this} object */ public final ImmutableSecondie withInteger(@Nullable V integer) { if (this.integer == integer) return this; return new ImmutableSecondie<>(integer, this.list, this.set, this.coll); } /** * Copy the current immutable object with elements that replace the content of {@link Secondie#list() list}. * @param elements The elements to set * @return A modified copy of {@code this} object */ @SafeVarargs public final ImmutableSecondie withList(@Nullable T... elements) { if (elements == null) { return new ImmutableSecondie<>(this.integer, null, this.set, this.coll); } @Nullable ImmutableList newValue = elements == null ? null : ImmutableList.copyOf(elements); return new ImmutableSecondie<>(this.integer, newValue, this.set, this.coll); } /** * Copy the current immutable object with elements that replace the content of {@link Secondie#list() list}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of list elements to set * @return A modified copy of {@code this} object */ public final ImmutableSecondie withList(@Nullable Iterable elements) { if (this.list == elements) return this; @Nullable ImmutableList newValue = elements == null ? null : ImmutableList.copyOf(elements); return new ImmutableSecondie<>(this.integer, newValue, this.set, this.coll); } /** * Copy the current immutable object with elements that replace the content of {@link Secondie#set() set}. * @param elements The elements to set * @return A modified copy of {@code this} object */ @SafeVarargs public final ImmutableSecondie withSet(@Nullable V... elements) { if (elements == null) { return new ImmutableSecondie<>(this.integer, this.list, null, this.coll); } @Nullable ImmutableSet newValue = elements == null ? null : ImmutableSet.copyOf(elements); return new ImmutableSecondie<>(this.integer, this.list, newValue, this.coll); } /** * Copy the current immutable object with elements that replace the content of {@link Secondie#set() set}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of set elements to set * @return A modified copy of {@code this} object */ public final ImmutableSecondie withSet(@Nullable Iterable elements) { if (this.set == elements) return this; @Nullable ImmutableSet newValue = elements == null ? null : ImmutableSet.copyOf(elements); return new ImmutableSecondie<>(this.integer, this.list, newValue, this.coll); } /** * Copy the current immutable object by replacing the {@link Secondie#coll() coll} map with the specified map. * Nulls are not permitted as keys or values. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param coll The entries to be added to the coll map * @return A modified copy of {@code this} object */ public final ImmutableSecondie withColl(Multimap coll) { if (this.coll == coll) return this; ImmutableMultimap newValue = ImmutableMultimap.copyOf(coll); return new ImmutableSecondie<>(this.integer, this.list, this.set, newValue); } /** * This instance is equal to all instances of {@code ImmutableSecondie} 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 ImmutableSecondie && equalTo((ImmutableSecondie) another); } private boolean equalTo(ImmutableSecondie another) { return Objects.equal(integer, another.integer) && Objects.equal(list, another.list) && Objects.equal(set, another.set) && coll.equals(another.coll) && Objects.equal(lst, another.lst); } /** * Computes a hash code from attributes: {@code integer}, {@code list}, {@code set}, {@code coll}, {@code lst}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + Objects.hashCode(integer); h = h * 17 + Objects.hashCode(list); h = h * 17 + Objects.hashCode(set); h = h * 17 + coll.hashCode(); h = h * 17 + Objects.hashCode(lst); return h; } /** * Prints the immutable value {@code Secondie} with attribute values. * @return A string representation of the value */ @Override public String toString() { return MoreObjects.toStringHelper("Secondie") .omitNullValues() .add("integer", integer) .add("list", list) .add("set", set) .add("coll", coll) .add("lst", lst) .toString(); } /** * Construct a new immutable {@code Secondie} instance. * @param integer The value for the {@code integer} attribute * @param list The value for the {@code list} attribute * @return An immutable Secondie instance */ public static ImmutableSecondie of(@Nullable V integer, @Nullable List list) { return of(integer, (Iterable) list); } /** * Construct a new immutable {@code Secondie} instance. * @param integer The value for the {@code integer} attribute * @param list The value for the {@code list} attribute * @return An immutable Secondie instance */ public static ImmutableSecondie of(@Nullable V integer, @Nullable Iterable list) { return new ImmutableSecondie<>(integer, list); } /** * Creates an immutable copy of a {@link Secondie} 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 generic parameter V * @param instance The instance to copy * @return A copied immutable Secondie instance */ public static ImmutableSecondie copyOf(Secondie instance) { if (instance instanceof ImmutableSecondie) { return (ImmutableSecondie) instance; } return ImmutableSecondie.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableSecondie ImmutableSecondie}. * @param generic parameter T * @param generic parameter V * @return A new ImmutableSecondie builder */ public static ImmutableSecondie.Builder builder() { return new ImmutableSecondie.Builder(); } /** * Builds instances of type {@link ImmutableSecondie ImmutableSecondie}. * 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_SET = 0x1L; private long optBits; private @Nullable V integer; private ImmutableList.Builder list = null; private ImmutableSet.Builder set = null; private ImmutableMultimap.Builder coll = ImmutableMultimap.builder(); private Builder() { } /** * Fill a builder with attribute values from the provided {@code Secondie} 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(Secondie instance) { Preconditions.checkNotNull(instance, "instance"); @Nullable V integerValue = instance.integer(); if (integerValue != null) { integer(integerValue); } addAllList(instance.list()); addAllSet(instance.set()); putAllColl(instance.coll()); return this; } /** * Initializes the value for the {@link Secondie#integer() integer} attribute. * @param integer The value for integer (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ public final Builder integer(@Nullable V integer) { this.integer = integer; return this; } /** * Adds one element to {@link Secondie#list() list} list. * @param element A list element * @return {@code this} builder for use in a chained invocation */ public final Builder addList(T element) { if (this.list == null) { this.list = ImmutableList.builder(); } this.list.add(element); return this; } /** * Adds elements to {@link Secondie#list() list} list. * @param elements An array of list elements * @return {@code this} builder for use in a chained invocation */ @SafeVarargs public final Builder addList(T... elements) { if (this.list == null) { this.list = ImmutableList.builder(); } this.list.add(elements); return this; } /** * Sets or replaces all elements for {@link Secondie#list() list} list. * @param elements An iterable of list elements * @return {@code this} builder for use in a chained invocation */ public final Builder list(@Nullable Iterable elements) { if (elements == null) { this.list = null; return this; } this.list = ImmutableList.builder(); return addAllList(elements); } /** * Adds elements to {@link Secondie#list() list} list. * @param elements An iterable of list elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAllList(Iterable elements) { Preconditions.checkNotNull(elements, "list element"); if (this.list == null) { this.list = ImmutableList.builder(); } this.list.addAll(elements); return this; } /** * Adds one element to {@link Secondie#set() set} set. * @param element A set element * @return {@code this} builder for use in a chained invocation */ public final Builder addSet(V element) { if (this.set == null) { this.set = ImmutableSet.builder(); } this.set.add(element); optBits |= OPT_BIT_SET; return this; } /** * Adds elements to {@link Secondie#set() set} set. * @param elements An array of set elements * @return {@code this} builder for use in a chained invocation */ @SafeVarargs public final Builder addSet(V... elements) { if (this.set == null) { this.set = ImmutableSet.builder(); } this.set.add(elements); optBits |= OPT_BIT_SET; return this; } /** * Sets or replaces all elements for {@link Secondie#set() set} set. * @param elements An iterable of set elements * @return {@code this} builder for use in a chained invocation */ public final Builder set(@Nullable Iterable elements) { if (elements == null) { this.set = null; optBits |= OPT_BIT_SET; return this; } this.set = ImmutableSet.builder(); return addAllSet(elements); } /** * Adds elements to {@link Secondie#set() set} set. * @param elements An iterable of set elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAllSet(Iterable elements) { Preconditions.checkNotNull(elements, "set element"); if (this.set == null) { this.set = ImmutableSet.builder(); } this.set.addAll(elements); optBits |= OPT_BIT_SET; return this; } /** * Put all mappings from the specified key to values for {@link Secondie#coll() coll} true. Nulls are not permitted * @param key The key for coll * @param values The values for coll * @return {@code this} builder for use in a chained invocation */ @SafeVarargs public final Builder putColl(T key, V... values) { this.coll.putAll(key, Arrays.asList(values)); return this; } /** * Put all mappings from the specified key to values for {@link Secondie#coll() coll} true. Nulls are not permitted * @param key The key for coll * @param values The values for coll * @return {@code this} builder for use in a chained invocation */ public final Builder putAllColl(T key, Iterable values) { this.coll.putAll(key, values); return this; } /** * Put one entry to the {@link Secondie#coll() coll} map. * @param key The key in the coll map * @param value The associated value in the coll map * @return {@code this} builder for use in a chained invocation */ public final Builder putColl(T key, V value) { this.coll.put(key, value); return this; } /** * Put one entry to the {@link Secondie#coll() coll} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ public final Builder putColl(Map.Entry entry) { this.coll.put(entry); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link Secondie#coll() coll} map. Nulls are not permitted * @param coll The entries that will be added to the coll map * @return {@code this} builder for use in a chained invocation */ public final Builder coll(Multimap coll) { this.coll = ImmutableMultimap.builder(); return putAllColl(coll); } /** * Put all mappings from the specified map as entries to {@link Secondie#coll() coll} map. Nulls are not permitted * @param coll The entries that will be added to the coll map * @return {@code this} builder for use in a chained invocation */ public final Builder putAllColl(Multimap coll) { this.coll.putAll(coll); return this; } /** * Builds a new {@link ImmutableSecondie ImmutableSecondie}. * @return An immutable instance of Secondie * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableSecondie build() { return new ImmutableSecondie(this); } private boolean setIsSet() { return (optBits & OPT_BIT_SET) != 0; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy