Class NameValueMap<T>

Object
NameValueMap<T>
Type Parameters:
T - element-type in NameValue instances.
All Implemented Interfaces:
Iterable<NameValue<T>>, NamedProvider<T>

public class NameValueMap<T> extends Object implements Iterable<NameValue<T>>, NamedProvider<T>
Builds a mapping from names to values, given NameValue instances.

Each name in NameValue should be unique.

This is similar as a standard Map but additionally:

Author:
Owen Feehan
  • Constructor Details

    • NameValueMap

      public NameValueMap(Iterable<? extends NameValue<T>> list)
      Creates and populates with elements from an Iterable.
      Parameters:
      list - the elements to populate with.
    • NameValueMap

      public NameValueMap()
  • Method Details

    • keys

      public Set<String> keys()
      Description copied from interface: NamedProvider
      Returns a set of keys associated with the provider.

      There's no guarantee that it refers to all valid keys.

      Specified by:
      keys in interface NamedProvider<T>
      Returns:
      a set of all keys associated with the provider.
    • getOptional

      public Optional<T> getOptional(String key)
      Description copied from interface: NamedProvider
      Retrieves the item if it exists, or returns Optional.empty() if it doesn't exist.

      Note that a 'key' might still throw an exception for another reason (but never because a particular key is absent).

      Specified by:
      getOptional in interface NamedProvider<T>
      Parameters:
      key - a unique name for the item.
      Returns:
      the item, if it exists, otherwise Optional.empty().
    • iterator

      public Iterator<NameValue<T>> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • add

      public void add(String name, T value)
      Adds an element.
      Parameters:
      name - the name that is added.
      value - the value that is added.
    • add

      public void add(NameValue<T> value)
      Adds an element.
      Parameters:
      value - the name and value that is added, reusing the existing object.
    • removeIfExists

      public void removeIfExists(T element)
      Removes an element from the set, if it exists.

      If the element doesn't exist, nothing happens.

      Parameters:
      element - the element to remove, if it exists.
    • size

      public int size()
      The number of elements in the set.
      Returns:
      the number of elements.
    • stream

      public Stream<NameValue<T>> stream()
      Exposes the elements in the set as a stream.
      Returns:
      a newly created stream of all elements in the set.