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

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

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

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;

/**
 * Builds instances of type {@link JdkMaps JdkMaps}.
 * Initialize attributes and then invoke the {@link #build()} method to create an
 * immutable instance.
 * 

{@code JdkMapsBuilder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ @Generated(from = "JdkMaps", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") @NotThreadSafe public final class JdkMapsBuilder { private Map just = new LinkedHashMap(); private Map ords = new LinkedHashMap(); private EnumMap pols = new EnumMap(RetentionPolicy.class); private Map navs = new LinkedHashMap(); /** * Creates a builder for {@link JdkMaps JdkMaps} instances. *

   * new JdkMapsBuilder()
   *    .putJust|putAllJust(long => int) // {@link JdkMaps#just() just} mappings
   *    .putOrds|putAllOrds(int => String) // {@link JdkMaps#ords() ords} mappings
   *    .putPols|putAllPols(annotation.RetentionPolicy => int) // {@link JdkMaps#pols() pols} mappings
   *    .putNavs|putAllNavs(String => int) // {@link JdkMaps#navs() navs} mappings
   *    .build();
   * 
*/ public JdkMapsBuilder() { } /** * Fill a builder with attribute values from the provided {@code JdkMaps} 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 */ @CanIgnoreReturnValue public final JdkMapsBuilder from(JdkMaps instance) { Objects.requireNonNull(instance, "instance"); putAllJust(instance.just()); putAllOrds(instance.ords()); putAllPols(instance.pols()); putAllNavs(instance.navs()); return this; } /** * Put one entry to the {@link JdkMaps#just() just} map. * @param key The key in the just map * @param value The associated value in the just map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putJust(long key, int value) { this.just.put( Objects.requireNonNull(key, "just key"), Objects.requireNonNull(value, "just value")); return this; } /** * Put one entry to the {@link JdkMaps#just() just} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putJust(Map.Entry entry) { Long k = entry.getKey(); Integer v = entry.getValue(); this.just.put( Objects.requireNonNull(k, "just key"), Objects.requireNonNull(v, "just value")); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link JdkMaps#just() just} map. Nulls are not permitted * @param entries The entries that will be added to the just map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder just(Map entries) { this.just.clear(); return putAllJust(entries); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#just() just} map. Nulls are not permitted * @param entries The entries that will be added to the just map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putAllJust(Map entries) { for (Map.Entry e : entries.entrySet()) { Long k = e.getKey(); Integer v = e.getValue(); this.just.put( Objects.requireNonNull(k, "just key"), Objects.requireNonNull(v, "just value")); } return this; } /** * Put one entry to the {@link JdkMaps#ords() ords} map. * @param key The key in the ords map * @param value The associated value in the ords map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putOrds(int key, String value) { this.ords.put( Objects.requireNonNull(key, "ords key"), Objects.requireNonNull(value, "ords value")); return this; } /** * Put one entry to the {@link JdkMaps#ords() ords} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putOrds(Map.Entry entry) { Integer k = entry.getKey(); String v = entry.getValue(); this.ords.put( Objects.requireNonNull(k, "ords key"), Objects.requireNonNull(v, "ords value")); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link JdkMaps#ords() ords} map. Nulls are not permitted * @param entries The entries that will be added to the ords map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder ords(Map entries) { this.ords.clear(); return putAllOrds(entries); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#ords() ords} map. Nulls are not permitted * @param entries The entries that will be added to the ords map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putAllOrds(Map entries) { for (Map.Entry e : entries.entrySet()) { Integer k = e.getKey(); String v = e.getValue(); this.ords.put( Objects.requireNonNull(k, "ords key"), Objects.requireNonNull(v, "ords value")); } return this; } /** * Put one entry to the {@link JdkMaps#pols() pols} map. * @param key The key in the pols map * @param value The associated value in the pols map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putPols(RetentionPolicy key, int value) { this.pols.put( Objects.requireNonNull(key, "pols key"), Objects.requireNonNull(value, "pols value")); return this; } /** * Put one entry to the {@link JdkMaps#pols() pols} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putPols(Map.Entry entry) { RetentionPolicy k = entry.getKey(); Integer v = entry.getValue(); this.pols.put( Objects.requireNonNull(k, "pols key"), Objects.requireNonNull(v, "pols value")); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link JdkMaps#pols() pols} map. Nulls are not permitted * @param entries The entries that will be added to the pols map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder pols(Map entries) { this.pols.clear(); return putAllPols(entries); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#pols() pols} map. Nulls are not permitted * @param entries The entries that will be added to the pols map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putAllPols(Map entries) { for (Map.Entry e : entries.entrySet()) { RetentionPolicy k = e.getKey(); Integer v = e.getValue(); this.pols.put( Objects.requireNonNull(k, "pols key"), Objects.requireNonNull(v, "pols value")); } return this; } /** * Put one entry to the {@link JdkMaps#navs() navs} map. * @param key The key in the navs map * @param value The associated value in the navs map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putNavs(String key, int value) { this.navs.put( Objects.requireNonNull(key, "navs key"), Objects.requireNonNull(value, "navs value")); return this; } /** * Put one entry to the {@link JdkMaps#navs() navs} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putNavs(Map.Entry entry) { String k = entry.getKey(); Integer v = entry.getValue(); this.navs.put( Objects.requireNonNull(k, "navs key"), Objects.requireNonNull(v, "navs value")); return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link JdkMaps#navs() navs} map. Nulls are not permitted * @param entries The entries that will be added to the navs map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder navs(Map entries) { this.navs.clear(); return putAllNavs(entries); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#navs() navs} map. Nulls are not permitted * @param entries The entries that will be added to the navs map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final JdkMapsBuilder putAllNavs(Map entries) { for (Map.Entry e : entries.entrySet()) { String k = e.getKey(); Integer v = e.getValue(); this.navs.put( Objects.requireNonNull(k, "navs key"), Objects.requireNonNull(v, "navs value")); } return this; } /** * Builds a new {@link JdkMaps JdkMaps}. * @return An immutable instance of JdkMaps * @throws java.lang.IllegalStateException if any required attributes are missing */ public JdkMaps build() { return JdkMapsBuilder.ImmutableJdkMaps.validate(new JdkMapsBuilder.ImmutableJdkMaps(this)); } /** * Immutable implementation of {@link JdkMaps}. *

* Use the builder to create immutable instances: * {@code new JdkMapsBuilder()}. * Use the static factory method to get the default singleton instance: * {@code JdkMapsBuilder.ImmutableJdkMaps.of()}. */ @Generated(from = "JdkMaps", generator = "Immutables") @Immutable @CheckReturnValue private static final class ImmutableJdkMaps implements JdkMaps { private final Map just; private final SortedMap ords; private final Map pols; private final NavigableMap navs; private ImmutableJdkMaps() { this.just = Collections.emptyMap(); this.ords = createUnmodifiableSortedMap(false, false, false, Collections.emptyMap()); this.pols = Collections.emptyMap(); this.navs = createUnmodifiableSortedMap(true, false, false, Collections.emptyMap()); } private ImmutableJdkMaps(JdkMapsBuilder builder) { this.just = createUnmodifiableMap(false, false, builder.just); this.ords = createUnmodifiableSortedMap(false, false, false, builder.ords); this.pols = createUnmodifiableEnumMap(false, false, builder.pols); this.navs = createUnmodifiableSortedMap(true, false, false, builder.navs); } /** * @return The value of the {@code just} attribute */ @Override public Map just() { return just; } /** * @return The value of the {@code ords} attribute */ @Override public SortedMap ords() { return ords; } /** * @return The value of the {@code pols} attribute */ @Override public Map pols() { return pols; } /** * @return The value of the {@code navs} attribute */ @Override public NavigableMap navs() { return navs; } /** * This instance is equal to all instances of {@code ImmutableJdkMaps} 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 JdkMapsBuilder.ImmutableJdkMaps && equalTo(0, (JdkMapsBuilder.ImmutableJdkMaps) another); } private boolean equalTo(int synthetic, JdkMapsBuilder.ImmutableJdkMaps another) { return just.equals(another.just) && ords.equals(another.ords) && pols.equals(another.pols) && navs.equals(another.navs); } /** * Computes a hash code from attributes: {@code just}, {@code ords}, {@code pols}, {@code navs}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + just.hashCode(); h += (h << 5) + ords.hashCode(); h += (h << 5) + pols.hashCode(); h += (h << 5) + navs.hashCode(); return h; } /** * Prints the immutable value {@code JdkMaps} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "JdkMaps{" + "just=" + just + ", ords=" + ords + ", pols=" + pols + ", navs=" + navs + "}"; } private static final JdkMapsBuilder.ImmutableJdkMaps INSTANCE = validate(new JdkMapsBuilder.ImmutableJdkMaps()); /** * Returns the default immutable singleton value of {@code JdkMaps} * @return An immutable instance of JdkMaps */ public static JdkMaps of() { return INSTANCE; } private static JdkMapsBuilder.ImmutableJdkMaps validate(JdkMapsBuilder.ImmutableJdkMaps instance) { return INSTANCE != null && INSTANCE.equalTo(0, instance) ? INSTANCE : instance; } } private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map map) { switch (map.size()) { case 0: return Collections.emptyMap(); case 1: { Map.Entry e = map.entrySet().iterator().next(); K k = e.getKey(); V v = e.getValue(); if (checkNulls) { Objects.requireNonNull(k, "key"); Objects.requireNonNull(v, "value"); } if (skipNulls && (k == null || v == null)) { return Collections.emptyMap(); } return Collections.singletonMap(k, v); } default: { Map linkedMap = new LinkedHashMap<>(map.size()); if (skipNulls || checkNulls) { for (Map.Entry e : map.entrySet()) { K k = e.getKey(); V v = e.getValue(); if (skipNulls) { if (k == null || v == null) continue; } else if (checkNulls) { Objects.requireNonNull(k, "key"); Objects.requireNonNull(v, "value"); } linkedMap.put(k, v); } } else { linkedMap.putAll(map); } return Collections.unmodifiableMap(linkedMap); } } } private static , V> Map createUnmodifiableEnumMap(boolean checkNulls, boolean skipNulls, Map map) { if (map.isEmpty()) return Collections.emptyMap(); EnumMap enumMap = new EnumMap<>(map); if (checkNulls || skipNulls) { Iterator it = map.values().iterator(); while(it.hasNext()) { V v = it.next(); if (checkNulls) { Objects.requireNonNull(v, "value"); } else if (skipNulls) { if (v == null) it.remove(); } } } return Collections.unmodifiableMap(enumMap); } private static , V> NavigableMap createUnmodifiableSortedMap(boolean reverse, boolean checkNulls, boolean skipNulls, Map map) { TreeMap sortedMap = reverse ? new TreeMap(Collections.reverseOrder()) : new TreeMap(); if (checkNulls || skipNulls) { for (Map.Entry e : map.entrySet()) { K k = e.getKey(); V v = e.getValue(); if (skipNulls) { if (k == null || v == null) continue; } else if (checkNulls) { Objects.requireNonNull(k, "key"); Objects.requireNonNull(v, "value"); } sortedMap.put(k, v); } } else { sortedMap.putAll(map); } return Collections.unmodifiableNavigableMap(sortedMap); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy