gausssoft.collections
Class SynchronizedMap

java.lang.Object
  |
  +--gausssoft.collections.SynchronizedMap
All Implemented Interfaces:
Map, Serializable
Direct Known Subclasses:
SynchronizedSortedMap

public class SynchronizedMap
extends Object
implements Map, Serializable

The SynchronizedMap class is used to synchronize all operations on a Map object on a particular object.

Each method of this implementation synchronizes on a specified object specified at construction time, and then calls the method of the same signature of the backing map. Each instance is serializable only if the backing map and mutex is serializable.

This is similar to the synchronization methods in Collections, but this implementation allows you to choose what object to synchronize on, and allows subclassing.

The keySet(), entrySet() and values() all return collections that synchronize on the same mutex as the synchronized map. The first time these methods are called, a SynchronizedCollection or SynchronizedSet is created, and stored within the SynchronizedMap. Every subsequent call to these methods will return these stored objects, as opposed to creating new synchronized objects each method call.

Author:
Allan Crooks
See Also:
Collections, Map, Serialized Form

Inner classes inherited from class java.util.Map
Map.Entry
 
Field Summary
protected  Object mutex
          Object to synchronize on.
protected  Map root
          Backing map.
 
Constructor Summary
SynchronizedMap(Map root)
          Creates a new SynchronizedMap.
SynchronizedMap(Map root, Object mutex)
          Creates a new SynchronizedMap.
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set entrySet()
           
 boolean equals(Object o)
           
 Object get(Object key)
           
 int hashCode()
           
 boolean isEmpty()
           
 Set keySet()
           
 Object put(Object key, Object value)
           
 void putAll(Map t)
           
 Object remove(Object key)
           
 int size()
           
 String toString()
           
 Collection values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

root

protected final Map root
Backing map.

mutex

protected final Object mutex
Object to synchronize on.
Constructor Detail

SynchronizedMap

public SynchronizedMap(Map root,
                       Object mutex)
Creates a new SynchronizedMap.
Parameters:
root - The map to use as the backing map.
mutex - The object to synchronize operations on.
Throws:
NullPointerException - If root is null.

SynchronizedMap

public SynchronizedMap(Map root)
Creates a new SynchronizedMap.

The object constructed will synchronize all operations on itself.

Parameters:
root - The map to use as the backing map.
Throws:
NullPointerException - If root is null.
Method Detail

size

public int size()
Specified by:
size in interface Map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map

get

public Object get(Object key)
Specified by:
get in interface Map

put

public Object put(Object key,
                  Object value)
Specified by:
put in interface Map

remove

public Object remove(Object key)
Specified by:
remove in interface Map

putAll

public void putAll(Map t)
Specified by:
putAll in interface Map

clear

public void clear()
Specified by:
clear in interface Map

keySet

public Set keySet()
Specified by:
keySet in interface Map

values

public Collection values()
Specified by:
values in interface Map

entrySet

public Set entrySet()
Specified by:
entrySet in interface Map

equals

public boolean equals(Object o)
Specified by:
equals in interface Map
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object