Class LazyMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
com.atlassian.confluence.util.collections.LazyMap<K,V>
Type Parameters:
K - the key type.
V - the value type.
All Implemented Interfaces:
Map<K,V>

@NotThreadSafe public class LazyMap<K,V> extends AbstractMap<K,V>
A Map that is constructed from a map of suppliers for the values, that are evaluated lazily when required. The values returned from the suppliers are cached so they are only retrieved once.

The goal is to reduce the overhead required to instantiate and store entries which might not be used. For example, when rendering Velocity templates, the context map is populated with many things that are only ever required occasionally.

Use copyOf(com.atlassian.confluence.util.collections.LazyMap<K, V>), putAll(LazyMap) and putAllIfAbsent(com.atlassian.confluence.util.collections.LazyMap<K, V>) when copying or merging LazyMap instances. These methods guarantee that the suppliers will not be invoked unnecessarily and never more than once, even when copied to a new map.

This class is not thread-safe and should not be used if thread-safety is required. Adding thread-safety to this class would be fairly trivial through.

Since:
3.4