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

de.alpharogroup.collections.map.InsertionOrderMap Maven / Gradle / Ivy

There is a newer version: 8.4
Show newest version
/**
 * The MIT License
 *
 * Copyright (C) 2015 Asterios Raptis
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package de.alpharogroup.collections.map;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * The class {@link InsertionOrderMap} overwrites the put-method from the LinkedHashMap.
 * That inserts the value to the right order it was inserted in the Map. Note that the difference in
 * the LinkedHashMap is the order does not change if we put the same key with a new value. In this
 * class the order changes when we put a new value with the same key.
 * 
 * @author Asterios Raptis
 * @version 1.0
 * @param 
 *            the generic type of keys maintained by this map
 * @param 
 *            the generic type of mapped values
 * @see java.util.LinkedHashMap
 */
public class InsertionOrderMap extends LinkedHashMap
{

	/**
	 * The serialVersionUID.
	 */
	private static final long serialVersionUID = -3585706680928306464L;

	/**
	 * Constructs an empty insertion-ordered {@link InsertionOrderMap} instance with a default
	 * capacity (16) and load factor (0.75).
	 */
	public InsertionOrderMap()
	{
	}

	/**
	 * Constructs an empty insertion-ordered {@link InsertionOrderMap} instance with the specified
	 * initial capacity and a default load factor (0.75).
	 * 
	 * @param initialCapacity
	 *            the initial capacity.
	 * @throws IllegalArgumentException
	 *             if the initial capacity is negative.
	 */
	public InsertionOrderMap(final int initialCapacity)
	{
		super(initialCapacity);
	}

	/**
	 * Constructs an empty insertion-ordered {@link InsertionOrderMap} instance with the specified
	 * initial capacity and load factor.
	 * 
	 * @param initialCapacity
	 *            the initial capacity.
	 * @param loadFactor
	 *            the load factor.
	 * @throws IllegalArgumentException
	 *             if the initial capacity is negative or the load factor is nonpositive.
	 */
	public InsertionOrderMap(final int initialCapacity, final float loadFactor)
	{
		super(initialCapacity, loadFactor);
	}

	/**
	 * Constructs an empty {@link InsertionOrderMap} instance with the specified initial capacity,
	 * load factor and ordering mode.
	 * 
	 * @param initialCapacity
	 *            the initial capacity.
	 * @param loadFactor
	 *            the load factor.
	 * @param accessOrder
	 *            the ordering mode - true for access-order, false for
	 *            insertion-order.
	 * @throws IllegalArgumentException
	 *             if the initial capacity is negative or the load factor is nonpositive.
	 */
	public InsertionOrderMap(final int initialCapacity, final float loadFactor,
		final boolean accessOrder)
	{
		super(initialCapacity, loadFactor, accessOrder);
	}

	/**
	 * Constructs an insertion-ordered {@link InsertionOrderMap} instance with the same mappings as
	 * the specified map. The InsertionOrderMap instance is created with a a default load
	 * factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map.
	 * 
	 * @param m
	 *            the map whose mappings are to be placed in this map.
	 * @throws NullPointerException
	 *             if the specified map is null.
	 */
	public InsertionOrderMap(final Map m)
	{
		super(m);
	}

	/**
	 * Associates the specified value with the specified key in this map. If the map previously
	 * contained a mapping for this key, the old value is removed and then insert new with the same
	 * key and the new value. That inserts the value to the right order it was inserted in the Map.
	 * Note that the difference in the LinkedHashMap is the order does not change if we put the same
	 * key with a new value. In this class the order changes when we put a new value with the same
	 * key.
	 * 
	 * @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. A null return can also indicate that the HashMap previously
	 *         associated null with the specified key.
	 */
	@Override
	public V put(final K key, final V value)
	{
		V retObj = null;
		if (super.containsKey(key))
		{
			retObj = super.remove(key);
			super.put(key, value);
		}
		else
		{
			retObj = super.put(key, value);
		}
		return retObj;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy