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.


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()
          If speed is of the essence, this will return the fastest thread-safe immutable map known.
 SortedMap<K,V> toFastSortedMap()
          If speed is of the essence, this will return the fastest thread-safe immutable sorted map known.
 SortedMap<K,V> toFastSortedMap(Comparator<K> comparator)
          If speed is of the essence, this will return the fastest thread-safe immutable sorted map known.
 HashMap<K,V> toHashMap()
           
 Map<K,V> toImmutableMap()
          Deprecated. use toMap() instead.
 LinkedHashMap<K,V> toLinkedHashMap()
           
 Map<K,V> toListOrderedMap()
           
 Map<K,V> toMap()
           
 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
 

Method Detail

emptyMap

public static <K,V> Map<K,V> emptyMap()
Static factory method for creating an empty map.

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 empty MapBuilder

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.

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.

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.

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.

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.

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 but note that many Map implementations do not allow null keys or values.

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. Allows null keys but note that many Map implementations do not allow null keys.

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.

toHashMap

public HashMap<K,V> toHashMap()
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.

toListOrderedMap

public Map<K,V> toListOrderedMap()
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

public Map<K,V> toFastMap()
If speed is of the essence, this will return the fastest thread-safe immutable map known. Not null tolerant though.

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

toFastSortedMap

public SortedMap<K,V> toFastSortedMap()
If speed is of the essence, this will return the fastest thread-safe immutable sorted map known. Not null tolerant though and the keys must be comparable.

Returns:
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)
If speed is of the essence, this will return the fastest thread-safe immutable sorted map known. Not null tolerant though and the keys must be comparable.

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

toImmutableMap

@Deprecated
public Map<K,V> toImmutableMap()
Deprecated. use toMap() instead.

Returns:
an immutable map backed by a HashMap


Copyright © 2002-2012 Atlassian. All Rights Reserved.