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

org.immutables.fixture.modifiable.ModifiableCompanion Maven / Gradle / Ivy

package org.immutables.fixture.modifiable;

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.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * A modifiable implementation of the {@link Companion Companion} type.
 * 

Use the {@link #create()} static factory methods to create new instances. * Use the {@link #toImmutable()} method to convert to canonical immutable instances. *

ModifiableCompanion is not thread-safe * @see ImmutableCompanion */ @SuppressWarnings("all") @ParametersAreNonnullByDefault @Generated({"Modifiables.generator", "Companion"}) @NotThreadSafe final class ModifiableCompanion implements Companion { private static final long INIT_BIT_INTEGER = 0x1L; private static final long INIT_BIT_STRING = 0x2L; private static final long INIT_BIT_ARRAY_INTS = 0x4L; private static final long INIT_BIT_ARRAY_STRINGS = 0x8L; private long initBits = 0xfL; private int integer; private String string; private @Nullable Boolean bools; private final ArrayList str = new ArrayList(); private final LinkedHashSet ints = new LinkedHashSet(); private int[] arrayInts; private String[] arrayStrings; private final TreeSet ords = new TreeSet(); private final EnumSet pols = EnumSet.noneOf(RetentionPolicy.class); private final TreeSet navs = new TreeSet(Collections.reverseOrder()); private final Map just = new LinkedHashMap(); private final TreeMap ordsmap = new TreeMap(); private final EnumMap polsmap = new EnumMap(RetentionPolicy.class); private final TreeMap navsmap = new TreeMap(Collections.reverseOrder()); private ModifiableCompanion() {} /** * Construct a modifiable instance of {@code Companion}. * @return A new modifiable instance */ public static ModifiableCompanion create() { return new ModifiableCompanion(); } /** * @return value of {@code integer} attribute */ @Override public final int integer() { if (!integerIsSet()) { checkRequiredAttributes(); } return integer; } /** * @return value of {@code string} attribute */ @Override public final String string() { if (!stringIsSet()) { checkRequiredAttributes(); } return string; } /** * @return value of {@code bools} attribute, may be {@code null} */ @Override public final @Nullable Boolean bools() { return bools; } /** * @return modifiable list {@code str} */ @Override public final List str() { return str; } /** * @return modifiable set {@code ints} */ @Override public final Set ints() { return ints; } /** * @return assigned modifiable {@code arrayInts} array */ @Override public final int[] arrayInts() { if (!arrayIntsIsSet()) { checkRequiredAttributes(); } return arrayInts; } /** * @return assigned modifiable {@code arrayStrings} array */ @Override public final String[] arrayStrings() { if (!arrayStringsIsSet()) { checkRequiredAttributes(); } return arrayStrings; } /** * @return modifiable sortedSet {@code ords} */ @Override public final SortedSet ords() { return ords; } /** * @return modifiable set {@code pols} */ @Override public final Set pols() { return pols; } /** * @return modifiable sortedSet {@code navs} */ @Override public final NavigableSet navs() { return navs; } /** * @return value of {@code just} attribute */ @Override public final Map just() { return just; } /** * @return value of {@code ordsmap} attribute */ @Override public final SortedMap ordsmap() { return ordsmap; } /** * @return value of {@code polsmap} attribute */ @Override public final Map polsmap() { return polsmap; } /** * @return value of {@code navsmap} attribute */ @Override public final NavigableMap navsmap() { return navsmap; } /** * Clears the object by setting all attributes to their initial values. * @return {@code this} for use in a chained invocation */ public ModifiableCompanion clear() { initBits = 0xfL; integer = 0; string = null; bools = null; str.clear(); ints.clear(); arrayInts = null; arrayStrings = null; ords.clear(); pols.clear(); navs.clear(); just.clear(); ordsmap.clear(); polsmap.clear(); navsmap.clear(); return this; } /** * Fill this modifiable instance with attribute values from the provided {@link Companion} instance. * Regular attribute values will be overridden, i.e. replaced with ones of an instance. * Any of the instance's absent optional values will not be copied (will not override current values). * Collection elements and entries will be added, not replaced. * @param instance The instance from which to copy values * @return {@code this} for use in a chained invocation */ public ModifiableCompanion from(Companion instance) { Preconditions.checkNotNull(instance, "instance"); setInteger(instance.integer()); setString(instance.string()); @Nullable Boolean boolsValue = instance.bools(); if (boolsValue != null) { setBools(boolsValue); } addAllStr(instance.str()); addAllInts(instance.ints()); setArrayInts(instance.arrayInts()); setArrayStrings(instance.arrayStrings()); addAllOrds(instance.ords()); addAllPols(instance.pols()); addAllNavs(instance.navs()); putAllJust(instance.just()); putAllOrdsmap(instance.ordsmap()); putAllPolsmap(instance.polsmap()); putAllNavsmap(instance.navsmap()); return this; } /** * Assigns a value to the {@link Companion#integer() integer} attribute. * @param integer The value for integer * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setInteger(int integer) { this.integer = integer; initBits &= ~INIT_BIT_INTEGER; return this; } /** * Assigns a value to the {@link Companion#string() string} attribute. * @param string The value for string * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setString(String string) { this.string = Preconditions.checkNotNull(string, "string"); initBits &= ~INIT_BIT_STRING; return this; } /** * Assigns a value to the {@link Companion#bools() bools} attribute. * @param bools The value for bools, can be {@code null} * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setBools(@Nullable Boolean bools) { this.bools = bools; return this; } /** * Adds one element to {@link Companion#str() str} list. * @param element The str element * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addStr(String element) { str.add(Preconditions.checkNotNull(element, "str element")); return this; } /** * Adds elements to {@link Companion#str() str} list. * @param elements An array of str elements * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion addStr(String... elements) { for (String element : elements) { addStr(Preconditions.checkNotNull(element, "str element")); } return this; } /** * Sets or replaces all elements for {@link Companion#str() str} list. * @param elements An iterable of str elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setStr(Iterable elements) { str.clear(); return addAllStr(elements); } /** * Adds elements to {@link Companion#str() str} list. * @param elements An iterable of str elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addAllStr(Iterable elements) { for (String element : elements) { str.add(Preconditions.checkNotNull(element, "str element")); } return this; } /** * Adds one element to {@link Companion#ints() ints} set. * @param element The ints element * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addInts(int element) { ints.add(element); return this; } /** * Adds elements to {@link Companion#ints() ints} set. * @param elements An array of ints elements * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion addInts(int... elements) { for (int element : elements) { addInts(element); } return this; } /** * Sets or replaces all elements for {@link Companion#ints() ints} set. * @param elements An iterable of ints elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setInts(Iterable elements) { ints.clear(); return addAllInts(elements); } /** * Adds elements to {@link Companion#ints() ints} set. * @param elements An iterable of ints elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addAllInts(Iterable elements) { for (int element : elements) { ints.add(element); } return this; } /** * Assigns a value to the {@link Companion#arrayInts() arrayInts} attribute. * @param elements The elements for arrayInts * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion setArrayInts(int... elements) { this.arrayInts = elements.clone(); initBits &= ~INIT_BIT_ARRAY_INTS; return this; } /** * Assigns a value to the {@link Companion#arrayStrings() arrayStrings} attribute. * @param elements The elements for arrayStrings * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion setArrayStrings(String... elements) { this.arrayStrings = elements.clone(); initBits &= ~INIT_BIT_ARRAY_STRINGS; return this; } /** * Adds one element to {@link Companion#ords() ords} sortedSet. * @param element The ords element * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addOrds(int element) { ords.add(element); return this; } /** * Adds elements to {@link Companion#ords() ords} sortedSet. * @param elements An array of ords elements * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion addOrds(int... elements) { for (int element : elements) { addOrds(element); } return this; } /** * Sets or replaces all elements for {@link Companion#ords() ords} sortedSet. * @param elements An iterable of ords elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setOrds(Iterable elements) { ords.clear(); return addAllOrds(elements); } /** * Adds elements to {@link Companion#ords() ords} sortedSet. * @param elements An iterable of ords elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addAllOrds(Iterable elements) { for (int element : elements) { ords.add(element); } return this; } /** * Adds one element to {@link Companion#pols() pols} set. * @param element The pols element * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addPols(RetentionPolicy element) { pols.add(Preconditions.checkNotNull(element, "pols element")); return this; } /** * Adds elements to {@link Companion#pols() pols} set. * @param elements An array of pols elements * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion addPols(RetentionPolicy... elements) { for (RetentionPolicy element : elements) { addPols(Preconditions.checkNotNull(element, "pols element")); } return this; } /** * Sets or replaces all elements for {@link Companion#pols() pols} set. * @param elements An iterable of pols elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setPols(Iterable elements) { pols.clear(); return addAllPols(elements); } /** * Adds elements to {@link Companion#pols() pols} set. * @param elements An iterable of pols elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addAllPols(Iterable elements) { for (RetentionPolicy element : elements) { pols.add(Preconditions.checkNotNull(element, "pols element")); } return this; } /** * Adds one element to {@link Companion#navs() navs} sortedSet. * @param element The navs element * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addNavs(int element) { navs.add(element); return this; } /** * Adds elements to {@link Companion#navs() navs} sortedSet. * @param elements An array of navs elements * @return {@code this} for use in a chained invocation */ public final ModifiableCompanion addNavs(int... elements) { for (int element : elements) { addNavs(element); } return this; } /** * Sets or replaces all elements for {@link Companion#navs() navs} sortedSet. * @param elements An iterable of navs elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setNavs(Iterable elements) { navs.clear(); return addAllNavs(elements); } /** * Adds elements to {@link Companion#navs() navs} sortedSet. * @param elements An iterable of navs elements * @return {@code this} for use in a chained invocation */ public ModifiableCompanion addAllNavs(Iterable elements) { for (int element : elements) { navs.add(element); } return this; } /** * Put one entry to the {@link Companion#just() just} map. * @param key The key in just map * @param value The associated value in the just map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putJust(long key, int value) { just.put( key, value); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link Companion#just() just} map. * Nulls are not permitted as keys or values. * @param entries The entries that will be added to the just map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setJust(Map entries) { just.clear(); return putAllJust(entries); } /** * Put all mappings from the specified map as entries to the {@link Companion#just() just} map. * Nulls are not permitted as keys or values. * @param entries to be added to just map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putAllJust(Map entries) { for (Map.Entry entry : entries.entrySet()) { just.put( Preconditions.checkNotNull(entry.getKey(), "just key"), Preconditions.checkNotNull(entry.getValue(), "just value")); } return this; } /** * Put one entry to the {@link Companion#ordsmap() ordsmap} map. * @param key The key in ordsmap map * @param value The associated value in the ordsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putOrdsmap(int key, String value) { ordsmap.put( key, Preconditions.checkNotNull(value, "ordsmap value")); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link Companion#ordsmap() ordsmap} map. * Nulls are not permitted as keys or values. * @param entries The entries that will be added to the ordsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setOrdsmap(Map entries) { ordsmap.clear(); return putAllOrdsmap(entries); } /** * Put all mappings from the specified map as entries to the {@link Companion#ordsmap() ordsmap} map. * Nulls are not permitted as keys or values. * @param entries to be added to ordsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putAllOrdsmap(Map entries) { for (Map.Entry entry : entries.entrySet()) { ordsmap.put( Preconditions.checkNotNull(entry.getKey(), "ordsmap key"), Preconditions.checkNotNull(entry.getValue(), "ordsmap value")); } return this; } /** * Put one entry to the {@link Companion#polsmap() polsmap} map. * @param key The key in polsmap map * @param value The associated value in the polsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putPolsmap(RetentionPolicy key, int value) { polsmap.put( Preconditions.checkNotNull(key, "polsmap key"), value); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link Companion#polsmap() polsmap} map. * Nulls are not permitted as keys or values. * @param entries The entries that will be added to the polsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setPolsmap(Map entries) { polsmap.clear(); return putAllPolsmap(entries); } /** * Put all mappings from the specified map as entries to the {@link Companion#polsmap() polsmap} map. * Nulls are not permitted as keys or values. * @param entries to be added to polsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putAllPolsmap(Map entries) { for (Map.Entry entry : entries.entrySet()) { polsmap.put( Preconditions.checkNotNull(entry.getKey(), "polsmap key"), Preconditions.checkNotNull(entry.getValue(), "polsmap value")); } return this; } /** * Put one entry to the {@link Companion#navsmap() navsmap} map. * @param key The key in navsmap map * @param value The associated value in the navsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putNavsmap(String key, int value) { navsmap.put( Preconditions.checkNotNull(key, "navsmap key"), value); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link Companion#navsmap() navsmap} map. * Nulls are not permitted as keys or values. * @param entries The entries that will be added to the navsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion setNavsmap(Map entries) { navsmap.clear(); return putAllNavsmap(entries); } /** * Put all mappings from the specified map as entries to the {@link Companion#navsmap() navsmap} map. * Nulls are not permitted as keys or values. * @param entries to be added to navsmap map * @return {@code this} for use in a chained invocation */ public ModifiableCompanion putAllNavsmap(Map entries) { for (Map.Entry entry : entries.entrySet()) { navsmap.put( Preconditions.checkNotNull(entry.getKey(), "navsmap key"), Preconditions.checkNotNull(entry.getValue(), "navsmap value")); } return this; } /** * Returns {@code true} if the required attribute {@link Companion#integer() integer} is set. * @return {@code true} if set */ public final boolean integerIsSet() { return (initBits & INIT_BIT_INTEGER) == 0; } /** * Returns {@code true} if the required attribute {@link Companion#string() string} is set. * @return {@code true} if set */ public final boolean stringIsSet() { return (initBits & INIT_BIT_STRING) == 0; } /** * Returns {@code true} if the required attribute {@link Companion#arrayInts() arrayInts} is set. * @return {@code true} if set */ public final boolean arrayIntsIsSet() { return (initBits & INIT_BIT_ARRAY_INTS) == 0; } /** * Returns {@code true} if the required attribute {@link Companion#arrayStrings() arrayStrings} is set. * @return {@code true} if set */ public final boolean arrayStringsIsSet() { return (initBits & INIT_BIT_ARRAY_STRINGS) == 0; } /** * Returns {@code true} if all required attributes are set, indicating that the object is initialized. * @return {@code true} if set */ public final boolean isInitialized() { return initBits == 0; } private void checkRequiredAttributes() { if (!isInitialized()) { throw new IllegalStateException(formatRequiredAttributesMessage()); } } private String formatRequiredAttributesMessage() { List attributes = Lists.newArrayList(); if (!integerIsSet()) attributes.add("integer"); if (!stringIsSet()) attributes.add("string"); if (!arrayIntsIsSet()) attributes.add("arrayInts"); if (!arrayStringsIsSet()) attributes.add("arrayStrings"); return "Companion in not initialized, some of the required attributes are not set " + attributes; } /** * Converts to {@link ImmutableCompanion ImmutableCompanion}. * @return An immutable instance of Companion */ public final ImmutableCompanion toImmutable() { checkRequiredAttributes(); return ImmutableCompanion.copyOf(this); } /** * This instance is equal to all instances of {@code ModifiableCompanion} that have equal attribute values. * An uninitialized instance is equal only to itself. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; if (!(another instanceof ModifiableCompanion)) return false; ModifiableCompanion other = (ModifiableCompanion) another; if (!isInitialized() || !other.isInitialized()) { return false; } return equalTo(other); } private boolean equalTo(ModifiableCompanion another) { return integer == another.integer && string.equals(another.string) && Objects.equal(bools, another.bools) && str.equals(another.str) && ints.equals(another.ints) && Arrays.equals(arrayInts, another.arrayInts) && Arrays.equals(arrayStrings, another.arrayStrings) && ords.equals(another.ords) && pols.equals(another.pols) && navs.equals(another.navs) && just.equals(another.just) && ordsmap.equals(another.ordsmap) && polsmap.equals(another.polsmap) && navsmap.equals(another.navsmap); } /** * Computes a hash code from attributes: {@code integer}, {@code string}, {@code bools}, {@code str}, {@code ints}, {@code arrayInts}, {@code arrayStrings}, {@code ords}, {@code pols}, {@code navs}, {@code just}, {@code ordsmap}, {@code polsmap}, {@code navsmap}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + integer; h = h * 17 + string.hashCode(); h = h * 17 + Objects.hashCode(bools); h = h * 17 + str.hashCode(); h = h * 17 + ints.hashCode(); h = h * 17 + Arrays.hashCode(arrayInts); h = h * 17 + Arrays.hashCode(arrayStrings); h = h * 17 + ords.hashCode(); h = h * 17 + pols.hashCode(); h = h * 17 + navs.hashCode(); h = h * 17 + just.hashCode(); h = h * 17 + ordsmap.hashCode(); h = h * 17 + polsmap.hashCode(); h = h * 17 + navsmap.hashCode(); return h; } /** * Generates a string representation of this {@code Companion}. * If uninitialized, some attribute values may appear as question marks. * @return A string representation */ @Override public String toString() { return MoreObjects.toStringHelper("ModifiableCompanion") .add("integer", integerIsSet() ? integer() : "?") .add("string", stringIsSet() ? string() : "?") .add("bools", bools()) .add("str", str()) .add("ints", ints()) .add("arrayInts", arrayIntsIsSet() ? Arrays.toString(arrayInts()) : "?") .add("arrayStrings", arrayStringsIsSet() ? Arrays.toString(arrayStrings()) : "?") .add("ords", ords()) .add("pols", pols()) .add("navs", navs()) .add("just", just()) .add("ordsmap", ordsmap()) .add("polsmap", polsmap()) .add("navsmap", navsmap()) .toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy