Package com.atlassian.jira.util.collect
Class MapBuilder<K,V>
java.lang.Object
com.atlassian.jira.util.collect.MapBuilder<K,V>
- Type Parameters:
K- the key typeV- the value type
Utility for easily creating Maps of all standard types. If your map does not need to
contain
null as either a key or a value, then ImmutableMap.builder()
should be preferred.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionMapBuilder<K,V> Add a key/value.MapBuilder<K,V> Add all the entries contained in the map.MapBuilder<K,V> addIfValueNotNull(K key, V value) Add a key/value only if the value is notnull.static <K,V> Map<K, V> build(K key, V value) Returns an immutable Map containing the given key-value pair.static <K,V> Map<K, V> build(K key1, V value1, K key2, V value2) Returns an immutable Map containing the given key-value pairs.static <K,V> Map<K, V> build(K key1, V value1, K key2, V value2, K key3, V value3) Returns an immutable Map containing the given key-value pairs.static <K,V> Map<K, V> build(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) Returns an immutable Map containing the given key-value pairs.static <K,V> Map<K, V> emptyMap()Static factory method for creating an empty map.static <K,V> MapBuilder<K, V> Static factory method for creating a freshMapBuilder.static <K,V> MapBuilder<K, V> newBuilder(Map<? extends K, ? extends V> map) Static factory method for creating a freshMapBuilderbased on the contents of a source Map.static <K,V> MapBuilder<K, V> newBuilder(K key, V value) Static factory method for creating a freshMapBuilderand adding the given key/value pair.static <K,V> MapBuilder<K, V> newBuilder(K key1, V value1, K key2, V value2) Static factory method for creating a freshMapBuilderand adding the given key/value pairs.static <K,V> MapBuilder<K, V> newBuilder(K key1, V value1, K key2, V value2, K key3, V value3) Static factory method for creating a freshMapBuilderand adding the given key/value pairs.static <K,V> MapBuilder<K, V> newBuilder(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) Static factory method for creating a freshMapBuilderand adding the given key/value pairs.static <K,V> Map<K, V> singletonMap(K key, V value) Static factory method for creating an immutableMapwith a single entry.Deprecated.toFastSortedMap(Comparator<K> comparator) Deprecated.UseImmutableSortedMapdirectly instead.Returns a mutableHashMapwhere the entries are in no defined order.Returns a mutableLinkedHashMapwhere the entries are in the same order as they were added.Returns an immutableMapwhere the entries are in the same order as they were added.toMap()Returns an immutableMapwhere the entries are in no defined order.toSortedMap(Comparator<K> comparator) toTreeMap(Comparator<K> comparator)
-
Field Details
-
map
-
-
Constructor Details
-
MapBuilder
protected MapBuilder()prefernewBuilder()for inferred generic arguments.
-
-
Method Details
-
emptyMap
Static factory method for creating an empty map. This is exactly equivalent toCollections.emptyMap()and is provided only for completeness.- Type Parameters:
K- the key typeV- the value type- Returns:
- an empty immutable map
-
newBuilder
Static factory method for creating a freshMapBuilder.- Type Parameters:
K- the key typeV- the value type- Returns:
- a new empty
MapBuilder
-
newBuilder
Static factory method for creating a freshMapBuilderand adding the given key/value pair.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the keyvalue- the value- Returns:
- a new
MapBuilder
-
newBuilder
Static factory method for creating a freshMapBuilderand adding the given key/value pairs.- Type Parameters:
K- the key typeV- the value type- Parameters:
key1- the keyvalue1- the valuekey2- the keyvalue2- the value- Returns:
- a new
MapBuilderwith the specified elements
-
newBuilder
public static <K,V> MapBuilder<K,V> newBuilder(K key1, V value1, K key2, V value2, K key3, V value3) Static factory method for creating a freshMapBuilderand adding the given key/value pairs.- Type Parameters:
K- the key typeV- the value type- Parameters:
key1- the keyvalue1- the valuekey2- the keyvalue2- the valuekey3- the keyvalue3- the value- Returns:
- a new
MapBuilderwith the specified elements
-
newBuilder
public static <K,V> MapBuilder<K,V> newBuilder(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) Static factory method for creating a freshMapBuilderand adding the given key/value pairs.- Type Parameters:
K- the key typeV- the value type- Parameters:
key1- the keyvalue1- the valuekey2- the keyvalue2- the valuekey3- the keyvalue3- the valuekey4- the keyvalue4- the value- Returns:
- a new
MapBuilderwith the specified elements
-
newBuilder
Static factory method for creating a freshMapBuilderbased on the contents of a source Map.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map, may benull- Returns:
- a new
MapBuilder, populated with the values frommap, if one is provided
-
singletonMap
Static factory method for creating an immutableMapwith a single entry. This is exactly equivalent toCollections.singletonMap(key, value)and is only provided for completeness.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the keyvalue- the value- Returns:
- a new empty
MapBuilder
-
build
Returns an immutable Map containing the given key-value pair. PreferImmutableMap.of(Object, Object)if neitherkeynorvaluewill ever need to benull.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- The Keyvalue- The Value- Returns:
- an immutable Map containing the given key-value pair.
-
build
Returns an immutable Map containing the given key-value pairs. PreferImmutableMap.of(Object, Object, Object, Object)if none of the keys or values will ever need to benull.- Type Parameters:
K- the key typeV- the value type- Parameters:
key1- The Keyvalue1- The Valuekey2- The Keyvalue2- The Value- Returns:
- an immutable Map containing the given key-value pair.
-
build
public static <K,V> Map<K,V> build(K key1, @Nullable V value1, K key2, @Nullable V value2, K key3, @Nullable V value3) Returns an immutable Map containing the given key-value pairs. PreferImmutableMap.of(Object, Object, Object, Object, Object, Object)if none of the keys or values will ever need to benull.- Type Parameters:
K- the key typeV- the value type- Parameters:
key1- The Keyvalue1- The Valuekey2- The Keyvalue2- The Valuekey3- The Keyvalue3- The Value- Returns:
- an immutable Map containing the given key-value pair.
-
build
public static <K,V> Map<K,V> build(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) Returns an immutable Map containing the given key-value pairs. PreferImmutableMap.of(Object, Object, Object, Object, Object, Object, Object, Object)if none of the keys or values will ever need to benull.- Type Parameters:
K- the key typeV- the value type- Parameters:
key1- The Keyvalue1- The Valuekey2- The Keyvalue2- The Valuekey3- The Keyvalue3- The Valuekey4- The Keyvalue4- The Value- Returns:
- an immutable Map containing the given key-value pair.
-
add
Add a key/value. Allowsnulls. PreferImmutableMap.builder()if that is not needed.- Parameters:
key- the keyvalue- the value- Returns:
- this
-
addIfValueNotNull
Add a key/value only if the value is notnull. If the intent is to produce an immutable map that does not containnullvalues, thenImmutableMapshould be preferred. This method should only be useful when intending to calltoMutableMap().- Parameters:
key- the keyvalue- the value- Returns:
- this
-
addAll
Add all the entries contained in the map. Accepts a null map and does nothing.- Parameters:
map- the map to add- Returns:
- this
-
toMap
Returns an immutableMapwhere the entries are in no defined order. The map may containnulls. If this ability is not needed, thenImmutableMapis preferred.- Returns:
- an immutable
Mapwhere the entries are in no defined order.
-
toHashMap
Returns a mutableHashMapwhere the entries are in no defined order.- Returns:
- a mutable
HashMapwhere the entries are in no defined order.
-
toLinkedHashMap
Returns a mutableLinkedHashMapwhere the entries are in the same order as they were added.- Returns:
- a mutable
LinkedHashMapwhere the entries are in the same order as they were added.
-
toListOrderedMap
Returns an immutableMapwhere the entries are in the same order as they were added. The map may containnulls. If this ability is not needed, then usingImmutableMapis preferred.- Returns:
- an immutable
Mapwhere the entries are in the same order as they were added.
-
toSortedMap
- Returns:
- an immutable
SortedMapwhere the entries are in the natural order of the keys. Note that the key type must implementComparable.
-
toSortedMap
- Parameters:
comparator- used to sort the keys.- Returns:
- an immutable
SortedMapwhere the entries are in the order defined by the suppliedComparator.
-
toTreeMap
- Returns:
- a mutable
TreeMapwhere the entries are in the natural order of the keys. Note that the key type must implementComparable.
-
toTreeMap
- Parameters:
comparator- used to sort the keys.- Returns:
- a mutable
TreeMapwhere the entries are in the order defined by the suppliedComparator.
-
toMutableMap
- Returns:
- a mutable
Mapwhere the entries are in no defined order.
-
toFastSortedMap
Deprecated.UseImmutableSortedMapdirectly instead. Since v6.1.This returns a guavaImmutableSortedMap, which does not acceptnulls. Please use that class directly, instead.- Returns:
- a fast immutable sorted map
- Throws:
NullPointerException- if there are anynulls in this builder.ClassCastException- if the keys in this builder do not implementComparable.
-
toFastSortedMap
Deprecated.UseImmutableSortedMapdirectly instead. Since v6.1.This returns a guavaImmutableSortedMap, which does not acceptnulls. Please use that class directly, instead.- Parameters:
comparator- for sorting the map- Returns:
- a fast immutable sorted map
- Throws:
NullPointerException- if there are anynulls in this builder.ClassCastException- if the keys in this builder do not implementComparable.
-
ImmutableSortedMapdirectly instead.