Interface LocalMap<K,​V>

  • All Superinterfaces:
    Map<K,​V>

    public interface LocalMap<K,​V>
    extends Map<K,​V>
    Local maps can be used to share data safely in a single Vert.x instance.

    By default the map allows immutable keys and values. Custom keys and values should implement Shareable interface. The map returns their copies.

    This ensures there is no shared access to mutable state from different threads (e.g. different event loops) in the Vert.x instance, and means you don't have to protect access to that state using synchronization or locks.

    Since the version 3.4, this class extends the Map interface. However some methods are only accessible in Java.

    Author:
    Tim Fox
    See Also:
    Shareable
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void clear()
      Clear all entries in the map
      void close()
      Close and release the map
      V compute​(K key, java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
      V computeIfAbsent​(K key, java.util.function.Function<? super K,​? extends V> mappingFunction)
      If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
      V computeIfPresent​(K key, java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
      If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
      boolean containsKey​(Object key)
      Returns true if this map contains a mapping for the specified key.
      boolean containsValue​(Object value)
      Returns @{code true} if this map maps one or more keys to the specified value.
      Set<Map.Entry<K,​V>> entrySet()
      Returns a Set view of the mappings contained in this map.
      void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)
      Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
      V get​(Object key)
      Get a value from the map
      V getOrDefault​(Object key, V defaultValue)
      Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
      boolean isEmpty()  
      Set<K> keySet()  
      V merge​(K key, V value, java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
      If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
      V put​(K key, V value)
      Put an entry in the map
      void putAll​(Map<? extends K,​? extends V> m)
      Copies all of the mappings from the specified map to this map.
      V putIfAbsent​(K key, V value)
      Put the entry only if there is no existing entry for that key
      V remove​(Object key)
      Remove an entry from the map
      boolean remove​(Object key, Object value)
      Removes the entry for the specified key only if it is currently mapped to the specified value.
      boolean removeIfPresent​(K key, V value)
      Remove the entry only if there is an entry with the specified key and value.
      V replace​(K key, V value)
      Replace the entry only if there is an existing entry with the key
      boolean replace​(K key, V oldValue, V newValue)
      Replaces the entry for the specified key only if currently mapped to the specified value.
      void replaceAll​(java.util.function.BiFunction<? super K,​? super V,​? extends V> function)
      Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
      boolean replaceIfPresent​(K key, V oldValue, V newValue)
      Replace the entry only if there is an existing entry with the specified key and value.
      int size()
      Get the size of the map
      Collection<V> values()  
    • Method Detail

      • get

        V get​(Object key)
        Get a value from the map
        Specified by:
        get in interface Map<K,​V>
        Parameters:
        key - the key
        Returns:
        the value, or null if none
      • put

        V put​(K key,
              V value)
        Put an entry in the map
        Specified by:
        put in interface Map<K,​V>
        Parameters:
        key - the key
        value - the value
        Returns:
        return the old value, or null if none
      • remove

        V remove​(Object key)
        Remove an entry from the map
        Specified by:
        remove in interface Map<K,​V>
        Parameters:
        key - the key
        Returns:
        the old value
      • clear

        void clear()
        Clear all entries in the map
        Specified by:
        clear in interface Map<K,​V>
      • size

        int size()
        Get the size of the map
        Specified by:
        size in interface Map<K,​V>
        Returns:
        the number of entries in the map
      • isEmpty

        boolean isEmpty()
        Specified by:
        isEmpty in interface Map<K,​V>
        Returns:
        true if there are zero entries in the map
      • putIfAbsent

        V putIfAbsent​(K key,
                      V value)
        Put the entry only if there is no existing entry for that key
        Specified by:
        putIfAbsent in interface Map<K,​V>
        Parameters:
        key - the key
        value - the value
        Returns:
        the old value or null, if none
      • removeIfPresent

        boolean removeIfPresent​(K key,
                                V value)
        Remove the entry only if there is an entry with the specified key and value.

        This method is the poyglot version of remove(Object, Object).

        Parameters:
        key - the key
        value - the value
        Returns:
        true if removed
      • replaceIfPresent

        boolean replaceIfPresent​(K key,
                                 V oldValue,
                                 V newValue)
        Replace the entry only if there is an existing entry with the specified key and value.

        This method is the polyglot version of replace(Object, Object, Object).

        Parameters:
        key - the key
        oldValue - the old value
        newValue - the new value
        Returns:
        true if removed
      • replace

        V replace​(K key,
                  V value)
        Replace the entry only if there is an existing entry with the key
        Specified by:
        replace in interface Map<K,​V>
        Parameters:
        key - the key
        value - the new value
        Returns:
        the old value
      • close

        void close()
        Close and release the map
      • keySet

        Set<K> keySet()
        Specified by:
        keySet in interface Map<K,​V>
        Returns:
        the set of keys in the map
      • values

        Collection<V> values()
        Specified by:
        values in interface Map<K,​V>
        Returns:
        the set of values in the map
      • compute

        V compute​(K key,
                  java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

        If the function returns null, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Specified by:
        compute in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        remappingFunction - the function to compute a value
        Returns:
        the new value associated with the specified key, or null if none
      • computeIfAbsent

        V computeIfAbsent​(K key,
                          java.util.function.Function<? super K,​? extends V> mappingFunction)
        If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.

        If the function returns null no mapping is recorded. If the function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded.

        Specified by:
        computeIfAbsent in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        mappingFunction - the function to compute a value
        Returns:
        the current (existing or computed) value associated with the specified key, or null if the computed value is null
      • computeIfPresent

        V computeIfPresent​(K key,
                           java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

        If the function returns null, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Specified by:
        computeIfPresent in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        remappingFunction - the function to compute a value
        Returns:
        the new value associated with the specified key, or null if none
      • containsKey

        boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key.
        Specified by:
        containsKey in interface Map<K,​V>
        Parameters:
        key - key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
      • containsValue

        boolean containsValue​(Object value)
        Returns @{code true} if this map maps one or more keys to the specified value.
        Specified by:
        containsValue in interface Map<K,​V>
        Parameters:
        value - value whose presence in this map is to be tested
        Returns:
      • entrySet

        Set<Map.Entry<K,​V>> entrySet()
        Returns a Set view of the mappings contained in this map.

        Unlike the default Map implementation, the set is not backed by the map. So changes made to the map are not reflected in the set. Entries added or remove to the set are not reflected to the map. In addition, the entries are not modifiable (Map.Entry.setValue(Object) is not supported).

        Specified by:
        entrySet in interface Map<K,​V>
        Returns:
        a set view of the mappings contained in this map
      • forEach

        void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)
        Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.

        Exceptions thrown by the action are relayed to the caller.

        Specified by:
        forEach in interface Map<K,​V>
        Parameters:
        action - The action to be performed for each entry
      • getOrDefault

        V getOrDefault​(Object key,
                       V defaultValue)
        Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
        Specified by:
        getOrDefault in interface Map<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        defaultValue - the default mapping of the key
        Returns:
        the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
      • merge

        V merge​(K key,
                V value,
                java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key.
        Specified by:
        merge in interface Map<K,​V>
        Parameters:
        key - key with which the resulting value is to be associated
        value - the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key
        remappingFunction - the function to recompute a value if present
        Returns:
        the new value associated with the specified key, or null if no value is associated with the key
      • putAll

        void putAll​(Map<? extends K,​? extends V> m)
        Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
        Specified by:
        putAll in interface Map<K,​V>
        Parameters:
        m - mappings to be stored in this map
      • remove

        boolean remove​(Object key,
                       Object value)
        Removes the entry for the specified key only if it is currently mapped to the specified value.
        Specified by:
        remove in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is associated
        value - value expected to be associated with the specified key
        Returns:
        true if the value was removed
      • replace

        boolean replace​(K key,
                        V oldValue,
                        V newValue)
        Replaces the entry for the specified key only if currently mapped to the specified value.
        Specified by:
        replace in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is associated
        oldValue - value expected to be associated with the specified key
        newValue - value to be associated with the specified key
        Returns:
        true if the value was replaced
      • replaceAll

        void replaceAll​(java.util.function.BiFunction<? super K,​? super V,​? extends V> function)
        Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.
        Specified by:
        replaceAll in interface Map<K,​V>
        Parameters:
        function - the function to apply to each entry