org.inria.ns.reflex.util
Class DeferredMap

java.lang.Object
  extended by org.inria.ns.reflex.util.DeferredMap
All Implemented Interfaces:
Map
Direct Known Subclasses:
DeferredEntryMap

public class DeferredMap
extends Object
implements Map

A map that contains entries supplied by a factory. It is useful when the values of the entries have to be computed, when the values can change, or when computing the values is time-consuming.

A deferred map is made of 2 pieces : this class which is exposed as a map, and the entry factory, which is responsible of supplying entries.

An entry is built at the latest possible stage, that is to say when it is explicitely expected.

Thus, an entry is looked up first inside an internal map, and if it is not found, it is get from the factory and put in the internal map for the next accesses.

Author:
Philippe Poulard
See Also:
EntryFactory

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
DeferredMap(EntryFactory factory)
          Create a new deferred map that relies on a factory.
 
Method Summary
 void clear()
          Clear the entries cached in the internal map.
 boolean containsKey(Object key)
          Indicates whether an entry is in this map or not.
 boolean containsValue(Object value)
          Indicates whether an entry value is in this map or not.
 Set entrySet()
          Get the set of entries in this map.
 boolean equals(Object o)
          Indicates whether this map has the same entries as the given map.
 Object get(Object key)
          Get an entry value.
 void getAll()
          Force all the entries to be get from the factory and put them in the internal map.
 int hashCode()
          Return the hashcode of this map.
 boolean isEmpty()
          Indicates whether this map is empty or not.
 Set keySet()
          Return the keys of this map.
 Object put(Object key, Object value)
          Store an entry in this map.
 void putAll(Map map)
          Store all the entries of a map in this map.
 Object remove(Object key)
          Remove an entry in this map ; an entry declared unremovable in the factory is preserved.
 void removeEntry(Object key)
          Remove an entry in this map ; an entry declared unremovable in the factory is preserved.
 int size()
          Return the size of this map.
 Collection values()
          Return all the values in this map.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DeferredMap

public DeferredMap(EntryFactory factory)
Create a new deferred map that relies on a factory.

Parameters:
factory - The non-null factory that can build entries when they are requested for the first time.
See Also:
HashMap
Method Detail

getAll

public void getAll()
Force all the entries to be get from the factory and put them in the internal map.


clear

public void clear()
Clear the entries cached in the internal map. Entries from the factory that are not removable will be stored again if they are requested again.

Specified by:
clear in interface Map

containsKey

public boolean containsKey(Object key)
Indicates whether an entry is in this map or not.

Specified by:
containsKey in interface Map
Parameters:
key - The entry key.
Returns:
true if the entry is in the internal map of in the factory, false otherwise.

containsValue

public boolean containsValue(Object value)
Indicates whether an entry value is in this map or not.

Specified by:
containsValue in interface Map
Parameters:
value - The entry value.
Returns:
true if the entry value is in this map, false otherwise.

entrySet

public Set entrySet()
Get the set of entries in this map. Force all entries to be stored in the internal map.

Specified by:
entrySet in interface Map
Returns:
A set of entries.

equals

public boolean equals(Object o)
Indicates whether this map has the same entries as the given map.

Specified by:
equals in interface Map
Overrides:
equals in class Object
Parameters:
o - The map to compare with.
Returns:
true if the 2 maps contains the same entries, false otherwise.

get

public Object get(Object key)
Get an entry value.

Specified by:
get in interface Map
Parameters:
key - The entry key to get.
Returns:
The entry value to get.

hashCode

public int hashCode()
Return the hashcode of this map.

Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object
Returns:
The hashcode of this map.

isEmpty

public boolean isEmpty()
Indicates whether this map is empty or not.

Specified by:
isEmpty in interface Map
Returns:
true if this map is empty, false otherwise.

keySet

public Set keySet()
Return the keys of this map.

Specified by:
keySet in interface Map
Returns:
The set of keys stored in this map.

put

public Object put(Object key,
                  Object value)
Store an entry in this map.

Specified by:
put in interface Map
Parameters:
key - The entry key.
value - The entry value.
Returns:
The previous value of the entry.

putAll

public void putAll(Map map)
Store all the entries of a map in this map.

Specified by:
putAll in interface Map
Parameters:
map - The map to store in this map.

remove

public Object remove(Object key)
Remove an entry in this map ; an entry declared unremovable in the factory is preserved.

Specified by:
remove in interface Map
Parameters:
key - The entry key to remove.
Returns:
The value of the entry to remove.

removeEntry

public void removeEntry(Object key)
Remove an entry in this map ; an entry declared unremovable in the factory is preserved.

Parameters:
key - The entry key to remove.

size

public int size()
Return the size of this map.

Specified by:
size in interface Map
Returns:
The number of entries in the factory + the number of foreign entries.

values

public Collection values()
Return all the values in this map.

Specified by:
values in interface Map
Returns:
All the values in this map.