Class TypedMap

java.lang.Object
play.libs.typedmap.TypedMap

public final class TypedMap extends Object
A TypedMap is an immutable map containing typed values. Each entry is associated with a TypedKey that can be used to look up the value. A TypedKey also defines the type of the value, e.g. a TypedKey<String> would be associated with a String value.

Instances of this class are created with the empty() method.

The elements inside TypedMaps cannot be enumerated. This is a decision designed to enforce modularity. It's not possible to accidentally or intentionally access a value in a TypedMap without holding the corresponding TypedKey.

  • Constructor Details

    • TypedMap

      public TypedMap(play.api.libs.typedmap.TypedMap underlying)
  • Method Details

    • asScala

      public play.api.libs.typedmap.TypedMap asScala()
      Returns:
      the underlying Scala TypedMap which this instance wraps.
    • get

      public <A> A get(TypedKey<A> key)
      Get a value from the map, throwing an exception if it is not present.
      Type Parameters:
      A - The type of value to retrieve.
      Parameters:
      key - The key for the value to retrieve.
      Returns:
      The value, if it is present in the map.
      Throws:
      NoSuchElementException - If the value isn't present in the map.
    • getOptional

      public <A> Optional<A> getOptional(TypedKey<A> key)
      Get a value from the map, returning an empty Optional if it is not present.
      Type Parameters:
      A - The type of value to retrieve.
      Parameters:
      key - The key for the value to retrieve.
      Returns:
      An Optional, with the value present if it is in the map.
    • containsKey

      public boolean containsKey(TypedKey<?> key)
      Check if the map contains a value with the given key.
      Parameters:
      key - The key to check for.
      Returns:
      True if the value is present, false otherwise.
    • put

      public <A> TypedMap put(TypedKey<A> key, A value)
      Update the map with the given key and value, returning a new instance of the map.
      Type Parameters:
      A - The type of value.
      Parameters:
      key - The key to set.
      value - The value to use.
      Returns:
      A new instance of the map with the new entry added.
    • putAll

      public TypedMap putAll(TypedEntry<?> e1)
      Update the map with one entry, returning a new instance of the map.
      Parameters:
      e1 - The new entry to add to the map.
      Returns:
      A new instance of the map with the new entry added.
    • putAll

      public TypedMap putAll(TypedEntry<?> e1, TypedEntry<?> e2)
      Update the map with two entries, returning a new instance of the map.
      Parameters:
      e1 - The first new entry to add to the map.
      e2 - The second new entry to add to the map.
      Returns:
      A new instance of the map with the new entries added.
    • putAll

      public TypedMap putAll(TypedEntry<?> e1, TypedEntry<?> e2, TypedEntry<?> e3)
      Update the map with three entries, returning a new instance of the map.
      Parameters:
      e1 - The first new entry to add to the map.
      e2 - The second new entry to add to the map.
      e3 - The third new entry to add to the map.
      Returns:
      A new instance of the map with the new entries added.
    • putAll

      public TypedMap putAll(TypedEntry<?>... entries)
      Update the map with several entries, returning a new instance of the map.
      Parameters:
      entries - The new entries to add to the map.
      Returns:
      A new instance of the map with the new entries added.
    • putAll

      public TypedMap putAll(List<TypedEntry<?>> entries)
      Update the map with several entries, returning a new instance of the map.
      Parameters:
      entries - The new entries to add to the map.
      Returns:
      A new instance of the map with the new entries added.
    • remove

      public TypedMap remove(TypedKey<?> k1)
      Removes a key from the map, returning a new instance of the map.
      Parameters:
      k1 - The key to remove.
      Returns:
      A new instance of the map with the entry removed.
    • remove

      public TypedMap remove(TypedKey<?> k1, TypedKey<?> k2)
      Removes two keys from the map, returning a new instance of the map.
      Parameters:
      k1 - The first key to remove.
      k2 - The second key to remove.
      Returns:
      A new instance of the map with the entries removed.
    • remove

      public TypedMap remove(TypedKey<?> k1, TypedKey<?> k2, TypedKey<?> k3)
      Removes three keys from the map, returning a new instance of the map.
      Parameters:
      k1 - The first key to remove.
      k2 - The second key to remove.
      k3 - The third key to remove.
      Returns:
      A new instance of the map with the entries removed.
    • remove

      public TypedMap remove(TypedKey<?>... keys)
      Removes keys from the map, returning a new instance of the map.
      Parameters:
      keys - The keys to remove.
      Returns:
      A new instance of the map with the entries removed.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • empty

      public static TypedMap empty()
      Returns:
      the empty TypedMap instance.
    • create

      public static TypedMap create(TypedEntry<?> e1)
      Parameters:
      e1 - typed entry
      Returns:
      a newly built TypedMap from a entry of key and value.
    • create

      public static TypedMap create(TypedEntry<?> e1, TypedEntry<?> e2)
      Parameters:
      e1 - first typed entry
      e2 - second typed entry
      Returns:
      a newly built TypedMap from a two entries of keys and values.
    • create

      public static TypedMap create(TypedEntry<?> e1, TypedEntry<?> e2, TypedEntry<?> e3)
      Parameters:
      e1 - first typed entry
      e2 - second typed entry
      e3 - third typed entry
      Returns:
      a newly built TypedMap from a three entries of keys and values.
    • create

      public static TypedMap create(TypedEntry<?>... entries)
      Parameters:
      entries - the list of typed entries
      Returns:
      a newly built TypedMap from a list of keys and values.