com.atlassian.confluence.util.collections
Class LazyMap<K,V>

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

@NotThreadSafe
public class LazyMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.util.Map<K,V>

A Map that is backed by a delegate map that contains suppliers for the values that are called lazily when required. The values returned from the delegate 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.

Note: even though this class is effectively immutable and does not support direct mutation, the caching strategy 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.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Method Summary
 boolean containsKey(java.lang.Object key)
           
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
           
 V get(java.lang.Object key)
          .
 boolean isEmpty()
           
 java.util.Set<K> keySet()
           
static
<K,V> LazyMap<K,V>
newInstance(java.util.Map<K,? extends com.google.common.base.Supplier<? extends V>> delegate)
           
 V remove(java.lang.Object key)
           
 int size()
           
 
Methods inherited from class java.util.AbstractMap
clear, clone, containsValue, equals, hashCode, put, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsValue, equals, hashCode, put, putAll, values
 

Method Detail

newInstance

public static <K,V> LazyMap<K,V> newInstance(java.util.Map<K,? extends com.google.common.base.Supplier<? extends V>> delegate)

get

public V get(java.lang.Object key)
.

Implements the local caching of a value from the master.

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class java.util.AbstractMap<K,V>

keySet

public java.util.Set<K> keySet()
Specified by:
keySet in interface java.util.Map<K,V>
Overrides:
keySet in class java.util.AbstractMap<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<K,V>
Overrides:
containsKey in class java.util.AbstractMap<K,V>

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface java.util.Map<K,V>
Specified by:
entrySet in class java.util.AbstractMap<K,V>

size

public int size()
Specified by:
size in interface java.util.Map<K,V>
Overrides:
size in class java.util.AbstractMap<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<K,V>
Overrides:
isEmpty in class java.util.AbstractMap<K,V>

remove

public V remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<K,V>
Overrides:
remove in class java.util.AbstractMap<K,V>


Copyright © 2003-2014 Atlassian. All Rights Reserved.