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

de.alpharogroup.collections.map.MapFactory 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.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections4.functors.InstantiateFactory;
import org.apache.commons.collections4.map.LazyMap;

import lombok.experimental.UtilityClass;

/**
 * The factory class {@link MapFactory} provides factory methods for create new {@link Map} objects
 *
 * @version 1.0
 * @author Asterios Raptis
 */
@UtilityClass
public final class MapFactory
{

	/**
	 * Factory method for create a map for counting elements of the given collection
	 *
	 * @param 
	 *            the generic type of the elements
	 * @param elements
	 *            the elements
	 * @return the new map ready to count elements
	 */
	public static  Map newCounterMap(Collection elements)
	{
		Map elementsCount = MapFactory.newHashMap();
		for (K element : elements)
		{
			if (elementsCount.containsKey(element))
			{
				elementsCount.merge(element, 1, Integer::sum);
				continue;
			}
			elementsCount.put(element, 0);
		}
		return elementsCount;
	}

	/**
	 * Factory method for create a new {@link InsertionOrderMap}.
	 *
	 * @param 
	 *            the generic type of the key
	 * @param 
	 *            the generic type of the value
	 *
	 * @return The new {@link InsertionOrderMap}.
	 */
	public static  Map newInsertionOrderMap()
	{
		return new InsertionOrderMap<>();
	}

	/**
	 * Factory method for create a new {@link InsertionOrderMap}.
	 *
	 * @param 
	 *            the generic type of the key
	 * @param 
	 *            the generic type of the value
	 * @param initialCapacity
	 *            the initial capacity
	 *
	 * @return The new {@link InsertionOrderMap}.
	 */
	public static  Map newInsertionOrderMap(final int initialCapacity)
	{
		return new InsertionOrderMap<>(initialCapacity);
	}

	/**
	 * Factory method for create a new {@link InsertionOrderMap}.
	 *
	 * @param 
	 *            the generic type of the key
	 * @param 
	 *            the generic type of the value
	 * @param map
	 *            the map
	 * @return The new {@link InsertionOrderMap}.
	 */
	public static  Map newInsertionOrderMap(Map map)
	{
		return new InsertionOrderMap<>(map);
	}

	/**
	 * Factory method for {@link java.util.Map} that acts like a javascript associative array.
	 *
	 * This Map is the counterpart for the associative array in js.
	 *
	 * For instance: in js you can create and fetch associative arrays like this:
	 *
	 * 
	 * $arrayObj[0]['firstName'] = 'Albert';
	 * $arrayObj[0]['lastName'] = 'Einstein';
	 * $arrayObj[1]['firstName'] = 'Neil';
	 * $arrayObj[0]['lastName'] = 'Armstrong';
	 * 
* * With this method you can do the same like this: * *
	 * final Map<Integer, Map<String, String>> arrayMap = MapExtensions.newAssosiativeArrayMap();
	 *
	 * arrayMap.get(0).put("firstName", "Albert");
	 * arrayMap.get(0).put("lastName", "Einstein");
	 * arrayMap.get(1).put("firstName", "Neil");
	 * arrayMap.get(1).put("lastName", "Armstrong");
	 * 
* * @return The {@link java.util.Map} that acts like a javascript associative array. */ public static Map> newAssosiativeArrayMap() { return newLazyMap(); } /** * Factory method for create a new {@link ConcurrentHashMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link ConcurrentHashMap}. */ public static ConcurrentHashMap newConcurrentHashMap() { return new ConcurrentHashMap<>(); } /** * Factory method for create a new {@link ConcurrentHashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param map * the map * @return The new {@link ConcurrentHashMap}. */ public static ConcurrentHashMap newConcurrentHashMap(Map map) { return new ConcurrentHashMap<>(map); } /** * Factory method for create a new {@link ConcurrentHashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param initialCapacity * the initial capacity * @return The new {@link ConcurrentHashMap}. */ public static ConcurrentHashMap newConcurrentHashMap(final int initialCapacity) { return new ConcurrentHashMap<>(initialCapacity); } /** * Factory method for create a new {@link HashMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link HashMap}. */ public static Map newHashMap() { return new HashMap<>(); } /** * Factory method for create a new {@link HashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param map * the map * @return The new {@link HashMap}. */ public static Map newHashMap(Map map) { return new HashMap<>(map); } /** * Factory method for create a new {@link LinkedHashMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link LinkedHashMap}. */ public static Map newLinkedHashMap() { return new LinkedHashMap<>(); } /** * Factory method for create a new {@link LinkedHashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param map * the map * @return The new {@link LinkedHashMap}. */ public static Map newLinkedHashMap(Map map) { return new LinkedHashMap<>(map); } /** * Factory method for create a new {@link LinkedHashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param initialCapacity * the initial capacity * * @return The new {@link LinkedHashMap}. */ public static Map newLinkedHashMap(final int initialCapacity) { return new LinkedHashMap<>(initialCapacity); } /** * Factory method for create a new {@link HashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param initialCapacity * the initial capacity * @return The new {@link HashMap}. */ public static Map newHashMap(final int initialCapacity) { return new HashMap<>(initialCapacity); } /** * Factory method for create a new {@link LazyMap} from commons-collections4 that encapsulates a * {@link HashMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link LazyMap}. */ public static Map newLazyMap() { return newLazyHashMap(); } /** * Factory method for create a new {@link LazyMap} from commons-collections4 that encapsulates a * {@link HashMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link LazyMap}. */ public static Map newLazyHashMap() { return newLazyHashMap(new HashMap()); } /** * Factory method for create a new {@link LazyMap} from commons-collections4 that encapsulates a * {@link HashMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param map * the map to initialize with it * @return The new {@link LazyMap}. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map newLazyHashMap(HashMap map) { return LazyMap.lazyMap(map, new InstantiateFactory(HashMap.class)); } /** * Factory method for create a new {@link LazyMap} from commons-collections4 that encapsulates a * {@link TreeMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link LazyMap}. */ public static Map newLazyTreeMap() { return newLazyTreeMap(new TreeMap()); } /** * Factory method for create a new {@link LazyMap} from commons-collections4 that encapsulates a * {@link TreeMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param map * the map * @return The new {@link LazyMap}. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map newLazyTreeMap(TreeMap map) { return LazyMap.lazyMap(map, new InstantiateFactory(TreeMap.class)); } /** * Factory method for create a new {@link LazyMap} from commons-collections4 that encapsulates a * {@link TreeMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param comparator * the comparator * * @return The new {@link LazyMap}. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map newLazyTreeMap(final Comparator comparator) { return LazyMap.lazyMap(new TreeMap(comparator), new InstantiateFactory(TreeMap.class)); } /** * Factory method for create a new {@link TreeMap}. * * @param * the generic type of the key * @param * the generic type of the value * * @return The new {@link TreeMap}. */ public static Map newTreeMap() { return new TreeMap<>(); } /** * Factory method for create a new {@link TreeMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param map * the map * @return The new {@link TreeMap}. */ public static Map newTreeMap(Map map) { return new TreeMap<>(map); } /** * Factory method for create a new {@link TreeMap}. * * @param * the generic type of the key * @param * the generic type of the value * @param comparator * the comparator * @return The new {@link TreeMap}. */ public static Map newTreeMap(final Comparator comparator) { return new TreeMap<>(comparator); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy