001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.lang.reflect.Constructor;
021    
022    import java.util.HashMap;
023    import java.util.LinkedHashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Raymond Aug??
029     */
030    public class MapUtil {
031    
032            public static <K, V> void copy(
033                    Map<? extends K, ? extends V> master, Map<? super K, ? super V> copy) {
034    
035                    copy.clear();
036    
037                    merge(master, copy);
038            }
039    
040            public static <K, V> Map<K, V> filter(
041                    Map<? extends K, ? extends V> inputMap, Map<K, V> outputMap,
042                    PredicateFilter<K> keyPredicateFilter) {
043    
044                    for (Map.Entry<? extends K, ? extends V> entry : inputMap.entrySet()) {
045                            if (keyPredicateFilter.filter(entry.getKey())) {
046                                    outputMap.put(entry.getKey(), entry.getValue());
047                            }
048                    }
049    
050                    return outputMap;
051            }
052    
053            public static <T> Map<T, T> fromArray(T... array) {
054                    if ((array.length % 2) != 0) {
055                            throw new IllegalArgumentException(
056                                    "Array length is not an even number");
057                    }
058    
059                    Map<T, T> map = new HashMap<T, T>();
060    
061                    for (int i = 0; i < array.length; i += 2) {
062                            T key = array[i];
063                            T value = array[i + 1];
064    
065                            map.put(key, value);
066                    }
067    
068                    return map;
069            }
070    
071            public static boolean getBoolean(Map<String, ?> map, String key) {
072                    return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
073            }
074    
075            public static boolean getBoolean(
076                    Map<String, ?> map, String key, boolean defaultValue) {
077    
078                    Object value = map.get(key);
079    
080                    if (value == null) {
081                            return defaultValue;
082                    }
083    
084                    if (value instanceof Boolean) {
085                            return (Boolean)value;
086                    }
087    
088                    if (value instanceof String[]) {
089                            String[] array = (String[])value;
090    
091                            if (array.length == 0) {
092                                    return defaultValue;
093                            }
094    
095                            return GetterUtil.getBoolean(array[0], defaultValue);
096                    }
097    
098                    return GetterUtil.getBoolean(String.valueOf(value), defaultValue);
099            }
100    
101            public static double getDouble(Map<String, ?> map, String key) {
102                    return getDouble(map, key, GetterUtil.DEFAULT_DOUBLE);
103            }
104    
105            public static double getDouble(
106                    Map<String, ?> map, String key, double defaultValue) {
107    
108                    Object value = map.get(key);
109    
110                    if (value == null) {
111                            return defaultValue;
112                    }
113    
114                    if (value instanceof Double) {
115                            return (Double)value;
116                    }
117    
118                    if (value instanceof String[]) {
119                            String[] array = (String[])value;
120    
121                            if (array.length == 0) {
122                                    return defaultValue;
123                            }
124    
125                            return GetterUtil.getDouble(array[0], defaultValue);
126                    }
127    
128                    return GetterUtil.getDouble(String.valueOf(value), defaultValue);
129            }
130    
131            public static int getInteger(Map<String, ?> map, String key) {
132                    return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
133            }
134    
135            public static int getInteger(
136                    Map<String, ?> map, String key, int defaultValue) {
137    
138                    Object value = map.get(key);
139    
140                    if (value == null) {
141                            return defaultValue;
142                    }
143    
144                    if (value instanceof Integer) {
145                            return (Integer)value;
146                    }
147    
148                    if (value instanceof String[]) {
149                            String[] array = (String[])value;
150    
151                            if (array.length == 0) {
152                                    return defaultValue;
153                            }
154    
155                            return GetterUtil.getInteger(array[0], defaultValue);
156                    }
157    
158                    return GetterUtil.getInteger(String.valueOf(value), defaultValue);
159            }
160    
161            public static long getLong(Map<Long, Long> map, long key) {
162                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
163            }
164    
165            public static long getLong(
166                    Map<Long, Long> map, long key, long defaultValue) {
167    
168                    Long value = map.get(key);
169    
170                    if (value == null) {
171                            return defaultValue;
172                    }
173                    else {
174                            return value;
175                    }
176            }
177    
178            public static long getLong(Map<String, ?> map, String key) {
179                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
180            }
181    
182            public static long getLong(
183                    Map<String, ?> map, String key, long defaultValue) {
184    
185                    Object value = map.get(key);
186    
187                    if (value == null) {
188                            return defaultValue;
189                    }
190    
191                    if (value instanceof Long) {
192                            return (Long)value;
193                    }
194    
195                    if (value instanceof String[]) {
196                            String[] array = (String[])value;
197    
198                            if (array.length == 0) {
199                                    return defaultValue;
200                            }
201    
202                            return GetterUtil.getLong(array[0], defaultValue);
203                    }
204    
205                    return GetterUtil.getLong(String.valueOf(value), defaultValue);
206            }
207    
208            public static short getShort(Map<String, ?> map, String key) {
209                    return getShort(map, key, GetterUtil.DEFAULT_SHORT);
210            }
211    
212            public static short getShort(
213                    Map<String, ?> map, String key, short defaultValue) {
214    
215                    Object value = map.get(key);
216    
217                    if (value == null) {
218                            return defaultValue;
219                    }
220    
221                    if (value instanceof Short) {
222                            return (Short)value;
223                    }
224    
225                    if (value instanceof String[]) {
226                            String[] array = (String[])value;
227    
228                            if (array.length == 0) {
229                                    return defaultValue;
230                            }
231    
232                            return GetterUtil.getShort(array[0], defaultValue);
233                    }
234    
235                    return GetterUtil.getShort(String.valueOf(value), defaultValue);
236            }
237    
238            public static String getString(Map<String, ?> map, String key) {
239                    return getString(map, key, GetterUtil.DEFAULT_STRING);
240            }
241    
242            public static String getString(
243                    Map<String, ?> map, String key, String defaultValue) {
244    
245                    Object value = map.get(key);
246    
247                    if (value == null) {
248                            return defaultValue;
249                    }
250    
251                    if (value instanceof String) {
252                            return GetterUtil.getString((String)value, defaultValue);
253                    }
254    
255                    if (value instanceof String[]) {
256                            String[] array = (String[])value;
257    
258                            if (array.length == 0 ) {
259                                    return defaultValue;
260                            }
261    
262                            return GetterUtil.getString(array[0], defaultValue);
263                    }
264    
265                    return GetterUtil.getString(String.valueOf(value), defaultValue);
266            }
267    
268            public static boolean isEmpty(Map<?, ?> map) {
269                    if ((map == null) || map.isEmpty()) {
270                            return true;
271                    }
272    
273                    return false;
274            }
275    
276            public static boolean isNotEmpty(Map<?, ?> map) {
277                    return !isEmpty(map);
278            }
279    
280            public static <K, V> void merge(
281                    Map<? extends K, ? extends V> master, Map<? super K, ? super V> copy) {
282    
283                    copy.putAll(master);
284            }
285    
286            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
287                    String[] params) {
288    
289                    return toLinkedHashMap(params, StringPool.COLON);
290            }
291    
292            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
293                    String[] params, String delimiter) {
294    
295                    LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
296    
297                    if (params == null) {
298                            return (LinkedHashMap<String, T>)map;
299                    }
300    
301                    for (String param : params) {
302                            String[] kvp = StringUtil.split(param, delimiter);
303    
304                            if (kvp.length == 2) {
305                                    map.put(kvp[0], kvp[1]);
306                            }
307                            else if (kvp.length == 3) {
308                                    String type = kvp[2];
309    
310                                    if (StringUtil.equalsIgnoreCase(type, "boolean") ||
311                                            type.equals(Boolean.class.getName())) {
312    
313                                            map.put(kvp[0], Boolean.valueOf(kvp[1]));
314                                    }
315                                    else if (StringUtil.equalsIgnoreCase(type, "double") ||
316                                                     type.equals(Double.class.getName())) {
317    
318                                            map.put(kvp[0], new Double(kvp[1]));
319                                    }
320                                    else if (StringUtil.equalsIgnoreCase(type, "int") ||
321                                                     type.equals(Integer.class.getName())) {
322    
323                                            map.put(kvp[0], new Integer(kvp[1]));
324                                    }
325                                    else if (StringUtil.equalsIgnoreCase(type, "long") ||
326                                                     type.equals(Long.class.getName())) {
327    
328                                            map.put(kvp[0], new Long(kvp[1]));
329                                    }
330                                    else if (StringUtil.equalsIgnoreCase(type, "short") ||
331                                                     type.equals(Short.class.getName())) {
332    
333                                            map.put(kvp[0], new Short(kvp[1]));
334                                    }
335                                    else if (type.equals(String.class.getName())) {
336                                            map.put(kvp[0], kvp[1]);
337                                    }
338                                    else {
339                                            try {
340                                                    Class<?> clazz = Class.forName(type);
341    
342                                                    Constructor<?> constructor = clazz.getConstructor(
343                                                            String.class);
344    
345                                                    map.put(kvp[0], constructor.newInstance(kvp[1]));
346                                            }
347                                            catch (Exception e) {
348                                                    _log.error(e.getMessage(), e);
349                                            }
350                                    }
351                            }
352                    }
353    
354                    return (LinkedHashMap<String, T>)map;
355            }
356    
357            public static String toString(Map<?, ?> map) {
358                    return toString(map, null, null);
359            }
360    
361            public static String toString(
362                    Map<?, ?> map, String hideIncludesRegex, String hideExcludesRegex) {
363    
364                    if (isEmpty(map)) {
365                            return StringPool.OPEN_CURLY_BRACE + StringPool.CLOSE_CURLY_BRACE;
366                    }
367    
368                    StringBundler sb = new StringBundler(map.size() * 4 + 1);
369    
370                    sb.append(StringPool.OPEN_CURLY_BRACE);
371    
372                    for (Map.Entry<?, ?> entry : map.entrySet()) {
373                            Object key = entry.getKey();
374                            Object value = entry.getValue();
375    
376                            String keyString = String.valueOf(key);
377    
378                            if (hideIncludesRegex != null) {
379                                    if (!keyString.matches(hideIncludesRegex)) {
380                                            value = "********";
381                                    }
382                            }
383    
384                            if (hideExcludesRegex != null) {
385                                    if (keyString.matches(hideExcludesRegex)) {
386                                            value = "********";
387                                    }
388                            }
389    
390                            sb.append(keyString);
391                            sb.append(StringPool.EQUAL);
392    
393                            if (value instanceof Map<?, ?>) {
394                                    sb.append(MapUtil.toString((Map<?, ?>)value));
395                            }
396                            else if (value instanceof String[]) {
397                                    String valueString = StringUtil.merge(
398                                            (String[])value, StringPool.COMMA_AND_SPACE);
399    
400                                    sb.append(
401                                            StringPool.OPEN_BRACKET.concat(valueString).concat(
402                                                    StringPool.CLOSE_BRACKET));
403                            }
404                            else {
405                                    sb.append(value);
406                            }
407    
408                            sb.append(StringPool.COMMA_AND_SPACE);
409                    }
410    
411                    sb.setStringAt(StringPool.CLOSE_CURLY_BRACE, sb.index() - 1);
412    
413                    return sb.toString();
414            }
415    
416            private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
417    
418    }