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.. This class may throw
exceptions when accessed by concurrent threads without synchronization.
This class is Serializable from Commons Collections 3.1.
| Modifier and Type | Field and Description |
|---|---|
| protected final Transformer | factory
The factory to use to construct elements |
| private static final long | serialVersionUID
Serialization version |
| Access | Constructor and Description |
|---|---|
| protected | |
| 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). |
| Modifier and Type | Method and Description |
|---|---|
| public V | get(final Object
the key whose associated value is to be returned key)Overrides org. Implements java. null if this map contains no mapping for the key.
|
| public static < the key type K, the value type V> LazyMap | |
| public static < the value type V, the key type K> LazyMap | Returns: a new lazy mapthe 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 | |
| private void |
| factory | back to summary |
|---|---|
| protected final Transformer<? super K, ? extends V> factory The factory to use to construct elements | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version | |
| LazyMap | back to summary |
|---|---|
| protected LazyMap(final Map<K, V> map, final Factory<? extends V> factory) Constructor that wraps (not copies).
| |
| LazyMap | back to summary |
|---|---|
| protected LazyMap(final Map<K, V> map, final Transformer<? super K, ? extends V> factory) Constructor that wraps (not copies).
| |
| get | back to summary |
|---|---|
| public V get(final Object key) Overrides org. Implements java. Doc from java. Returns the value to which the specified key is mapped,
or More formally, if this map contains a mapping from a key
If this map permits null values, then a return value of
| |
| lazyMap | back to summary |
|---|---|
| public static <K, V> LazyMap Factory method to create a lazily instantiated map.
| |
| lazyMap | back to summary |
|---|---|
| public static <V, K> LazyMap Factory method to create a lazily instantiated map.
| |
| readObject | back to summary |
|---|---|
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException Read the map in using a custom routine.
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(final ObjectOutputStream out) throws IOException Write the map out using a custom routine.
| |