All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.varra.util.RapidFastMap Maven / Gradle / Ivy

/*
 * utils4j - RapidFastMap.java, Feb 4, 2011 12:05:59 PM
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.varra.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.annotation.XmlElementWrapper;

/**
 * A {@link RapidFastMap} which provides a fast map with concurrency. A thread
 * safe implementation is added on top of the Hash Map mechanism.
 * 
 * @param 
 *            the key type
 * @param 
 *            the value type
 * @author Rajakrishna V. Reddy
 * @version 1.0
 */
public class RapidFastMap implements Cloneable, Serializable
{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The map. */
	@XmlElementWrapper
	final private LinkedHashMap map;
	
	/**
	 * Instantiates a new empty rapid fast map with a default initial capacity,
	 * load factor, and concurrencyLevel.
	 */
	public RapidFastMap()
	{
		map = new LinkedHashMap();
	}
	
	/**
	 * Instantiates a new rapid fast map with the given map elements and its
	 * capacity.
	 * 
	 * @param tempMap
	 *            the temp map
	 */
	public RapidFastMap(Map tempMap)
	{
		map = new LinkedHashMap(tempMap);
	}
	
	/**
	 * Returns true if this map contains no key-value mappings.
	 * 

* * This implementation returns size() == 0. * * @return true if this map contains no key-value mappings. */ public boolean isEmpty() { return map.isEmpty(); } /** * Returns the number of key-value mappings in this map. If the map contains * more than Integer.MAX_VALUE elements, returns * Integer.MAX_VALUE. *

* * This implementation returns entrySet().size(). * * @return the number of key-value mappings in this map. */ public int size() { return map.size(); } /** * Returns the value to which the specified key is mapped in this table. * * @param key * a key in the table. * @return the value to which the key is mapped in this table; null * if the key is not mapped to any value in this table. * @throws NullPointerException * if the key is null. */ public V get(K key) { return map.get(key); } /** * Returns this Object as {@link Map}. * * @return this as map */ public Map getAsMap() { return map; } /** * Tests if the specified object is a key in this table. * * @param key * possible key. * @return true if and only if the specified object is a key in * this table, as determined by the equals method; * false otherwise. * @throws NullPointerException * if the key is null. */ public boolean containsKey(K key) { return map.containsKey(key); } /** * Returns true if this map maps one or more keys to the specified * value. Note: This method requires a full internal traversal of the hash * table, and so is much slower than method containsKey. * * @param value * value whose presence in this map is to be tested. * @return true if this map maps one or more keys to the specified * value. * @throws NullPointerException * if the value is null. */ public boolean containsValue(V value) { return map.containsValue(value); } /** * Maps the specified key to the specified value in this * table. Neither the key nor the value can be null. * *

* The value can be retrieved by calling the get method with a key * that is equal to the original key. * * @param key * the table key. * @param value * the value. * @return the previous value of the specified key in this table, or * null if it did not have one. * @throws NullPointerException * if the key or value is null. */ public V put(K key, V value) { return map.put(key, value); } /** * If the specified key is not already associated with a value, associate it * with the given value. This is equivalent to * *

	 * if (!map.containsKey(key))
	 * 	return map.put(key, value);
	 * else
	 * 	return map.get(key);
	 * 
* * Except that the action is performed atomically. * * @param key * key with which the specified value is to be associated. * @param value * value to be associated with the specified key. * @return previous value associated with specified key, or null if * there was no mapping for key. * @throws NullPointerException * if the specified key or value is null. */ public V putIfAbsent(K key, V value) { if (map.containsKey(key) && map.get(key).equals(value)) return map.put(key, value); return null; } /** * Copies all of the mappings from the specified map to this one. * * These mappings replace any mappings that this map had for any of the keys * currently in the specified Map. * * @param t * Mappings to be stored in this map. */ public void putAll(Map t) { map.putAll(t); } /** * Removes the key (and its corresponding value) from this table. This * method does nothing if the key is not in the table. * * @param key * the key that needs to be removed. * @return the value to which the key had been mapped in this table, or * null if the key did not have a mapping. * @throws NullPointerException * if the key is null. */ public V remove(K key) { return map.remove(key); } /** * Remove entry for key only if currently mapped to given value. Acts as * *
	 * if (map.get(key).equals(value))
	 * {
	 * 	map.remove(key);
	 * 	return true;
	 * }
	 * else
	 * 	return false;
	 * 
* * except that the action is performed atomically. * * @param key * key with which the specified value is associated. * @param value * value associated with the specified key. * @return true if the value was removed * @throws NullPointerException * if the specified key is null. */ public boolean remove(K key, V value) { if (map.containsKey(key) && map.get(key).equals(value)) { map.remove(key); return true; } return false; } /** * Replace entry for key only if currently mapped to given value. Acts as * *
	 * if (map.get(key).equals(oldValue))
	 * {
	 * 	map.put(key, newValue);
	 * 	return true;
	 * }
	 * else
	 * 	return false;
	 * 
* * except that the action is performed atomically. * * @param key * key with which the specified value is associated. * @param oldValue * value expected to be associated with the specified key. * @param newValue * value to be associated with the specified key. * @return true if the value was replaced * @throws NullPointerException * if the specified key or values are null. */ public boolean replace(K key, V oldValue, V newValue) { V value = map.put(key, newValue); return value != null && value.equals(oldValue); } /** * Replace entry for key only if currently mapped to some value. Acts as * *
	 *  if ((map.containsKey(key)) {
	 *     return map.put(key, value);
	 * } else return null;
	 * 
* * except that the action is performed atomically. * * @param key * key with which the specified value is associated. * @param value * value to be associated with the specified key. * @return previous value associated with specified key, or null if * there was no mapping for key. * @throws NullPointerException * if the specified key or value is null. */ public V replace(K key, V value) { return map.put(key, value); } /** * Removes all mappings from this map. */ public void clear() { map.clear(); } /** * Returns a set view of the keys contained in this map. The set is backed * by the map, so changes to the map are reflected in the set, and * vice-versa. The set supports element removal, which removes the * corresponding mapping from this map, via the Iterator.remove, * Set.remove, removeAll, retainAll, and * clear operations. It does not support the add or * addAll operations. The view's returned iterator is a * "weakly consistent" iterator that will never throw * {@link java.util.ConcurrentModificationException}, and guarantees to * traverse elements as they existed upon construction of the iterator, and * may (but is not guaranteed to) reflect any modifications subsequent to * construction. * * @return a set view of the keys contained in this map. */ public Set keySet() { return map.keySet(); } /** * Returns a collection view of the values contained in this map. The * collection is backed by the map, so changes to the map are reflected in * the collection, and vice-versa. The collection supports element removal, * which removes the corresponding mapping from this map, via the * Iterator.remove, Collection.remove, removeAll, * retainAll, and clear operations. It does not support * the add or addAll operations. The view's returned * iterator is a "weakly consistent" iterator that will never throw * {@link java.util.ConcurrentModificationException}, and guarantees to * traverse elements as they existed upon construction of the iterator, and * may (but is not guaranteed to) reflect any modifications subsequent to * construction. * * @return a collection view of the values contained in this map. */ public Collection values() { return map.values(); } /** * Returns a collection view of the mappings contained in this map. Each * element in the returned collection is a Map.Entry. The * collection is backed by the map, so changes to the map are reflected in * the collection, and vice-versa. The collection supports element removal, * which removes the corresponding mapping from the map, via the * Iterator.remove, Collection.remove, removeAll, * retainAll, and clear operations. It does not support * the add or addAll operations. The view's returned * iterator is a "weakly consistent" iterator that will never throw * {@link java.util.ConcurrentModificationException}, and guarantees to * traverse elements as they existed upon construction of the iterator, and * may (but is not guaranteed to) reflect any modifications subsequent to * construction. * * @return a collection view of the mappings contained in this map. */ public Set> entrySet() { return map.entrySet(); } /** * Returns an enumeration of the keys in this table. * * @return an enumeration of the keys in this table. * @see #keySet */ public Set keys() { return map.keySet(); } /** * Returns an enumeration of the values in this table. * * @return an enumeration of the values in this table. * @see #values */ public Collection elements() { return map.values(); } /** * Compares the specified object with this map for equality. Returns * true if the given object is also a map and the two maps * represent the same mappings. More formally, two maps t1 and * t2 represent the same mappings if * t1.keySet().equals(t2.keySet()) and for every key k in * t1.keySet(), (t1.get(k)==null ? t2.get(k)==null : * t1.get(k).equals(t2.get(k))) . This ensures that the equals * method works properly across different implementations of the map * interface. *

* * This implementation first checks if the specified object is this map; if * so it returns true. Then, it checks if the specified object is a * map whose size is identical to the size of this set; if not, it returns * false. If so, it iterates over this map's entrySet * collection, and checks that the specified map contains each mapping that * this map contains. If the specified map fails to contain such a mapping, * false is returned. If the iteration completes, true is * returned. * * @param o * object to be compared for equality with this map. * @return true if the specified object is equal to this map. */ public boolean equals(Object o) { return map.equals(o); } /** * Returns the hash code value for this map. The hash code of a map is * defined to be the sum of the hash codes of each entry in the map's * entrySet() view. This ensures that t1.equals(t2) * implies that t1.hashCode()==t2.hashCode() for any two maps * t1 and t2, as required by the general contract of * Object.hashCode. *

* * This implementation iterates over entrySet(), calling * hashCode on each element (entry) in the Collection, and adding * up the results. * * @return the hash code value for this map. * @see java.util.Map.Entry#hashCode() * @see Object#hashCode() * @see Object#equals(Object) * @see Set#equals(Object) */ public int hashCode() { return map.hashCode(); } /** * Returns a string representation of this map. The string representation * consists of a list of key-value mappings in the order returned by the * map's entrySet view's iterator, enclosed in braces ( * "{}"). Adjacent mappings are separated by the characters * ", " (comma and space). Each key-value mapping is rendered as * the key followed by an equals sign ("=") followed by the * associated value. Keys and values are converted to strings as by * String.valueOf(Object). *

* * This implementation creates an empty string buffer, appends a left brace, * and iterates over the map's entrySet view, appending the string * representation of each map.entry in turn. After appending each * entry except the last, the string ", " is appended. Finally a * right brace is appended. A string is obtained from the stringBuffer, and * returned. * * @return a String representation of this map. */ public String toString() { return this.getClass().getName() + ": " + map.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy