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

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

package org.immutables.fixture.jdkonly;

import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.EnumMap;
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.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * 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. */ @SuppressWarnings("all") @ParametersAreNonnullByDefault @Generated({"Immutables.generator", "JdkMaps"}) @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. */ 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 */ 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 */ 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 */ public final JdkMapsBuilder putJust(Map.Entry entry) { this.just.put( Objects.requireNonNull(entry.getKey(), "just key"), Objects.requireNonNull(entry.getValue(), "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 just The entries that will be added to the just map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder just(Map just) { this.just.clear(); return putAllJust(just); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#just() just} map. Nulls are not permitted * @param just The entries that will be added to the just map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder putAllJust(Map just) { for (Map.Entry entry : just.entrySet()) { this.just.put( Objects.requireNonNull(entry.getKey(), "just key"), Objects.requireNonNull(entry.getValue(), "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 */ 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 */ public final JdkMapsBuilder putOrds(Map.Entry entry) { this.ords.put( Objects.requireNonNull(entry.getKey(), "ords key"), Objects.requireNonNull(entry.getValue(), "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 ords The entries that will be added to the ords map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder ords(Map ords) { this.ords.clear(); return putAllOrds(ords); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#ords() ords} map. Nulls are not permitted * @param ords The entries that will be added to the ords map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder putAllOrds(Map ords) { for (Map.Entry entry : ords.entrySet()) { this.ords.put( Objects.requireNonNull(entry.getKey(), "ords key"), Objects.requireNonNull(entry.getValue(), "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 */ 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 */ public final JdkMapsBuilder putPols(Map.Entry entry) { this.pols.put( Objects.requireNonNull(entry.getKey(), "pols key"), Objects.requireNonNull(entry.getValue(), "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 pols The entries that will be added to the pols map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder pols(Map pols) { this.pols.clear(); return putAllPols(pols); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#pols() pols} map. Nulls are not permitted * @param pols The entries that will be added to the pols map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder putAllPols(Map pols) { for (Map.Entry entry : pols.entrySet()) { this.pols.put( Objects.requireNonNull(entry.getKey(), "pols key"), Objects.requireNonNull(entry.getValue(), "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 */ 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 */ public final JdkMapsBuilder putNavs(Map.Entry entry) { this.navs.put( Objects.requireNonNull(entry.getKey(), "navs key"), Objects.requireNonNull(entry.getValue(), "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 navs The entries that will be added to the navs map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder navs(Map navs) { this.navs.clear(); return putAllNavs(navs); } /** * Put all mappings from the specified map as entries to {@link JdkMaps#navs() navs} map. Nulls are not permitted * @param navs The entries that will be added to the navs map * @return {@code this} builder for use in a chained invocation */ public final JdkMapsBuilder putAllNavs(Map navs) { for (Map.Entry entry : navs.entrySet()) { this.navs.put( Objects.requireNonNull(entry.getKey(), "navs key"), Objects.requireNonNull(entry.getValue(), "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()}. */ @Immutable 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, Collections.emptyMap()); this.pols = Collections.emptyMap(); this.navs = createUnmodifiableSortedMap(true, false, Collections.emptyMap()); } private ImmutableJdkMaps(JdkMapsBuilder builder) { this.just = createUnmodifiableMap(false, builder.just); this.ords = createUnmodifiableSortedMap(false, false, builder.ords); this.pols = createUnmodifiableEnumMap(false, builder.pols); this.navs = createUnmodifiableSortedMap(true, 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 ImmutableJdkMaps && equalTo((ImmutableJdkMaps) another); } private boolean equalTo(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() { int h = 31; h = h * 17 + just.hashCode(); h = h * 17 + ords.hashCode(); h = h * 17 + pols.hashCode(); h = h * 17 + 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 ImmutableJdkMaps INSTANCE = validate(new ImmutableJdkMaps()); /** * Returns the default immutable singleton value of {@code JdkMaps} * @return An immutable instance of JdkMaps */ public static JdkMaps of() { return INSTANCE; } private static ImmutableJdkMaps validate(ImmutableJdkMaps instance) { return INSTANCE != null && INSTANCE.equalTo(instance) ? INSTANCE : instance; } } private static Map createUnmodifiableMap(boolean checkNulls, 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"); } return Collections.singletonMap(k, v); } default: { Map linkedMap = new LinkedHashMap(map.size()); linkedMap.putAll(map); if (checkNulls) { for (Map.Entry e : linkedMap.entrySet()) { Objects.requireNonNull(e.getKey(), "key"); Objects.requireNonNull(e.getValue(), "value"); } } return Collections.unmodifiableMap(linkedMap); } } } private static , V> Map createUnmodifiableEnumMap(boolean checkNulls, Map map) { EnumMap enumMap = new EnumMap(map); if (checkNulls) { for (V value : map.values()) { Objects.requireNonNull(value, "value"); } } return Collections.unmodifiableMap(enumMap); } private static , V> NavigableMap createUnmodifiableSortedMap(boolean reverse, boolean checkNulls, Map map) { TreeMap sortedMap = reverse ? new TreeMap(Collections.reverseOrder()) : new TreeMap(); sortedMap.putAll(map); if (checkNulls) { for (Map.Entry e : sortedMap.entrySet()) { Objects.requireNonNull(e.getKey(), "key"); Objects.requireNonNull(e.getValue(), "value"); } } return Collections.unmodifiableNavigableMap(sortedMap); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy