Top Description Constructors Methods
org.eclipse.xtext.xbase.lib

public Class MapExtensions

extends Object
Class Inheritance
Annotations
@GwtCompatible
Imports
java.util.Collections, .Map, .Map.Entry, .Objects, org.eclipse.xtext.xbase.lib.Functions.Function1, .Functions.Function2, .Procedures.Procedure2, .Procedures.Procedure3, org.eclipse.xtext.xbase.lib.internal.FunctionDelegate, .UnmodifiableMergingMapView, com.google.common.annotations.GwtCompatible, com.google.common.base.Predicate, com.google.common.collect.Iterables, .Maps

This is an extension library for maps.
Authors
Sebastian Zarnekow - Initial contribution and API, Stephane Galland - Add operators on map structures.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <K, V> Map<K, V>

Returns:

a filtered view on top of the original map. Never null.
filter
(Map<K, V>
the original map. May not be null.
original
,
final Functions.Function2<? super K, ? super V, Boolean>
the predicate. May not be null.
predicate
)

Returns a filtered live view on top of the original map.

public static <K, V> void
forEach(Map<K, V>
the map. May not be null.
map
,
Procedures.Procedure2<? super K, ? super V>
the procedure. May not be null.
procedure
)

Applies the given procedure for each key value pair of the given map.

public static <K, V> void
forEach(Map<K, V>
the map. May not be null.
map
,
Procedures.Procedure3<? super K, ? super V, ? super Integer>
the procedure. May not be null.
procedure
)

Applies the given procedure for each key value pair of the given map.

public static <K, V1, V2> Map<K, V2>

Returns:

a map with equal keys but transformed values. Never null.
mapValues
(Map<K, V1>
the original map. May not be null.
original
,
Functions.Function1<? super V1, ? extends V2>
the transformation. May not be null.
transformation
)

Returns a map that performs the given transformation for each value of original when requested.

public static <
type of the map keys.
K
,
type of the map values.
V
>
V

Returns:

the value previously associated to the key, or null if the key was not present in the map before the addition.
operator_add
(Map<K, V>
the map to update.
map
,
Pair<? extends K, ? extends V>
the entry (key, value) to add into the map.
entry
)

Add the given pair into the map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
void
operator_add(Map<K, V>
the map to update.
outputMap
,
Map<? extends K, ? extends V>
the entries to add.
inputMap
)

Add the given entries of the input map into the output map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

an immutable map with the content of the map and with the given entry.
operator_minus
(Map<K, V>
the map to consider.
left
,
final Pair<? extends K, ? extends V>
the entry (key, value) to remove from the map.
right
)

Remove the given pair from a given map for obtaining a new map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

the map with the content of the map except the key.
operator_minus
(Map<K, V>
the map to update.
map
,
final K
the key to remove.
key
)

Replies the elements of the given map except the pair with the given key.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

the map with the content of the left map except the pairs of the right map.
operator_minus
(Map<K, V>
the map to update.
left
,
final Map<? extends K, ? extends V>
the pairs to remove.
right
)

Replies the elements of the left map without the pairs in the right map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

the map with the content of the map except the pairs.
operator_minus
(Map<K, V>
the map to update.
map
,
final Iterable<?>
the keys of the pairs to remove.
keys
)

Replies the elements of the given map except the pairs with the given keys.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

an immutable map with the content of the map and with the given entry.
operator_plus
(Map<K, V>
the map to consider.
left
,
final Pair<? extends K, ? extends V>
the entry (key, value) to add into the map.
right
)

Add the given pair to a given map for obtaining a new map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

a map with the merged contents from the two maps.
operator_plus
(Map<K, V>
the left map.
left
,
Map<? extends K, ? extends V>
the right map.
right
)

Merge the two maps.

public static <
type of the map keys.
K
,
type of the map values.
V
>
V

Returns:

the removed value, or null if the key was not present in the map.
operator_remove
(Map<K, V>
the map to update.
map
,
K
the key to remove.
key
)

Remove a key from the given map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
boolean

Returns:

true if the pair was removed.
operator_remove
(Map<K, V>
the map to update.
map
,
Pair<? extends K, ? extends V>
the entry (key, value) to remove from the map.
entry
)

Remove the given pair into the map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
void
operator_remove(Map<K, V>
the map to update.
map
,
Iterable<? super K>
the keys of the pairs to remove.
keysToRemove
)

