Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.liferay.portal.kernel.util.MapUtil Maven / Gradle / Ivy
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.kernel.util;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.lang.reflect.Constructor;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author Brian Wing Shun Chan
* @author Raymond Augé
*/
public class MapUtil {
public static void copy(
Map master, Map copy) {
copy.clear();
merge(master, copy);
}
public static Map filter(
Map inputMap, Map outputMap,
PredicateFilter keyPredicateFilter) {
for (Map.Entry entry : inputMap.entrySet()) {
if (keyPredicateFilter.filter(entry.getKey())) {
outputMap.put(entry.getKey(), entry.getValue());
}
}
return outputMap;
}
public static boolean getBoolean(Map map, String key) {
return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
}
public static boolean getBoolean(
Map map, String key, boolean defaultValue) {
Object value = map.get(key);
if (value == null) {
return defaultValue;
}
if (value instanceof Boolean) {
return (Boolean)value;
}
if (value instanceof String[]) {
String[] array = (String[])value;
if (array.length == 0) {
return defaultValue;
}
return GetterUtil.getBoolean(array[0], defaultValue);
}
return GetterUtil.getBoolean(String.valueOf(value), defaultValue);
}
public static double getDouble(Map map, String key) {
return getDouble(map, key, GetterUtil.DEFAULT_DOUBLE);
}
public static double getDouble(
Map map, String key, double defaultValue) {
Object value = map.get(key);
if (value == null) {
return defaultValue;
}
if (value instanceof Double) {
return (Double)value;
}
if (value instanceof String[]) {
String[] array = (String[])value;
if (array.length == 0) {
return defaultValue;
}
return GetterUtil.getDouble(array[0], defaultValue);
}
return GetterUtil.getDouble(String.valueOf(value), defaultValue);
}
public static int getInteger(Map map, String key) {
return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
}
public static int getInteger(
Map map, String key, int defaultValue) {
Object value = map.get(key);
if (value == null) {
return defaultValue;
}
if (value instanceof Integer) {
return (Integer)value;
}
if (value instanceof String[]) {
String[] array = (String[])value;
if (array.length == 0) {
return defaultValue;
}
return GetterUtil.getInteger(array[0], defaultValue);
}
return GetterUtil.getInteger(String.valueOf(value), defaultValue);
}
public static long getLong(Map map, long key) {
return getLong(map, key, GetterUtil.DEFAULT_LONG);
}
public static long getLong(
Map map, long key, long defaultValue) {
Long value = map.get(key);
if (value == null) {
return defaultValue;
}
else {
return value;
}
}
public static long getLong(Map map, String key) {
return getLong(map, key, GetterUtil.DEFAULT_LONG);
}
public static long getLong(
Map map, String key, long defaultValue) {
Object value = map.get(key);
if (value == null) {
return defaultValue;
}
if (value instanceof Long) {
return (Long)value;
}
if (value instanceof String[]) {
String[] array = (String[])value;
if (array.length == 0) {
return defaultValue;
}
return GetterUtil.getLong(array[0], defaultValue);
}
return GetterUtil.getLong(String.valueOf(value), defaultValue);
}
public static short getShort(Map map, String key) {
return getShort(map, key, GetterUtil.DEFAULT_SHORT);
}
public static short getShort(
Map map, String key, short defaultValue) {
Object value = map.get(key);
if (value == null) {
return defaultValue;
}
if (value instanceof Short) {
return (Short)value;
}
if (value instanceof String[]) {
String[] array = (String[])value;
if (array.length == 0) {
return defaultValue;
}
return GetterUtil.getShort(array[0], defaultValue);
}
return GetterUtil.getShort(String.valueOf(value), defaultValue);
}
public static String getString(Map map, String key) {
return getString(map, key, GetterUtil.DEFAULT_STRING);
}
public static String getString(
Map map, String key, String defaultValue) {
Object value = map.get(key);
if (value == null) {
return defaultValue;
}
if (value instanceof String) {
return GetterUtil.getString((String)value, defaultValue);
}
if (value instanceof String[]) {
String[] array = (String[])value;
if (array.length == 0 ) {
return defaultValue;
}
return GetterUtil.getString(array[0], defaultValue);
}
return GetterUtil.getString(String.valueOf(value), defaultValue);
}
public static void merge(
Map master, Map copy) {
copy.putAll(master);
}
public static LinkedHashMap toLinkedHashMap(
String[] params) {
return toLinkedHashMap(params, StringPool.COLON);
}
public static LinkedHashMap toLinkedHashMap(
String[] params, String delimiter) {
LinkedHashMap map = new LinkedHashMap();
for (String param : params) {
String[] kvp = StringUtil.split(param, delimiter);
if (kvp.length == 2) {
map.put(kvp[0], kvp[1]);
}
else if (kvp.length == 3) {
String type = kvp[2];
if (StringUtil.equalsIgnoreCase(type, "boolean") ||
type.equals(Boolean.class.getName())) {
map.put(kvp[0], Boolean.valueOf(kvp[1]));
}
else if (StringUtil.equalsIgnoreCase(type, "double") ||
type.equals(Double.class.getName())) {
map.put(kvp[0], new Double(kvp[1]));
}
else if (StringUtil.equalsIgnoreCase(type, "int") ||
type.equals(Integer.class.getName())) {
map.put(kvp[0], new Integer(kvp[1]));
}
else if (StringUtil.equalsIgnoreCase(type, "long") ||
type.equals(Long.class.getName())) {
map.put(kvp[0], new Long(kvp[1]));
}
else if (StringUtil.equalsIgnoreCase(type, "short") ||
type.equals(Short.class.getName())) {
map.put(kvp[0], new Short(kvp[1]));
}
else if (type.equals(String.class.getName())) {
map.put(kvp[0], kvp[1]);
}
else {
try {
Class clazz = Class.forName(type);
Constructor constructor = clazz.getConstructor(
String.class);
map.put(kvp[0], constructor.newInstance(kvp[1]));
}
catch (Exception e) {
_log.error(e.getMessage(), e);
}
}
}
}
return (LinkedHashMap)map;
}
public static String toString(Map map) {
return toString(map, null, null);
}
public static String toString(
Map map, String hideIncludesRegex, String hideExcludesRegex) {
if ((map == null) || map.isEmpty()) {
return StringPool.OPEN_CURLY_BRACE + StringPool.CLOSE_CURLY_BRACE;
}
StringBundler sb = new StringBundler(map.size() * 4 + 1);
sb.append(StringPool.OPEN_CURLY_BRACE);
for (Map.Entry entry : map.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
String keyString = String.valueOf(key);
if (hideIncludesRegex != null) {
if (!keyString.matches(hideIncludesRegex)) {
value = "********";
}
}
if (hideExcludesRegex != null) {
if (keyString.matches(hideExcludesRegex)) {
value = "********";
}
}
sb.append(keyString);
sb.append(StringPool.EQUAL);
if (value instanceof Map) {
sb.append(MapUtil.toString((Map)value));
}
else if (value instanceof String[]) {
String valueString = StringUtil.merge(
(String[])value, StringPool.COMMA_AND_SPACE);
sb.append(
StringPool.OPEN_BRACKET.concat(valueString).concat(
StringPool.CLOSE_BRACKET));
}
else {
sb.append(value);
}
sb.append(StringPool.COMMA_AND_SPACE);
}
sb.setStringAt(StringPool.CLOSE_CURLY_BRACE, sb.index() - 1);
return sb.toString();
}
private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
}