Class BindingMap


  • @Beta
    public final class BindingMap
    extends Object
    Utility class for instantiating Maps containing Identifiable values. Unlike normal Map instantiation utilities, methods in this class index values via their identifier, hence providing a more convenient API, amenable to fluent builders.

    A typical example of use with generated DataObjects looks like this:

       
         Foo foo = new FooBuilder()
              .setBar(BindingMap.of(
                  new BarBuilder().setName("one").build(),
                  new BarBuilder().setName("two").build()))
              .build();
       
     

    Another alternative is to use builders:

       
         Foo foo = new FooBuilder()
              .setBar(BindingMap.<BarKey, Bar>builder()
                  .add(new BarBuilder().setName("one").build())
                  .add(new BarBuilder().setName("two").build())
                  .build())
              .build();
       
     

    This class allows for two modes of operation:

    • Unordered, available through of(Identifiable...)/builder() family of functions. Maps instantiated through this, preferred, interface will have their iteration order randomized, as explain in Java 9+ unmodifiable collections.
    • Ordered, available through ordered(Identifiable...)/orderedBuilder() family of functions. Maps instantiated through this interface have a predictable iteration order, as per ImmutableMap class design. The use of this interface is generally discouraged, as it may lead to code relying on map iteration order. Nevertheless it may prove useful where the goal is to have predictable outcomes and hence is provided for completeness.
    • Method Detail

      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1)
        Returns an unmodifiable map containing a single mapping.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the mapping's value
        Returns:
        a Map containing the specified value
        Throws:
        NullPointerException - if the value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2)
        Returns an unmodifiable map containing two mappings. The resulting map is NOT guaranteed retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3)
        Returns an unmodifiable map containing three mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4)
        Returns an unmodifiable map containing four mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4,
                                                                                                           V v5)
        Returns an unmodifiable map containing five mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4,
                                                                                                           V v5,
                                                                                                           V v6)
        Returns an unmodifiable map containing six mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        v6 - the sixth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4,
                                                                                                           V v5,
                                                                                                           V v6,
                                                                                                           V v7)
        Returns an unmodifiable map containing seven mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        v6 - the sixth mapping's value
        v7 - the seventh mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4,
                                                                                                           V v5,
                                                                                                           V v6,
                                                                                                           V v7,
                                                                                                           V v8)
        Returns an unmodifiable map containing eight mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        v6 - the sixth mapping's value
        v7 - the seventh mapping's value
        v8 - the eighth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4,
                                                                                                           V v5,
                                                                                                           V v6,
                                                                                                           V v7,
                                                                                                           V v8,
                                                                                                           V v9)
        Returns an unmodifiable map containing nine mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        v6 - the sixth mapping's value
        v7 - the seventh mapping's value
        v8 - the eighth mapping's value
        v9 - the ninth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V v1,
                                                                                                           V v2,
                                                                                                           V v3,
                                                                                                           V v4,
                                                                                                           V v5,
                                                                                                           V v6,
                                                                                                           V v7,
                                                                                                           V v8,
                                                                                                           V v9,
                                                                                                           V v10)
        Returns an unmodifiable map containing ten mappings. The resulting map is NOT guaranteed to retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        v6 - the sixth mapping's value
        v7 - the seventh mapping's value
        v8 - the eighth mapping's value
        v9 - the ninth mapping's value
        v10 - the ninth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • of

        @SafeVarargs
        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(V... values)
        Returns an unmodifiable map containing given values. The resulting map is NOT guaranteed to retain iteration order of the input array.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        values - values from which the map is populated
        Returns:
        a Map containing the specified values
        Throws:
        IllegalArgumentException - if there are any duplicate keys in the provided values
        NullPointerException - if any value is null, or if the values array is null
      • of

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> of​(Collection<V> values)
        Returns an unmodifiable map containing given values. The resulting map is NOT guaranteed to retain iteration order of the input collection.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        values - values from which the map is populated
        Returns:
        a Map containing the specified values
        Throws:
        IllegalArgumentException - if there are any duplicate keys in the provided values
        NullPointerException - if any value is null, or if the values array is null
      • toMap

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Collector<V,​?,​? extends Map<K,​V>> toMap()
        Returns a collector which collects binding Identifiable objects into an unmodifiable map. The resulting map is NOT guaranteed to retain iteration order of the stream it collects.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Returns:
        A collector that accumulates the input elements into an unmodifiable map.
      • builder

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull BindingMap.Builder<K,​V> builder()
        Create a builder on an unmodifiable map, which does not retain value insertion order. The builder will be pre-sized to hold 4 elements.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Returns:
        A BindingMap.Builder instance.
      • builder

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull BindingMap.Builder<K,​V> builder​(int expectedSize)
        Create a builder on an unmodifiable map, which does not retain value insertion order. The builder will be pre-sized to hold specified number of elements.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        expectedSize - Expected number of values in the resulting map
        Returns:
        A BindingMap.Builder instance.
      • ordered

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> ordered​(V v1,
                                                                                                                V v2)
        Returns an unmodifiable map containing two mappings. The resulting map will retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • ordered

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> ordered​(V v1,
                                                                                                                V v2,
                                                                                                                V v3)
        Returns an unmodifiable map containing three mappings. The resulting map will retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • ordered

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> ordered​(V v1,
                                                                                                                V v2,
                                                                                                                V v3,
                                                                                                                V v4)
        Returns an unmodifiable map containing four mappings. The resulting map will retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • ordered

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> ordered​(V v1,
                                                                                                                V v2,
                                                                                                                V v3,
                                                                                                                V v4,
                                                                                                                V v5)
        Returns an unmodifiable map containing five mappings. The resulting map will retain iteration order of mappings.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        v1 - the first mapping's value
        v2 - the second mapping's value
        v3 - the third mapping's value
        v4 - the fourth mapping's value
        v5 - the fifth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        IllegalArgumentException - if the values contain duplicate keys
        NullPointerException - if any value is null
      • ordered

        @SafeVarargs
        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> ordered​(V... values)
        Returns an unmodifiable map containing given values. Resulting Map will retain the iteration order of values.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        values - values from which the map is populated
        Returns:
        a Map containing the specified values
        Throws:
        IllegalArgumentException - if there are any duplicate keys in the provided values
        NullPointerException - if any value is null, or if the values array is null
      • ordered

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Map<K,​V> ordered​(Collection<V> values)
        Returns an unmodifiable map containing given values. Resulting Map will retain the iteration order of values.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        values - values from which the map is populated
        Returns:
        a Map containing the specified values
        Throws:
        IllegalArgumentException - if there are any duplicate keys in the provided values
        NullPointerException - if any value is null, or if the values array is null
      • toOrderedMap

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull Collector<V,​?,​? extends Map<K,​V>> toOrderedMap()
        Returns a collector which collects binding Identifiable objects into an unmodifiable map. The resulting map will retain iteration order of the stream it collects.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Returns:
        A collector that accumulates the input elements into an unmodifiable map.
      • orderedBuilder

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull BindingMap.Builder<K,​V> orderedBuilder()
        Create a builder on an unmodifiable map, which retains value insertion order. The builder will be pre-sized to hold 4 elements.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Returns:
        A BindingMap.Builder instance.
      • orderedBuilder

        public static <K extends Identifier<V>,​V extends Identifiable<K>> @NonNull BindingMap.Builder<K,​V> orderedBuilder​(int expectedSize)
        Create a builder on an unmodifiable map, which retains value insertion order. The builder will be pre-sized to hold specified number of elements.
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        expectedSize - Expected number of values in the resulting map
        Returns:
        A BindingMap.Builder instance.