Class ImmutableMaps

java.lang.Object
io.atlassian.fugue.extras.ImmutableMaps

public class ImmutableMaps extends Object
Provides some utility methods to convert Iterables to ImmutableMap, and to transform Maps.
Since:
2.0
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K1, K2, V1, V2>
    com.google.common.collect.ImmutableMap<K2,V2>
    collect(Map<K1,V1> from, Function<? super K1,Option<K2>> keyPartial, Function<? super V1,Option<V2>> valuePartial)
    Filters and maps (aka transforms) the source map.
    static <K1, K2, V1, V2>
    com.google.common.collect.ImmutableMap<K2,V2>
    collect(Map<K1,V1> from, Function<Map.Entry<K1,V1>,Option<Map.Entry<K2,V2>>> partial)
    Filters and maps (aka transforms) the source map.
    static <K1, K2, V>
    com.google.common.collect.ImmutableMap<K2,V>
    collectByKey(Map<K1,V> from, Function<? super K1,Option<K2>> keyPartial)
    Filters and maps (aka transforms) the source map.
    static <K, V1, V2>
    com.google.common.collect.ImmutableMap<K,V2>
    collectByValue(Map<K,V1> from, Function<? super V1,Option<V2>> valuePartial)
    Filters and maps (aka transforms) the source map.
    static <K, V> com.google.common.collect.ImmutableMap<K,V>
    mapBy(Iterable<V> from, Function<? super V,? extends K> keyTransformer)
    Builds an immutable map that is keyed by the result of applying keyTransformer to each element of the given iterable of values.
    static <K, V> BiFunction<K,V,Map.Entry<K,V>>
    Returns a function that takes a key of type K and a value of type V and returns a Map entry.
    static <K, V> com.google.common.collect.ImmutableMap<K,V>
    mapTo(Iterable<K> from, Function<? super K,? extends V> valueTransformer)
    Builds an immutable map from the given iterable and compute the value by applying the valueTransformer.
    static <K, V> com.google.common.collect.ImmutableMap<K,V>
    toMap(Iterable<Map.Entry<K,V>> from)
    Builds an immutable map from the given iterable of Map.Entry.
    static <T, K, V> com.google.common.collect.ImmutableMap<K,V>
    toMap(Iterable<T> from, Function<? super T,? extends K> keyTransformer, Function<? super T,? extends V> valueTransformer)
    Builds an immutable map from the given iterable, with key derived from the application of the iterable to the keyTransformer, and value derived from the application of the iterable to the valueTransformer.
    static <K1, K2, V1, V2>
    com.google.common.collect.ImmutableMap<K2,V2>
    transform(Map<K1,V1> from, Function<? super K1,? extends K2> keyTransformer, Function<? super V1,? extends V2> valueTransformer)
    Returns an immutable map that applies the keyTransformer and valueTransformer functions to each entry of fromMap.
    static <K1, K2, V1, V2>
    com.google.common.collect.ImmutableMap<K2,V2>
    transform(Map<K1,V1> from, Function<Map.Entry<K1,V1>,Map.Entry<K2,V2>> function)
    Returns an immutable map that applies function to each entry of fromMap.
    static <K1, K2, V>
    com.google.common.collect.ImmutableMap<K2,V>
    transformKey(Map<K1,V> from, Function<? super K1,? extends K2> keyTransformer)
    Returns an immutable map that applies keyTransformer to the key of each entry of the source map.
    static <K, V1, V2>
    com.google.common.collect.ImmutableMap<K,V2>
    transformValue(Map<K,V1> from, Function<? super V1,? extends V2> valueTransformer)
    Returns an immutable map that applies valueTransformer to the value of each entry of the source map.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ImmutableMaps

      private ImmutableMaps()
  • Method Details

    • mapEntry

      public static <K, V> BiFunction<K,V,Map.Entry<K,V>> mapEntry()
      Returns a function that takes a key of type K and a value of type V and returns a Map entry.
      Type Parameters:
      K - the key type
      V - the value type
      Returns:
      a function that takes a K and a V and return the corresponding Map entry
    • toMap

      public static <K, V> com.google.common.collect.ImmutableMap<K,V> toMap(Iterable<Map.Entry<K,V>> from)
      Builds an immutable map from the given iterable of Map.Entry.

      Any null entries will be filtered out. Additionally, any entries containing null key or value will also be filtered out. If multiple entries return the same key, IllegalArgumentException will be thrown.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      from - the iterable we use as the source
      Returns:
      the transformed map
    • toMap

      public static <T, K, V> com.google.common.collect.ImmutableMap<K,V> toMap(Iterable<T> from, Function<? super T,? extends K> keyTransformer, Function<? super T,? extends V> valueTransformer)
      Builds an immutable map from the given iterable, with key derived from the application of the iterable to the keyTransformer, and value derived from the application of the iterable to the valueTransformer.

      null value is allowed and will be passed to the keyTransformer and valueTransformer. However, if either the keyTransformer or the valueTransformer returns null for an entry, the entry is ignored. If keyTransformer returns the same key for multiple entries, IllegalArgumentException will be thrown.

      Type Parameters:
      T - the input type
      K - the key type
      V - the value type
      Parameters:
      from - the iterable we use as the source
      keyTransformer - transform keys
      valueTransformer - transform values
      Returns:
      the transformed map
    • mapBy

      public static <K, V> com.google.common.collect.ImmutableMap<K,V> mapBy(Iterable<V> from, Function<? super V,? extends K> keyTransformer)
      Builds an immutable map that is keyed by the result of applying keyTransformer to each element of the given iterable of values.

      null value is allowed but will be ignored. If keyTransformer returns the same key for multiple entries, IllegalArgumentException will be thrown.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      from - the iterable we use as the source
      keyTransformer - transform keys
      Returns:
      the transformed map
    • mapTo

      public static <K, V> com.google.common.collect.ImmutableMap<K,V> mapTo(Iterable<K> from, Function<? super K,? extends V> valueTransformer)
      Builds an immutable map from the given iterable and compute the value by applying the valueTransformer.

      null value is allowed but will be ignored. If there are duplicate entries in the iterable, IllegalArgumentException will be thrown.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      from - the iterable we use as the source
      valueTransformer - transform values
      Returns:
      the transformed map
    • transform

      public static <K1, K2, V1, V2> com.google.common.collect.ImmutableMap<K2,V2> transform(Map<K1,V1> from, Function<Map.Entry<K1,V1>,Map.Entry<K2,V2>> function)
      Returns an immutable map that applies function to each entry of fromMap. If null is returned by the function for any entry, or if an entry returned by the function contains a null key or value, that entry is discarded in the result. If the function returns entries with the same key for multiple entries, IllegalArgumentException will be thrown.
      Type Parameters:
      K1 - the input key type
      K2 - the output key type
      V1 - the input value type
      V2 - the output value type
      Parameters:
      from - the map we use as the source
      function - transform keys and values
      Returns:
      the transformed map
    • transform

      public static <K1, K2, V1, V2> com.google.common.collect.ImmutableMap<K2,V2> transform(Map<K1,V1> from, Function<? super K1,? extends K2> keyTransformer, Function<? super V1,? extends V2> valueTransformer)
      Returns an immutable map that applies the keyTransformer and valueTransformer functions to each entry of fromMap. If for any entry, a null key or value is returned, that entry is discarded in the result. If the keyTransformer function returns the same key for multiple entries, IllegalArgumentException will be thrown.
      Type Parameters:
      K1 - the input key type
      K2 - the output key type
      V1 - the input value type
      V2 - the output value type
      Parameters:
      from - the map we use as the source
      keyTransformer - transform keys
      valueTransformer - transform values
      Returns:
      the transformed map
    • transformKey

      public static <K1, K2, V> com.google.common.collect.ImmutableMap<K2,V> transformKey(Map<K1,V> from, Function<? super K1,? extends K2> keyTransformer)
      Returns an immutable map that applies keyTransformer to the key of each entry of the source map. If null is returned by the keyTransformer for any entry, that entry is discarded in the result. If an entry contains a null value, it will also be discarded in the result. If the function returns the same result key for multiple keys, IllegalArgumentException will be thrown.
      Type Parameters:
      K1 - the input key type
      K2 - the output key type
      V - the value type
      Parameters:
      from - the map we use as the source
      keyTransformer - transform keys
      Returns:
      the transformed map
    • transformValue

      public static <K, V1, V2> com.google.common.collect.ImmutableMap<K,V2> transformValue(Map<K,V1> from, Function<? super V1,? extends V2> valueTransformer)
      Returns an immutable map that applies valueTransformer to the value of each entry of the source map. If null is returned by the valueTransformer for any entry, that entry is discarded in the result. If an entry contains a null key, it will also be discarded in the result.
      Type Parameters:
      K - the key type
      V1 - the input value type
      V2 - the output value type
      Parameters:
      from - the iterable we use as the source
      valueTransformer - transform values
      Returns:
      the transformed map
    • collect

      public static <K1, K2, V1, V2> com.google.common.collect.ImmutableMap<K2,V2> collect(Map<K1,V1> from, Function<Map.Entry<K1,V1>,Option<Map.Entry<K2,V2>>> partial)
      Filters and maps (aka transforms) the source map. Applies the given partial function to each entry of the unfiltered map. If the application returns none, the entry will be left out; otherwise, the transformed entry contained in the Option will be added to the result map.
      Type Parameters:
      K1 - the input key type
      K2 - the output key type
      V1 - the input value type
      V2 - the output value type
      Parameters:
      from - the iterable we use as the source
      partial - transform and select entries
      Returns:
      the transformed map
    • collect

      public static <K1, K2, V1, V2> com.google.common.collect.ImmutableMap<K2,V2> collect(Map<K1,V1> from, Function<? super K1,Option<K2>> keyPartial, Function<? super V1,Option<V2>> valuePartial)
      Filters and maps (aka transforms) the source map. Applies the given partial key function and partial value function to the key and value of each entry of the unfiltered map. If either of the application returns none, the entry will be left out; otherwise, an entry of transformed key and transformed value contained in the options will be added to the result map.
      Type Parameters:
      K1 - the input key type
      K2 - the output key type
      V1 - the input value type
      V2 - the output value type
      Parameters:
      from - the iterable we use as the source
      keyPartial - transform and collect keys
      valuePartial - transform and collect values
      Returns:
      the transformed map
    • collectByKey

      public static <K1, K2, V> com.google.common.collect.ImmutableMap<K2,V> collectByKey(Map<K1,V> from, Function<? super K1,Option<K2>> keyPartial)
      Filters and maps (aka transforms) the source map. Applies the given partial key function to the key of each entry of the unfiltered map. If the application returns none, the entry will be left out; otherwise, an entry of transformed key contained in the option and the original value will be added to the result map.
      Type Parameters:
      K1 - the input key type
      K2 - the output key type
      V - the value type
      Parameters:
      from - the iterable we use as the source
      keyPartial - transform and collect keys
      Returns:
      the transformed map
    • collectByValue

      public static <K, V1, V2> com.google.common.collect.ImmutableMap<K,V2> collectByValue(Map<K,V1> from, Function<? super V1,Option<V2>> valuePartial)
      Filters and maps (aka transforms) the source map. Applies the given partial value function to the value of each entry of the unfiltered map. If the application returns none, the entry will be left out; otherwise, an entry of the original key and the transformed key contained in the option will be added to the result map.
      Type Parameters:
      K - the key type
      V1 - the input value type
      V2 - the output value type
      Parameters:
      from - the iterable we use as the source
      valuePartial - transform and collect values
      Returns:
      the transformed map