Top Description Fields Constructors Methods
org.apache.commons.collections4.map

public Class LazyMap<K, V>

extends AbstractMapDecorator<K, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
org.apache.commons.collections4.map.LazySortedMap
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Map, org.apache.commons.collections4.Factory, .Transformer, org.apache.commons.collections4.functors.FactoryTransformer

Decorates another Map to create objects in the map on demand.

When the get(Object) method is called with a key that does not exist in the map, the factory is used to create the object. The created object will be added to the map using the requested key.

For instance:

Factory<Date> factory = new Factory<Date>() {
    public Date create() {
        return new Date();
    }
}
Map<String, Date> lazy = LazyMap.lazyMap(new HashMap<String, Date>(), factory);
Date date = lazy.get("NOW");

After the above code is executed, date will refer to a new Date instance. Furthermore, that Date instance is mapped to the "NOW" key in the map.

Note that LazyMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using java.util.Collections#synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

This class is Serializable from Commons Collections 3.1.

Since
3.0

Field Summary

Modifier and TypeField and Description
protected final Transformer<? super K, ? extends V>
factory

The factory to use to construct elements

private static final long
serialVersionUID

Serialization version

Inherited from org.apache.commons.collections4.map.AbstractMapDecorator:
map

Constructor Summary

AccessConstructor and Description
protected
LazyMap(final Map<K, V>
the map to decorate, must not be null
map
,
final Factory<? extends V>
the factory to use, must not be null
factory
)

Constructor that wraps (not copies).

protected
LazyMap(final Map<K, V>
the map to decorate, must not be null
map
,
final Transformer<? super K, ? extends V>
the factory to use, must not be null
factory
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
public V
get(final Object
the key whose associated value is to be returned
key
)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.get.

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
public static <
the key type
K
,
the value type
V
>
LazyMap<K, V>

Returns:

a new lazy map
lazyMap
(final Map<K, V>
the map to decorate, must not be null
map
,
final Factory<? extends V>
the factory to use, must not be null
factory
)

Factory method to create a lazily instantiated map.

public static <
the value type
V
,
the key type
K
>
LazyMap<K, V>

Returns:

a new lazy map
lazyMap
(final Map<K, V>
the map to decorate, must not be null
map
,
final Transformer<? super K, ? extends V>
the factory to use, must not be null
factory
)

Factory method to create a lazily instantiated map.

private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

Inherited from org.apache.commons.collections4.map.AbstractMapDecorator:
clearcontainsKeycontainsValuedecoratedentrySetequalshashCodeisEmptykeySetputputAllremovesizetoStringvalues

Field Detail

factoryback to summary
protected final Transformer<? super K, ? extends V> factory

The factory to use to construct elements

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

Constructor Detail

LazyMapback to summary
protected LazyMap(final Map<K, V> map, final Factory<? extends V> factory)

Constructor that wraps (not copies).

Parameters
map:Map<K, V>

the map to decorate, must not be null

factory:Factory<? extends V>

the factory to use, must not be null

Exceptions
NullPointerException:
if map or factory is null
LazyMapback to summary
protected LazyMap(final Map<K, V> map, final Transformer<? super K, ? extends V> factory)

Constructor that wraps (not copies).

Parameters
map:Map<K, V>

the map to decorate, must not be null

factory:Transformer<? super K, ? extends V>

the factory to use, must not be null

Exceptions
NullPointerException:
if map or factory is null

Method Detail

getback to summary
public V get(final Object key)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.get.

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Doc from java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that Objects.equals(key, k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Parameters
key:Object

the key whose associated value is to be returned

Returns:V

the value to which the specified key is mapped, or null if this map contains no mapping for the key

Annotations
@Override
lazyMapback to summary
public static <K, V> LazyMap<K, V> lazyMap(final Map<K, V> map, final Factory<? extends V> factory)

Factory method to create a lazily instantiated map.

Parameters
<K>
the key type
<V>
the value type
map:Map<K, V>

the map to decorate, must not be null

factory:Factory<? extends V>

the factory to use, must not be null

Returns:LazyMap<K, V>

a new lazy map

Exceptions
NullPointerException:
if map or factory is null
Since
4.0
lazyMapback to summary
public static <V, K> LazyMap<K, V> lazyMap(final Map<K, V> map, final Transformer<? super K, ? extends V> factory)

Factory method to create a lazily instantiated map.

Parameters
<V>
the value type
<K>
the key type
map:Map<K, V>

the map to decorate, must not be null

factory:Transformer<? super K, ? extends V>

the factory to use, must not be null

Returns:LazyMap<K, V>

a new lazy map

Exceptions
NullPointerException:
if map or factory is null
Since
4.0
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the map in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Annotations
@SuppressWarnings:unchecked
Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
Since
3.1
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the map out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream
Since
3.1