com.atlassian.jira.util.collect
Class MapBuilder<K,V>

java.lang.Object
  extended by com.atlassian.jira.util.collect.MapBuilder<K,V>
Type Parameters:
K - the key type
V - the value type

public class MapBuilder<K,V>
extends Object

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
protected  Map<K,V> map
           
 
Constructor Summary
protected MapBuilder()
          prefer newBuilder() for inferred generic arguments.
 
Method Summary
 MapBuilder<K,V> add(K key, V value)
          Add a key/value.
 MapBuilder<K,V> addAll(Map<? extends K,? extends V> map)
          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 not null.
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>
newBuilder()
          Static factory method for creating a fresh MapBuilder.
static
<K,V> MapBuilder<K,V>
newBuilder(K key, V value)
          Static factory method for creating a fresh MapBuilder and 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 fresh MapBuilder and 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 fresh MapBuilder and 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 fresh MapBuilder and adding the given key/value pairs.
static
<K,V> MapBuilder<K,V>
newBuilder(Map<? extends K,? extends V> map)
          Static factory method for creating a fresh MapBuilder based on the contents of a source Map.
static
<K,V> Map<K,V>
singletonMap(K key, V value)
          Static factory method for creating an immutable Map with a single entry.
 Map<K,V> toFastMap()
          Deprecated. Use ImmutableMap directly instead. Since v6.1.
 SortedMap<K,V> toFastSortedMap()
          Deprecated. Use ImmutableSortedMap directly instead. Since v6.1.
 SortedMap<K,V> toFastSortedMap(Comparator<K> comparator)
          Deprecated. Use ImmutableSortedMap directly instead. Since v6.1.
 HashMap<K,V> toHashMap()
          Returns a mutable HashMap where the entries are in no defined order.
 Map<K,V> toImmutableMap()
          Deprecated. use toMap() instead, or consider using ImmutableMap if if the map does not need to accept nulls.
 LinkedHashMap<K,V> toLinkedHashMap()
          Returns a mutable LinkedHashMap where the entries are in the same order as they were added.
 Map<K,V> toListOrderedMap()
          Returns an immutable Map where the entries are in the same order as they were added.
 Map<K,V> toMap()
          Returns an immutable Map where the entries are in no defined order.
 Map<K,V> toMutableMap()
           
 SortedMap<K,V> toSortedMap()
           
 SortedMap<K,V> toSortedMap(Comparator<K> comparator)
           
 TreeMap<K,V> toTreeMap()
           
 TreeMap<K,V> toTreeMap(Comparator<K> comparator)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

protected final Map<K,V> map
Constructor Detail

MapBuilder

protected MapBuilder()
prefer newBuilder() for inferred generic arguments.

Method Detail

emptyMap

public static <K,V> Map<K,V> emptyMap()
Static factory method for creating an empty map. This is exactly equivalent to Collections.emptyMap() and is provided only for completeness.

Type Parameters:
K - the key type
V - the value type
Returns:
an empty immutable map

newBuilder

public static <K,V> MapBuilder<K,V> newBuilder()
Static factory method for creating a fresh MapBuilder.

Type Parameters:
K - the key type
V - the value type
Returns:
a new empty MapBuilder

newBuilder

public static <K,V> MapBuilder<K,V> newBuilder(K key,
                                               V value)
Static factory method for creating a fresh MapBuilder and adding the given key/value pair.

Type Parameters:
K - the key type
V - the value type
Parameters:
key - the key
value - the value
Returns:
a new MapBuilder

newBuilder

public static <K,V> MapBuilder<K,V> newBuilder(K key1,
                                               V value1,
                                               K key2,
                                               V value2)
Static factory method for creating a fresh MapBuilder and adding the given key/value pairs.

Type Parameters:
K - the key type
V - the value type
Parameters:
key1 - the key
value1 - the value
key2 - the key
value2 - the value
Returns:
a new MapBuilder with 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 fresh MapBuilder and adding the given key/value pairs.

Type Parameters:
K - the key type
V - the value type
Parameters:
key1 - the key
value1 - the value
key2 - the key
value2 - the value
key3 - the key
value3 - the value
Returns:
a new MapBuilder with 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 fresh MapBuilder and adding the given key/value pairs.

Type Parameters:
K - the key type
V - the value type
Parameters:
key1 - the key
value1 - the value
key2 - the key
value2 - the value
key3 - the key
value3 - the value
key4 - the key
value4 - the value
Returns:
a new MapBuilder with the specified elements

newBuilder

public static <K,V> MapBuilder<K,V> newBuilder(@Nullable
                                               Map<? extends K,? extends V> map)
Static factory method for creating a fresh MapBuilder based on the contents of a source Map.

Type Parameters:
K - the key type
V - the value type
Parameters:
map - the source map, may be null
Returns:
a new MapBuilder, populated with the values from map, if one is provided

singletonMap

public static <K,V> Map<K,V> singletonMap(K key,
                                          V value)
Static factory method for creating an immutable Map with a single entry. This is exactly equivalent to Collections.singletonMap(key, value) and is only provided for completeness.

Type Parameters:
K - the key type
V - the value type
Parameters:
key - the key
value - the value
Returns:
a new empty MapBuilder

build

public static <K,V> Map<K,V> build(K key,
                                   V value)
Returns an immutable Map containing the given key-value pair. Prefer ImmutableMap.of(Object,Object) if neither key nor value will ever need to be null.