Remove pairs with the given keys from the map.

public static <
type of the map keys.
K
,
type of the map values.
V
>
Map<K, V>

Returns:

a map with the merged contents from the two maps.
union
(Map<? extends K, ? extends V>
the left map.
left
,
Map<? extends K, ? extends V>
the right map.
right
)

Merge the given maps.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

MapExtensionsback to summary
public MapExtensions()

Method Detail

filterback to summary
public static <K, V> Map<K, V> filter(Map<K, V> original, final Functions.Function2<? super K, ? super V, Boolean> predicate)

Returns a filtered live view on top of the original map. Changes to one affect the other.

The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the original map are reflected in the result map. The results supports removal if the original map supports removal.

Parameters
original:Map<K, V>

the original map. May not be null.

predicate:Functions.Function2<? super K, ? super V, Boolean>

the predicate. May not be null.

Returns:Map<K, V>

a filtered view on top of the original map. Never null.

forEachback to summary
public static <K, V> void forEach(Map<K, V> map, Procedures.Procedure2<? super K, ? super V> procedure)

Applies the given procedure for each key value pair of the given map.

Parameters
map:Map<K, V>

the map. May not be null.

procedure:Procedures.Procedure2<? super K, ? super V>

the procedure. May not be null.

forEachback to summary
public static <K, V> void forEach(Map<K, V> map, Procedures.Procedure3<? super K, ? super V, ? super Integer> procedure)

Applies the given procedure for each key value pair of the given map. The procedure takes the key, the value and a loop counter. If the counter would overflow, Integer#MAX_VALUE is returned for all subsequent pairs. The first pair is at index zero.

Parameters
map:Map<K, V>

the map. May not be null.

procedure:Procedures.Procedure3<? super K, ? super V, ? super Integer>

the procedure. May not be null.

mapValuesback to summary
public static <K, V1, V2> Map<K, V2> mapValues(Map<K, V1> original, Functions.Function1<? super V1, ? extends V2> transformation)

Returns a map that performs the given transformation for each value of original when requested.

The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the original map are reflected in the result map. The results supports removal if the original map supports removal.

Parameters
original:Map<K, V1>

the original map. May not be null.

transformation:Functions.Function1<? super V1, ? extends V2>

the transformation. May not be null.

Returns:Map<K, V2>

a map with equal keys but transformed values. Never null.

Annotations
@Pure
Since
2.4
operator_addback to summary
public static <K, V> V operator_add(Map<K, V> map, Pair<? extends K, ? extends V> entry)

Add the given pair into the map.

If the pair key already exists in the map, its value is replaced by the value in the pair, and the old value in the map is returned.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
map:Map<K, V>

the map to update.

entry:Pair<? extends K, ? extends V>

the entry (key, value) to add into the map.

Returns:V

the value previously associated to the key, or null if the key was not present in the map before the addition.

Annotations
@Inline:$1.put($2.getKey(), $2.getValue())
statementExpression:true
Since
2.15
operator_addback to summary
public static <K, V> void operator_add(Map<K, V> outputMap, Map<? extends K, ? extends V> inputMap)

Add the given entries of the input map into the output map.

If a key in the inputMap already exists in the outputMap, its value is replaced in the outputMap by the value from the inputMap.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
outputMap:Map<K, V>

the map to update.

inputMap:Map<? extends K, ? extends V>

the entries to add.

Annotations
@Inline:$1.putAll($2)
statementExpression:true
Since
2.15
operator_minusback to summary
public static <K, V> Map<K, V> operator_minus(Map<K, V> left, final Pair<? extends K, ? extends V> right)

Remove the given pair from a given map for obtaining a new map.

If the given key is inside the map, but is not mapped to the given value, the map will not be changed.

The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
left:Map<K, V>

the map to consider.

right:Pair<? extends K, ? extends V>

the entry (key, value) to remove from the map.

Returns:Map<K, V>

an immutable map with the content of the map and with the given entry.

Annotations
@Pure
Exceptions
IllegalArgumentException:
- when the right operand key exists in the left operand.
Since
2.15
operator_minusback to summary
public static <K, V> Map<K, V> operator_minus(Map<K, V> map, final K key)

Replies the elements of the given map except the pair with the given key.

