net.sf.staccatocommons.lang
Class MapBuilder<K,V,M extends Map<K,V>>

java.lang.Object
  extended by net.sf.staccatocommons.lang.MapBuilder<K,V,M>
Type Parameters:
K -
V -
M -
All Implemented Interfaces:
Builder<M>

public class MapBuilder<K,V,M extends Map<K,V>>
extends Object
implements Builder<M>

A Builder for Maps. With the exception of from(Map, Applicable), all factory methods of MapBuilder grant to return builders that build Unmodifiable maps

Author:
fbulgarelli

Constructor Summary
MapBuilder(M map, net.sf.staccatocommons.defs.Applicable<M,M> wrapperFunction)
          Creates a new MapBuilder
 
Method Summary
 M build()
          Returns the built object.
static
<K,V> MapBuilder<K,V,Map<K,V>>
from(Map<K,V> map)
          Creates a new MapBuilder that uses the given map instance, and using toUnmodifiableMap() as postprocessor.
static
<K,V> MapBuilder<K,V,Map<K,V>>
linkedMapWith(K key, V value)
          Creates a new MapBuilder using a LinkedHashMap as map implementation and the first key and value.
static
<K,V> MapBuilder<K,V,Map<K,V>>
mapWith(K key, V value)
          Creates a new MapBuilder using a HashMap as map implementation and the first key and value.
static
<K,V> net.sf.staccatocommons.defs.function.Function<Map<K,V>,Map<K,V>>
toUnmodifiableMap()
          A constant function that returns an unmodifiable view of its map argument
static
<K,V> net.sf.staccatocommons.defs.function.Function<SortedMap<K,V>,SortedMap<K,V>>
toUnmodifiableSortedMap()
          A constant function that returns an unmodifiable view of its sortedmap argument
static
<K,V> MapBuilder<K,V,SortedMap<K,V>>
treeMapWith(K key, V value)
          Creates a new MapBuilder using a TreeMap as map implementation and the first key and value.
 MapBuilder<K,V,M> unwrap()
          Disables wrapping, which means that a built map will be returned just as it was created.
 MapBuilder<K,V,M> with(K key, V value)
          Adds a key-value pair to the map
 MapBuilder<K,V,M> with(Map.Entry<K,V> entry)
          Adds an entry to the map
 MapBuilder<K,V,M> withWrapper(net.sf.staccatocommons.defs.Applicable<M,M> wrapperFunction)
          Sets the wrapper function, that is, the Applicable to apply to the resulting map before returning it through build().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapBuilder

public MapBuilder(@NonNull
                  M map,
                  @NonNull
                  net.sf.staccatocommons.defs.Applicable<M,M> wrapperFunction)
Creates a new MapBuilder

Parameters:
map - the map to build. Non null
Method Detail

with

@NonNull
public MapBuilder<K,V,M> with(K key,
                                      V value)
Adds a key-value pair to the map

Parameters:
key -
value -
Returns:
this MapBuilder

with

@NonNull
public MapBuilder<K,V,M> with(@NonNull
                                      Map.Entry<K,V> entry)
Adds an entry to the map

Parameters:
entry - the entry to add. Non null
Returns:
this MapBuilder

withWrapper

@NonNull
public MapBuilder<K,V,M> withWrapper(@NonNull
                                             net.sf.staccatocommons.defs.Applicable<M,M> wrapperFunction)
Sets the wrapper function, that is, the Applicable to apply to the resulting map before returning it through build(). By default, maps are wrapped with unmodifiable wrappers, but this behavior can be changed through this method.

Parameters:
wrapperFunction -
Returns:
this

unwrap

@NonNull
public MapBuilder<K,V,M> unwrap()
Disables wrapping, which means that a built map will be returned just as it was created. As a consequence, the map returned by build() will be modifiable.

Returns:
this

build

@NonNull
public M build()
Description copied from interface: Builder
Returns the built object.

Builders are not meant to be used more than once. So this method can only be invoked successfully one time. Subsequent invocations after a successful return will throw an BuilderAlreadyUsedException. However, implementors may relax this, and be reusable, that is being able to be used more than once; such implementors should document that feature.

Builders may impose restriction to the state of the object under construction. If such constraints are not met while invoking this method, a ObjectUnderConstructionException exception must be thrown.

Specified by:
build in interface Builder<M extends Map<K,V>>
Returns:
The built object.

from

@NonNull
public static <K,V> MapBuilder<K,V,Map<K,V>> from(@NonNull
                                                          Map<K,V> map)
Creates a new MapBuilder that uses the given map instance, and using toUnmodifiableMap() as postprocessor. The map built by the returned Builder grants to be Unmodifiable

Type Parameters:
K -
V -
Parameters:
map -
Returns:
a new MapBuilder that builds unmodifiable views of the given map

mapWith

@NonNull
public static <K,V> MapBuilder<K,V,Map<K,V>> mapWith(K key,
                                                             V value)
Creates a new MapBuilder using a HashMap as map implementation and the first key and value. The map built by the returned Builder grants to be Unmodifiable

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

linkedMapWith

@NonNull
public static <K,V> MapBuilder<K,V,Map<K,V>> linkedMapWith(K key,
                                                                   V value)
Creates a new MapBuilder using a LinkedHashMap as map implementation and the first key and value. The map built by the returned Builder grants to be Unmodifiable

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

treeMapWith

@NonNull
public static <K,V> MapBuilder<K,V,SortedMap<K,V>> treeMapWith(K key,
                                                                       V value)
Creates a new MapBuilder using a TreeMap as map implementation and the first key and value. The map built by the returned Builder grants to be Unmodifiable

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

toUnmodifiableMap

@Constant
public static <K,V> net.sf.staccatocommons.defs.function.Function<Map<K,V>,Map<K,V>> toUnmodifiableMap()
A constant function that returns an unmodifiable view of its map argument

Type Parameters:
K -
V -
Returns:
a constant Function

toUnmodifiableSortedMap

@Constant
public static <K,V> net.sf.staccatocommons.defs.function.Function<SortedMap<K,V>,SortedMap<K,V>> toUnmodifiableSortedMap()
A constant function that returns an unmodifiable view of its sortedmap argument

Type Parameters:
K -
V -
Returns:
a constant Function


Copyright © 2010-2012 Staccatocommons. All Rights Reserved.