Class KeyValue<K,​V>

  • Type Parameters:
    K - Key type.
    V - Value type.
    All Implemented Interfaces:
    Serializable

    public class KeyValue<K,​V>
    extends Value<V>
    A key-value container extension to Value. A KeyValue requires always a non-null key on construction.
    Author:
    Will Glozer, Mark Paluch
    See Also:
    Serialized Form
    • Constructor Detail

      • KeyValue

        protected KeyValue()
        Serializable constructor.
    • Method Detail

      • from

        public static <K,​T extends V,​V> KeyValue<K,​V> from​(K key,
                                                                             Optional<T> optional)
        Creates a KeyValue from a key and an Optional. The resulting value contains the value from the Optional if a value is present. Value is empty if the Optional is empty.
        Parameters:
        key - the key, must not be null.
        optional - the optional. May be empty but never null.
        Returns:
        the KeyValue
      • fromNullable

        public static <K,​T extends V,​V> KeyValue<K,​V> fromNullable​(K key,
                                                                                     T value)
        Creates a KeyValue from a key andvalue. The resulting value contains the value if the value is not null.
        Parameters:
        key - the key, must not be null.
        value - the value. May be null.
        Returns:
        the KeyValue
      • empty

        public static <K,​V> KeyValue<K,​V> empty​(K key)
        Returns an empty KeyValue instance with the key set. No value is present for this instance.
        Parameters:
        key - the key, must not be null.
        Returns:
        the KeyValue
      • just

        public static <K,​T extends V,​V> KeyValue<K,​V> just​(K key,
                                                                             T value)
        Creates a KeyValue from a key and value. The resulting value contains the value.
        Parameters:
        key - the key. Must not be null.
        value - the value. Must not be null.
        Returns:
        the KeyValue
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Value<V>
      • getKey

        public K getKey()
        Returns:
        the key
      • map

        public <R> KeyValue<K,​R> map​(Function<? super V,​? extends R> mapper)
        Returns a KeyValue consisting of the results of applying the given function to the value of this element. Mapping is performed only if a value is present.
        Overrides:
        map in class Value<V>
        Type Parameters:
        R - The element type of the new KeyValue
        Parameters:
        mapper - a stateless function to apply to each element
        Returns:
        the new KeyValue