FileDocCategorySizeDatePackage
FastHashMap.javaAPI DocHibernate 3.2.59410Thu Jun 03 11:31:32 BST 2004org.hibernate.util

FastHashMap

public final class FastHashMap extends Object implements Serializable, Map

A customized implementation of java.util.HashMap designed to operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes. Read calls are non-synchronized and write calls perform the following steps:

  • Clone the existing collection
  • Perform the modification on the clone
  • Replace the existing collection with the (modified) clone

NOTE: If you are creating and accessing a HashMap only within a single thread, you should use java.util.HashMap directly (with no synchronization), for maximum performance.

Fields Summary
private HashMap
map
The underlying map we are managing.
Constructors Summary
public FastHashMap()
Construct a an empty map.


		super();
		this.map = new HashMap();

	
public FastHashMap(int capacity)
Construct an empty map with the specified capacity.

param
capacity The initial capacity of the empty map


		super();
		this.map = new HashMap(capacity);

	
public FastHashMap(int capacity, float factor)
Construct an empty map with the specified capacity and load factor.

param
capacity The initial capacity of the empty map
param
factor The load factor of the new map


		super();
		this.map = new HashMap(capacity, factor);

	
public FastHashMap(Map map)
Construct a new map with the same mappings as the specified map.

param
map The map whose mappings are to be copied


		super();
		this.map = new HashMap(map);

	
Methods Summary
public voidclear()
Remove all mappings from this map.


	// --------------------------------------------------------- Public Methods

	      	 
	   

		synchronized (this) {
			HashMap temp = (HashMap) map.clone();
			temp.clear();
			map = temp;
		}

	
public java.lang.Objectclone()
Return a shallow copy of this FastHashMap instance. The keys and values themselves are not copied.


		return new FastHashMap(map);

	
public booleancontainsKey(java.lang.Object key)
Return true if this map contains a mapping for the specified key.

param
key Key to be searched for


		return map.containsKey(key);

	
public booleancontainsValue(java.lang.Object value)
Return true if this map contains one or more keys mapping to the specified value.

param
value Value to be searched for


		return map.containsValue(value);

	
public java.util.SetentrySet()
Return a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry.


		return map.entrySet();

	
public booleanequals(java.lang.Object o)
Compare the specified object with this list for equality. This implementation uses exactly the code that is used to define the list equals function in the documentation for the Map.equals method.

param
o Object to be compared to this list


		// Simple tests that require no synchronization
		if (o == this)
			return true;
		else if ( !(o instanceof Map) )
			return false;
		Map mo = (Map) o;

		// Compare the two maps for equality

		if ( mo.size() != map.size() )
			return false;
		java.util.Iterator i = map.entrySet().iterator();
		while ( i.hasNext() ) {
			Map.Entry e = (Map.Entry) i.next();
			Object key = e.getKey();
			Object value = e.getValue();
			if (value == null) {
				if ( !( mo.get(key) == null && mo.containsKey(key) ) )
					return false;
			}
			else {
				if ( !value.equals( mo.get(key) ) )
					return false;
			}
		}
		return true;

	
public java.lang.Objectget(java.lang.Object key)
Return the value to which this map maps the specified key. Returns null if the map contains no mapping for this key, or if there is a mapping with a value of null. Use the containsKey() method to disambiguate these cases.

param
key Key whose value is to be returned


		return map.get(key);

	
public inthashCode()
Return the hash code value for this map. This implementation uses exactly the code that is used to define the list hash function in the documentation for the Map.hashCode method.


		int h = 0;
		java.util.Iterator i = map.entrySet().iterator();
		while ( i.hasNext() )
			h += i.next().hashCode();
		return h;

	
public booleanisEmpty()
Return true if this map contains no mappings.


		return map.isEmpty();

	
public java.util.SetkeySet()
Return a set view of the keys contained in this map.


		return map.keySet();

	
public java.lang.Objectput(java.lang.Object key, java.lang.Object value)
Associate the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced and returned.

param
key The key with which the value is to be associated
param
value The value to be associated with this key


		synchronized (this) {
			HashMap temp = (HashMap) map.clone();
			Object result = temp.put(key, value);
			map = temp;
			return (result);
		}

	
public voidputAll(java.util.Map in)
Copy all of the mappings from the specified map to this one, replacing any mappings with the same keys.

param
in Map whose mappings are to be copied


		synchronized (this) {
			HashMap temp = (HashMap) map.clone();
			temp.putAll(in);
			map = temp;
		}

	
public java.lang.Objectremove(java.lang.Object key)
Remove any mapping for this key, and return any previously mapped value.

param
key Key whose mapping is to be removed


		synchronized (this) {
			HashMap temp = (HashMap) map.clone();
			Object result = temp.remove(key);
			map = temp;
			return (result);
		}

	
public intsize()
Return the number of key-value mappings in this map.


		return map.size();

	
public java.lang.StringtoString()

 return map.toString(); 
public java.util.Collectionvalues()
Return a collection view of the values contained in this map.


		return map.values();