org.carrot2.util.MapUtils Maven / Gradle / Ivy
Show all versions of carrot2-mini Show documentation
/*
* Carrot2 project.
*
* Copyright (C) 2002-2016, Dawid Weiss, Stanisław Osiński.
* All rights reserved.
*
* Refer to the full license file "carrot2.LICENSE"
* in the root folder of the repository checkout or at:
* http://www.carrot2.org/carrot2.LICENSE
*/
package org.carrot2.util;
import java.util.*;
import java.util.Map.Entry;
import org.carrot2.shaded.guava.common.collect.Lists;
import org.carrot2.shaded.guava.common.collect.Maps;
/**
* Utilities for working with {@link Map}s.
*/
public class MapUtils
{
private MapUtils()
{
}
public static HashMap asHashMap(Map map)
{
if (map instanceof HashMap)
{
return (HashMap) map;
}
else
{
return new HashMap(map);
}
}
/**
* Iterates through entries of the input map
and for values being String
* arrays puts the first element of the map in the result map. Scalar values get
* copied to the output map unchanged.
*
* This method might be useful for "unpacking" values from servlet HTTP requests.
*/
@SuppressWarnings({"rawtypes"})
public static Map unpack(final Map map)
{
final Map result = Maps.newHashMap();
for (Object entry : map.entrySet())
{
final Map.Entry mapEntry = (Entry) entry;
final String parameterName = (String) mapEntry.getKey();
final String [] parameterValues = (String []) mapEntry.getValue();
if (parameterValues.length == 1)
{
result.put(parameterName, parameterValues[0]);
}
else
{
result.put(parameterName, Lists.newArrayList(parameterValues));
}
}
return result;
}
public static Integer increment(Map map, K key)
{
return increment(map, key, 1);
}
public static Integer increment(Map map, K key, int value)
{
final Integer current = map.get(key);
if (current == null)
{
map.put(key, value);
return value;
}
else
{
map.put(key, current + value);
return current + value;
}
}
}