Class Index<K,​V>

  • Type Parameters:
    K - the key type
    V - the value type

    public final class Index<K,​V>
    extends java.lang.Object
    A bi-directional map in which keys and values must be unique.
    Since:
    4.0.0
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      @NotNull Index<K,​V>
      create​(@NotNull java.util.function.Function<? super V,​? extends K> keyFunction, @NotNull java.util.List<V> constants)
      Creates an index map.
      static <K,​V>
      @NotNull Index<K,​V>
      create​(@NotNull java.util.function.Function<? super V,​? extends K> keyFunction, @NotNull V @NotNull ... values)
      Creates an index map.
      static <K,​V extends java.lang.Enum<V>>
      @NotNull Index<K,​V>
      create​(java.lang.Class<V> type, @NotNull java.util.function.Function<? super V,​? extends K> keyFunction)
      Creates an index map.
      static <K,​V extends java.lang.Enum<V>>
      @NotNull Index<K,​V>
      create​(java.lang.Class<V> type, @NotNull java.util.function.Function<? super V,​? extends K> keyFunction, @NotNull V @NotNull ... values)
      Creates an index map.
      K key​(V value)
      Gets the key for a value.
      K keyOr​(V value, K defaultKey)
      Gets a key by its value or returns a fallback key.
      K keyOrThrow​(V value)
      Gets the key for a value or throws an exception.
      @NotNull java.util.Set<K> keys()
      Gets the keys.
      @NotNull java.util.Map<K,​V> keyToValue()
      Get an unmodifiable mapping of index entries from key to value.
      V value​(K key)
      Gets a value by its key.
      V valueOr​(K key, V defaultValue)
      Gets a value by its key or returns a fallback value.
      V valueOrThrow​(K key)
      Gets a value by its key.
      @NotNull java.util.Set<V> values()
      Gets the keys.
      @NotNull java.util.Map<V,​K> valueToKey()
      Get an unmodifiable mapping of index entries from value to key.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • create

        @NotNull
        public static <K,​V extends java.lang.Enum<V>> @NotNull Index<K,​V> create​(java.lang.Class<V> type,
                                                                                             @NotNull
                                                                                             @NotNull java.util.function.Function<? super V,​? extends K> keyFunction)
        Creates an index map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        type - the value type
        keyFunction - the key function
        Returns:
        the key map
        Since:
        4.0.0
      • create

        @SafeVarargs
        @NotNull
        public static <K,​V extends java.lang.Enum<V>> @NotNull Index<K,​V> create​(java.lang.Class<V> type,
                                                                                             @NotNull
                                                                                             @NotNull java.util.function.Function<? super V,​? extends K> keyFunction,
                                                                                             @NotNull
                                                                                             @NotNull V @NotNull ... values)
        Creates an index map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        type - the value type
        keyFunction - the key function
        values - the values
        Returns:
        the key map
        Since:
        4.0.0
      • create

        @SafeVarargs
        @NotNull
        public static <K,​V> @NotNull Index<K,​V> create​(@NotNull
                                                                   @NotNull java.util.function.Function<? super V,​? extends K> keyFunction,
                                                                   @NotNull
                                                                   @NotNull V @NotNull ... values)
        Creates an index map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        keyFunction - the key function
        values - the values
        Returns:
        the key map
        Since:
        4.0.0
      • create

        @NotNull
        public static <K,​V> @NotNull Index<K,​V> create​(@NotNull
                                                                   @NotNull java.util.function.Function<? super V,​? extends K> keyFunction,
                                                                   @NotNull
                                                                   @NotNull java.util.List<V> constants)
        Creates an index map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        keyFunction - the key function
        constants - the constants
        Returns:
        the key map
        Since:
        4.0.0
      • keys

        @NotNull
        public @NotNull java.util.Set<K> keys()
        Gets the keys.
        Returns:
        the keys
        Since:
        4.0.0
      • key

        @Nullable
        public K key​(@NotNull
                     V value)
        Gets the key for a value.
        Parameters:
        value - the value
        Returns:
        the key
        Since:
        4.0.0
      • keyOrThrow

        @NotNull
        public K keyOrThrow​(@NotNull
                            V value)
        Gets the key for a value or throws an exception.
        Parameters:
        value - the value
        Returns:
        the key
        Throws:
        java.util.NoSuchElementException - if there is no key for the value
        Since:
        4.11.0
      • keyOr

        @Contract("_, null -> null; _, !null -> !null")
        public K keyOr​(@NotNull
                       V value,
                       @Nullable
                       K defaultKey)
        Gets a key by its value or returns a fallback key.
        Parameters:
        value - the value
        defaultKey - the fallback key
        Returns:
        the key
        Since:
        4.11.0
      • values

        @NotNull
        public @NotNull java.util.Set<V> values()
        Gets the keys.
        Returns:
        the keys
        Since:
        4.0.0
      • value

        @Nullable
        public V value​(@NotNull
                       K key)
        Gets a value by its key.
        Parameters:
        key - the key
        Returns:
        the value
        Since:
        4.0.0
      • valueOrThrow

        @NotNull
        public V valueOrThrow​(@NotNull
                              K key)
        Gets a value by its key.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        java.util.NoSuchElementException - if there is no value for the key
        Since:
        4.11.0
      • valueOr

        @Contract("_, null -> null; _, !null -> !null")
        public V valueOr​(@NotNull
                         K key,
                         @Nullable
                         V defaultValue)
        Gets a value by its key or returns a fallback value.
        Parameters:
        key - the key
        defaultValue - the fallback value
        Returns:
        the value
        Since:
        4.11.0
      • keyToValue

        @NotNull
        public @NotNull java.util.Map<K,​V> keyToValue()
        Get an unmodifiable mapping of index entries from key to value.
        Returns:
        a mapping from key to value in the index
        Since:
        4.10.0
      • valueToKey

        @NotNull
        public @NotNull java.util.Map<V,​K> valueToKey()
        Get an unmodifiable mapping of index entries from value to key.
        Returns:
        a mapping from value to key in the index
        Since:
        4.10.0