Type Parameters:
K - the key type
V - the value type
Parameters:
key - The Key
value - 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)
Returns an immutable Map containing the given key-value pairs. Prefer ImmutableMap.of(Object,Object,Object,Object) if none of the keys or values will ever need to be null.

Type Parameters:
K - the key type
V - the value type
Parameters:
key1 - The Key
value1 - The Value
key2 - The Key
value2 - 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. Prefer ImmutableMap.of(Object,Object,Object,Object,Object,Object) if none of the keys or values will ever need to be null.

Type Parameters:
K - the key type
V - the value type
Parameters:
key1 - The Key
value1 - The Value
key2 - The Key
value2 - The Value
key3 - The Key
value3 - 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. Prefer ImmutableMap.of(Object,Object,Object,Object,Object,Object,Object,Object) if none of the keys or values will ever need to be null.

Type Parameters:
K - the key type
V - the value type
Parameters:
key1 - The Key
value1 - The Value
key2 - The Key
value2 - The Value
key3 - The Key
value3 - The Value
key4 - The Key
value4 - The Value
Returns:
an immutable Map containing the given key-value pair.

add

public MapBuilder<K,V> add(@Nullable
                           K key,
                           @Nullable
                           V value)
Add a key/value. Allows nulls. Prefer ImmutableMap.builder() if that is not needed.

Parameters:
key - the key
value - the value
Returns:
this

addIfValueNotNull

public MapBuilder<K,V> addIfValueNotNull(K key,
                                         @Nullable
                                         V value)
Add a key/value only if the value is not null. If the intent is to produce an immutable map that does not contain null values, then ImmutableMap should be preferred. This method should only be useful when intending to call toMutableMap().

Parameters:
key - the key
value - the value
Returns:
this

addAll

public MapBuilder<K,V> addAll(@Nullable
                              Map<? extends K,? extends V> map)
Add all the entries contained in the map. Accepts a null map and does nothing.

Parameters:
map - the map to add
Returns:
this

toMap

public Map<K,V> toMap()
Returns an immutable Map where the entries are in no defined order. The map may contain nulls. If this ability is not needed, then ImmutableMap is preferred.

Returns:
an immutable Map where the entries are in no defined order.

toHashMap

public HashMap<K,V> toHashMap()
Returns a mutable HashMap where the entries are in no defined order.

Returns:
a mutable HashMap where the entries are in no defined order.

toLinkedHashMap

public LinkedHashMap<K,V> toLinkedHashMap()
Returns a mutable LinkedHashMap where the entries are in the same order as they were added.

Returns:
a mutable LinkedHashMap where the entries are in the same order as they were added.

toListOrderedMap

public Map<K,V> toListOrderedMap()
Returns an immutable Map where the entries are in the same order as they were added. The map may contain nulls. If this ability is not needed, then using ImmutableMap is preferred.

Returns:
an immutable Map where the entries are in the same order as they were added.

toSortedMap

public SortedMap<K,V> toSortedMap()
Returns:
an immutable SortedMap where the entries are in the natural order of the keys. Note that the key type must implement Comparable.

toSortedMap

public SortedMap<K,V> toSortedMap(Comparator<K> comparator)
Parameters:
comparator - used to sort the keys.
Returns:
an immutable SortedMap where the entries are in the order defined by the supplied Comparator.

toTreeMap

public TreeMap<K,V> toTreeMap()
Returns:
a mutable TreeMap where the entries are in the natural order of the keys. Note that the key type must implement Comparable.

toTreeMap

public TreeMap<K,V> toTreeMap(Comparator<K> comparator)
Parameters:
comparator - used to sort the keys.
Returns:
a mutable TreeMap where the entries are in the order defined by the supplied Comparator.

toMutableMap

public Map<K,V> toMutableMap()
Returns:
a mutable Map where the entries are in no defined order.

toFastMap

@Deprecated
public Map<K,V> toFastMap()
Deprecated. Use ImmutableMap directly instead. Since v6.1.

This returns a guava ImmutableMap, which does not accept nulls. Please use that class directly, instead.

Returns:
a fast immutable map
Throws:
NullPointerException - if there are any nulls in this builder.

toFastSortedMap

public SortedMap<K,V> toFastSortedMap()
Deprecated. Use ImmutableSortedMap directly instead. Since v6.1.

This returns a guava ImmutableSortedMap, which does not accept nulls. Please use that class directly, instead.

Returns:
a fast immutable sorted map
Throws:
NullPointerException - if there are any nulls in this builder.
ClassCastException - if the keys in this builder do not implement Comparable.

toFastSortedMap

public SortedMap<K,V> toFastSortedMap(Comparator<K> comparator)
Deprecated. Use ImmutableSortedMap directly instead. Since v6.1.

This returns a guava ImmutableSortedMap, which does not accept nulls. Please use that class directly, instead.

Parameters:
comparator - for sorting the map
Returns:
a fast immutable sorted map
Throws:
NullPointerException - if there are any nulls in this builder.
ClassCastException - if the keys in this builder do not implement Comparable.

toImmutableMap

@Deprecated
public Map<K,V> toImmutableMap()
Deprecated. use toMap() instead, or consider using ImmutableMap if if the map does not need to accept nulls.

Returns an immutable map backed by a HashMap, which will accept nulls. If that ability is not needed, then ImmutableMap is preferred.

Returns:
an immutable map backed by a HashMap


Copyright © 2002-2014 Atlassian. All Rights Reserved.