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

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.

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.

toImmutableMap

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

Returns:
an immutable map backed by a HashMap


Copyright © 2002-2009 Atlassian. All Rights Reserved.