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

com.github.napp.util.mass.MapUtil Maven / Gradle / Ivy

There is a newer version: 1.1.8
Show newest version
package com.github.napp.util.mass;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import com.github.napp.util.mass.InternalEvaluator.EvaluationResult;
import com.github.napp.util.mass.InternalEvaluator.IDupeChecker;



/**
 * @author Alexandru Bledea
 * @since Jul 9, 2013
 */
public class MapUtil {

	/**
	 * Used to generate a map from the collection if items by using a evaluator. 
* If there are two objects with the same key, the latest entry will override the previous entry
* If there are null values in the collection and objects that evaluate to null they are skipped
* @param items the collection from which we create the map * @param generator the key generator * @return a map created from the items using the generator, if the key evaluates to null, it is not added to the map, if two items evaluate to the same key, the latest one will override any previous values */ public static Map generateMap(Collection items, IEvaluator generator) { try { return generateMap(items, generator, true); } catch (DuplicateKeyException e) { return null; // not going to happen } } /** * Used to generate a map from the collection if items by using a evaluator.
* Use allowDupes if you want values that evaluate to the same key to be added to the map. This will override the previous entry.
* If there are null values in the collection and objects that evaluate to null they are skipped
* @param items the collection from which we create the map * @param generator the key generator * @param allowDupes if we allow duplicate keys in the map, the latest one will always override the previous value, if we don't support it, throw {@link de.xwic.appkit.core.util.DuplicateKeyException} * @return a map created from the items using the generator, if the key evaluates to null, it is not added to the map * @throws DuplicateKeyException if we have a duplicate key and we don't allow that */ public static Map generateMap(Collection items, IEvaluator generator, boolean allowDupes) throws DuplicateKeyException { return generateMap(items, generator, allowDupes, true); } /** * Used to generate a map from the collection if items by using a evaluator.
* Use allowDupes if you want values that evaluate to the same key to be added to the map. This will override the previous entry.
* Use skipNullObjects to avoid {@link java.lang.NullPointerException} if a null value is in the collection
* Objects that evaluate to null won't be added to the map * @param items the collection from which we create the map * @param generator the key generator * @param allowDupes if we allow duplicate keys in the map, the latest one will always override the previous value, if we don't support it, throw {@link de.xwic.appkit.core.util.DuplicateKeyException} * @param skipNullObjects if we want to skip null objects, if we don't skip these objects, we will get a {@link java.lang.NullPointerException} * @return a map created from the items using the generator, if the key evaluates to null, it is not added to the map * @throws DuplicateKeyException if we have a duplicate key and we don't allow that * @throws NullPointerException if there is a null value in the collection and we don't skip it */ public static Map generateMap(Collection items, IEvaluator generator, boolean allowDupes, boolean skipNullObjects) throws DuplicateKeyException { return generateMap(items, generator, allowDupes, skipNullObjects, true); } /** * Used to generate a map from the collection if items by using a evaluator.
* Use allowDupes if you want values that evaluate to the same key to be added to the map. This will override the previous entry.
* Use skipNullObjects to avoid {@link java.lang.NullPointerException} if a null value is in the collection
* Use skipNullValues to skip adding object to the map it evaluates to null * @param items the collection from which we create the map * @param generator the key generator * @param allowDupes if we allow duplicate keys in the map, the latest one will always override the previous value, if we don't support it, throw {@link de.xwic.appkit.core.util.DuplicateKeyException} * @param skipNullObjects if we want to skip null objects, if we don't skip these objects, we will get a {@link java.lang.NullPointerException} * @param skipNullValues if we want to skip the null values from being added to the map * @return a map created from the items using the generator * @throws DuplicateKeyException if we have a duplicate key and we don't allow that * @throws NullPointerException if there is a null value in the collection and we don't skip it */ public static Map generateMap(Collection items, IEvaluator generator, boolean allowDupes, boolean skipNullObjects, boolean skipNullValues) throws DuplicateKeyException { if (items == null) { items = new ArrayList(); } EvaluationResult result = new EvaluationResult(); InternalHashMap map = new InternalHashMap(); for (Obj t : items) { result = InternalEvaluator.evaluate(t, skipNullObjects, generator, skipNullValues, allowDupes, map, result); if (!result.skip()) { map.put(result.getResult(), t); } } return map; } /** * @param initializer the map initializer * @return a unmodifiable {@link java.util.HashMap} containting the values that were set using the initializer */ public static Map unmodifiableMap(IMapInitializer initializer) { Map map = new HashMap(); initializer.initMap(map); return Collections.unmodifiableMap(map); } /** * equivallent to
* if (map.contains(key){
*   return map.get(key);
* }
* ? x = initializer.evaluate(initValue); // init something that should be mapped to the key
* map.put(key, x);
* return x;
* * @deprecated consider using AIMap instead * @param map * @param key * @param initValue * @param initializer * @return */ @Deprecated public static V get(Map map, K key, I initValue, IEvaluator initializer) { if (map.containsKey(key)) { return map.get(key); } // risky but convinient, this will allow us to reuse initializers // java 6 doesn't reify generics // if it did reify generics we would have had O extends X instead of X extends O // which would have been a much safer choice, oh well... V call = (V) initializer.evaluate(initValue); map.put(key, call); return call; } /** * equivallent to
* if (map.contains(key){
*   return map.get(key);
* }
* ? x = initializer.call(); // init something that should be mapped to the key
* map.put(key, x);
* return x;
* * @deprecated consider using AIMap instead * @param map * @param key * @param initializer * @return * */ @Deprecated public static X get(Map map, K key, final LazyInit initializer) { return get(map, key, null, initializer); } /** * @author Alexandru Bledea * @since Jul 31, 2013 * @param * @param */ private static class InternalHashMap extends HashMap implements IDupeChecker { /* (non-Javadoc) * @see de.xwic.appkit.core.util.InternalEvaluator.DupeChecker#checkDupe(java.lang.Object) */ @Override public boolean checkIfDupe(K what) { return containsKey(what); } } /** * used to create objects in a lazy manner, used when you may need something

* for instance
* if (condition1 & condition2 & condition3 & condition4){
*   return initializer.call();
* } * @author Alexandru Bledea * @since Aug 12, 2013 * @param * */ public abstract static class LazyInit implements IEvaluator, Callable { /* (non-Javadoc) * @see de.xwic.appkit.core.util.IEvaluator#evaluate(java.lang.Object) */ @Override public final V evaluate(Object obj) { try { return call(); } catch (Exception e) { throw new RuntimeException("Error creating new object", e); } } } /** * used to create objects in a lazy manner, used when you may need something

* for instance
* if (condition1 & condition2 & condition3 & condition4){
*   return initializer.call();
* } * @author Alexandru Bledea * @since Aug 12, 2013 * @param * */ public static class LazyCallable extends LazyInit { protected Callable callable; /** * @param callable */ public LazyCallable(Callable callable) { if ((this.callable = callable) == null) { throw new RuntimeException("Missing initializer"); } } /* (non-Javadoc) * @see java.util.concurrent.Callable#call() */ @Override public final V call() throws Exception { return callable.call(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy