Class ImmutableMaps
- java.lang.Object
-
- io.atlassian.fugue.extras.ImmutableMaps
-
public class ImmutableMaps extends java.lang.ObjectProvides some utility methods to convert Iterables to ImmutableMap, and to transform Maps.- Since:
- 2.0
-
-
Constructor Summary
Constructors Modifier Constructor Description privateImmutableMaps()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K1,K2,V1,V2>
com.google.common.collect.ImmutableMap<K2,V2>collect(java.util.Map<K1,V1> from, java.util.function.Function<? super K1,Option<K2>> keyPartial, java.util.function.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(java.util.Map<K1,V1> from, java.util.function.Function<java.util.Map.Entry<K1,V1>,Option<java.util.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(java.util.Map<K1,V> from, java.util.function.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(java.util.Map<K,V1> from, java.util.function.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(java.lang.Iterable<V> from, java.util.function.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>
java.util.function.BiFunction<K,V,java.util.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.static <K,V>
com.google.common.collect.ImmutableMap<K,V>mapTo(java.lang.Iterable<K> from, java.util.function.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(java.lang.Iterable<java.util.Map.Entry<K,V>> from)Builds an immutable map from the given iterable ofMap.Entry.static <T,K,V>
com.google.common.collect.ImmutableMap<K,V>toMap(java.lang.Iterable<T> from, java.util.function.Function<? super T,? extends K> keyTransformer, java.util.function.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(java.util.Map<K1,V1> from, java.util.function.Function<? super K1,? extends K2> keyTransformer, java.util.function.Function<? super V1,? extends V2> valueTransformer)Returns an immutable map that applies the keyTransformer and valueTransformer functions to each entry offromMap.static <K1,K2,V1,V2>
com.google.common.collect.ImmutableMap<K2,V2>transform(java.util.Map<K1,V1> from, java.util.function.Function<java.util.Map.Entry<K1,V1>,java.util.Map.Entry<K2,V2>> function)Returns an immutable map that applies function to each entry offromMap.static <K1,K2,V>
com.google.common.collect.ImmutableMap<K2,V>transformKey(java.util.Map<K1,V> from, java.util.function.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(java.util.Map<K,V1> from, java.util.function.Function<? super V1,? extends V2> valueTransformer)Returns an immutable map that applies valueTransformer to the value of each entry of the source map.
-
-
-
Method Detail
-
mapEntry
public static <K,V> java.util.function.BiFunction<K,V,java.util.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 typeV- 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(java.lang.Iterable<java.util.Map.Entry<K,V>> from)
Builds an immutable map from the given iterable ofMap.Entry.Any
nullentries will be filtered out. Additionally, any entries containingnullkey or value will also be filtered out. If multiple entries return the same key,IllegalArgumentExceptionwill be thrown.- Type Parameters:
K- the key typeV- 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(java.lang.Iterable<T> from, java.util.function.Function<? super T,? extends K> keyTransformer, java.util.function.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.nullvalue is allowed and will be passed to the keyTransformer and valueTransformer. However, if either the keyTransformer or the valueTransformer returnsnullfor an entry, the entry is ignored. If keyTransformer returns the same key for multiple entries,IllegalArgumentExceptionwill be thrown.- Type Parameters:
T- the input typeK- the key typeV- the value type- Parameters:
from- the iterable we use as the sourcekeyTransformer- transform keysvalueTransformer- transform values- Returns:
- the transformed map
-
mapBy
public static <K,V> com.google.common.collect.ImmutableMap<K,V> mapBy(java.lang.Iterable<V> from, java.util.function.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.nullvalue is allowed but will be ignored. If keyTransformer returns the same key for multiple entries,IllegalArgumentExceptionwill be thrown.- Type Parameters:
K- the key typeV- the value type- Parameters:
from- the iterable we use as the sourcekeyTransformer- transform keys- Returns:
- the transformed map
-
mapTo
public static <K,V> com.google.common.collect.ImmutableMap<K,V> mapTo(java.lang.Iterable<K> from, java.util.function.Function<? super K,? extends V> valueTransformer)Builds an immutable map from the given iterable and compute the value by applying the valueTransformer.nullvalue is allowed but will be ignored. If there are duplicate entries in the iterable,IllegalArgumentExceptionwill be thrown.- Type Parameters:
K- the key typeV- the value type- Parameters:
from- the iterable we use as the sourcevalueTransformer- transform values- Returns:
- the transformed map
-
transform
public static <K1,K2,V1,V2> com.google.common.collect.ImmutableMap<K2,V2> transform(java.util.Map<K1,V1> from, java.util.function.Function<java.util.Map.Entry<K1,V1>,java.util.Map.Entry<K2,V2>> function)Returns an immutable map that applies function to each entry offromMap. Ifnullis returned by the function for any entry, or if an entry returned by the function contains anullkey or value, that entry is discarded in the result. If the function returns entries with the same key for multiple entries,IllegalArgumentExceptionwill be thrown.- Type Parameters:
K1- the input key typeK2- the output key typeV1- the input value typeV2- the output value type- Parameters:
from- the map we use as the sourcefunction- transform keys and values- Returns:
- the transformed map
-
transform
public static <K1,K2,V1,V2> com.google.common.collect.ImmutableMap<K2,V2> transform(java.util.Map<K1,V1> from, java.util.function.Function<? super K1,? extends K2> keyTransformer, java.util.function.Function<? super V1,? extends V2> valueTransformer)Returns an immutable map that applies the keyTransformer and valueTransformer functions to each entry offromMap. If for any entry, anullkey or value is returned, that entry is discarded in the result. If the keyTransformer function returns the same key for multiple entries,IllegalArgumentExceptionwill be thrown.- Type Parameters:
K1- the input key typeK2- the output key typeV1- the input value typeV2- the output value type- Parameters:
from- the map we use as the sourcekeyTransformer- transform keysvalueTransformer- transform values- Returns:
- the transformed map
-
transformKey
public static <K1,K2,V> com.google.common.collect.ImmutableMap<K2,V> transformKey(java.util.Map<K1,V> from, java.util.function.Function<? super K1,? extends K2> keyTransformer)Returns an immutable map that applies keyTransformer to the key of each entry of the source map. Ifnullis returned by the keyTransformer for any entry, that entry is discarded in the result. If an entry contains anullvalue, it will also be discarded in the result. If thefunctionreturns the same result key for multiple keys,IllegalArgumentExceptionwill be thrown.- Type Parameters:
K1- the input key typeK2- the output key typeV- the value type- Parameters:
from- the map we use as the sourcekeyTransformer- transform keys- Returns:
- the transformed map
-
transformValue
public static <K,V1,V2> com.google.common.collect.ImmutableMap<K,V2> transformValue(java.util.Map<K,V1> from, java.util.function.Function<? super V1,? extends V2> valueTransformer)Returns an immutable map that applies valueTransformer to the value of each entry of the source map. Ifnullis returned by the valueTransformer for any entry, that entry is discarded in the result. If an entry contains anullkey, it will also be discarded in the result.- Type Parameters:
K- the key typeV1- the input value typeV2- the output value type- Parameters:
from- the iterable we use as the sourcevalueTransformer- transform values- Returns:
- the transformed map
-
collect
public static <K1,K2,V1,V2> com.google.common.collect.ImmutableMap<K2,V2> collect(java.util.Map<K1,V1> from, java.util.function.Function<java.util.Map.Entry<K1,V1>,Option<java.util.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 typeK2- the output key typeV1- the input value typeV2- the output value type- Parameters:
from- the iterable we use as the sourcepartial- transform and select entries- Returns:
- the transformed map
-
collect
public static <K1,K2,V1,V2> com.google.common.collect.ImmutableMap<K2,V2> collect(java.util.Map<K1,V1> from, java.util.function.Function<? super K1,Option<K2>> keyPartial, java.util.function.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 typeK2- the output key typeV1- the input value typeV2- the output value type- Parameters:
from- the iterable we use as the sourcekeyPartial- transform and collect keysvaluePartial- transform and collect values- Returns:
- the transformed map
-
collectByKey
public static <K1,K2,V> com.google.common.collect.ImmutableMap<K2,V> collectByKey(java.util.Map<K1,V> from, java.util.function.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 typeK2- the output key typeV- the value type- Parameters:
from- the iterable we use as the sourcekeyPartial- transform and collect keys- Returns:
- the transformed map
-
collectByValue
public static <K,V1,V2> com.google.common.collect.ImmutableMap<K,V2> collectByValue(java.util.Map<K,V1> from, java.util.function.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 typeV1- the input value typeV2- the output value type- Parameters:
from- the iterable we use as the sourcevaluePartial- transform and collect values- Returns:
- the transformed map
-
-