The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
map:Map<K, V>

the map to update.

key:K

the key to remove.

Returns:Map<K, V>

the map with the content of the map except the key.

Annotations
@Pure
Since
2.15
operator_minusback to summary
public static <K, V> Map<K, V> operator_minus(Map<K, V> left, final Map<? extends K, ? extends V> right)

Replies the elements of the left map without the pairs in the right map. If the pair's values differ from the value within the map, the map entry is not removed.

The difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.

Since this method uses HashMap instances internally, the keys of the supplied maps must be well-behaved with respect to Object#equals and Object#hashCode.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
left:Map<K, V>

the map to update.

right:Map<? extends K, ? extends V>

the pairs to remove.

Returns:Map<K, V>

the map with the content of the left map except the pairs of the right map.

Annotations
@Pure
Since
2.15
operator_minusback to summary
public static <K, V> Map<K, V> operator_minus(Map<K, V> map, final Iterable<?> keys)

Replies the elements of the given map except the pairs with the given keys.

The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
map:Map<K, V>

the map to update.

keys:Iterable<?>

the keys of the pairs to remove.

Returns:Map<K, V>

the map with the content of the map except the pairs.

Annotations
@Pure
Since
2.15
operator_plusback to summary
public static <K, V> Map<K, V> operator_plus(Map<K, V> left, final Pair<? extends K, ? extends V> right)

Add the given pair to a given map for obtaining a new map.

The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
left:Map<K, V>

the map to consider.

right:Pair<? extends K, ? extends V>

the entry (key, value) to add into the map.

Returns:Map<K, V>

an immutable map with the content of the map and with the given entry.

Annotations
@Pure
@Inline:$3.union($1, $4.singletonMap($2.getKey(), $2.getValue()))
imported:MapExtensions, Collections
Exceptions
IllegalArgumentException:
- when the right operand key exists in the left operand.
Since
2.15
operator_plusback to summary
public static <K, V> Map<K, V> operator_plus(Map<K, V> left, Map<? extends K, ? extends V> right)

Merge the two maps.

The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

If a key exists in the left and right operands, the value in the right operand is preferred.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
left:Map<K, V>

the left map.

right:Map<? extends K, ? extends V>

the right map.

Returns:Map<K, V>

a map with the merged contents from the two maps.

Annotations
@Pure
@Inline:$3.union($1, $2)
imported:MapExtensions
Exceptions
IllegalArgumentException:
- when a right operand key exists in the left operand.
Since
2.15
operator_removeback to summary
public static <K, V> V operator_remove(Map<K, V> map, K key)

Remove a key from the given map.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
map:Map<K, V>

the map to update.

key:K

the key to remove.

Returns:V

the removed value, or null if the key was not present in the map.

Annotations
@Inline:$1.remove($2)
statementExpression:true
Since
2.15
operator_removeback to summary
public static <K, V> boolean operator_remove(Map<K, V> map, Pair<? extends K, ? extends V> entry)

Remove the given pair into the map.

If the given key is inside the map, but is not mapped to the given value, the map will not be changed.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
map:Map<K, V>

the map to update.

entry:Pair<? extends K, ? extends V>

the entry (key, value) to remove from the map.

Returns:boolean

true if the pair was removed.

Annotations
@Inline:$1.remove($2.getKey(), $2.getValue())
statementExpression:true
Since
2.15
operator_removeback to summary
public static <K, V> void operator_remove(Map<K, V> map, Iterable<? super K> keysToRemove)

Remove pairs with the given keys from the map.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
map:Map<K, V>

the map to update.

keysToRemove:Iterable<? super K>

the keys of the pairs to remove.

Since
2.15
unionback to summary
public static <K, V> Map<K, V> union(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right)

Merge the given maps.

The replied map is a view on the given two maps. If a key exists in the two maps, the replied value is the value of the right operand.

Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.

The replied map is unmodifiable.

Parameters
<K>
type of the map keys.
<V>
type of the map values.
left:Map<? extends K, ? extends V>

the left map.

right:Map<? extends K, ? extends V>

the right map.

Returns:Map<K, V>

a map with the merged contents from the two maps.

Annotations
@Pure
@Inline:(new $3<$5, $6>($1, $2))
imported:UnmodifiableMergingMapView
constantExpression:true
Since
2.15