001    /**
002     * Copyright (c) 2000-2013 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.LinkedHashMap;
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Raymond Aug??
028     */
029    public class MapUtil {
030    
031            public static <K, V> void copy(
032                    Map<K, V> master, Map<? super K, ? super V> copy) {
033    
034                    copy.clear();
035    
036                    merge(master, copy);
037            }
038    
039            public static <K, V> Map<K, V> filter(
040                    Map<K, V> inputMap, Map<K, V> outputMap,
041                    PredicateFilter<K> keyPredicateFilter) {
042    
043                    for (Map.Entry<K, V> entry : inputMap.entrySet()) {
044                            if (keyPredicateFilter.filter(entry.getKey())) {
045                                    outputMap.put(entry.getKey(), entry.getValue());
046                            }
047                    }
048    
049                    return outputMap;
050            }
051    
052            public static boolean getBoolean(Map<String, ?> map, String key) {
053                    return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
054            }
055    
056            public static boolean getBoolean(
057                    Map<String, ?> map, String key, boolean defaultValue) {
058    
059                    Object value = map.get(key);
060    
061                    if (value == null) {
062                            return defaultValue;
063                    }
064    
065                    if (value instanceof Boolean) {
066                            return (Boolean)value;
067                    }
068    
069                    if (value instanceof String[]) {
070                            String[] array = (String[])value;
071    
072                            if (array.length == 0) {
073                                    return defaultValue;
074                            }
075    
076                            return GetterUtil.getBoolean(array[0], defaultValue);
077                    }
078    
079                    return GetterUtil.getBoolean(String.valueOf(value), defaultValue);
080            }
081    
082            public static double getDouble(Map<String, ?> map, String key) {
083                    return getDouble(map, key, GetterUtil.DEFAULT_DOUBLE);
084            }
085    
086            public static double getDouble(
087                    Map<String, ?> map, String key, double defaultValue) {
088    
089                    Object value = map.get(key);
090    
091                    if (value == null) {
092                            return defaultValue;
093                    }
094    
095                    if (value instanceof Double) {
096                            return (Double)value;
097                    }
098    
099                    if (value instanceof String[]) {
100                            String[] array = (String[])value;
101    
102                            if (array.length == 0) {
103                                    return defaultValue;
104                            }
105    
106                            return GetterUtil.getDouble(array[0], defaultValue);
107                    }
108    
109                    return GetterUtil.getDouble(String.valueOf(value), defaultValue);
110            }
111    
112            public static int getInteger(Map<String, ?> map, String key) {
113                    return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
114            }
115    
116            public static int getInteger(
117                    Map<String, ?> map, String key, int defaultValue) {
118    
119                    Object value = map.get(key);
120    
121                    if (value == null) {
122                            return defaultValue;
123                    }
124    
125                    if (value instanceof Integer) {
126                            return (Integer)value;
127                    }
128    
129                    if (value instanceof String[]) {
130                            String[] array = (String[])value;
131    
132                            if (array.length == 0) {
133                                    return defaultValue;
134                            }
135    
136                            return GetterUtil.getInteger(array[0], defaultValue);
137                    }
138    
139                    return GetterUtil.getInteger(String.valueOf(value), defaultValue);
140            }
141    
142            public static long getLong(Map<Long, Long> map, long key) {
143                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
144            }
145    
146            public static long getLong(
147                    Map<Long, Long> map, long key, long defaultValue) {
148    
149                    Long value = map.get(key);
150    
151                    if (value == null) {
152                            return defaultValue;
153                    }
154                    else {
155                            return value;
156                    }
157            }
158    
159            public static long getLong(Map<String, ?> map, String key) {
160                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
161            }
162    
163            public static long getLong(
164                    Map<String, ?> map, String key, long defaultValue) {
165    
166                    Object value = map.get(key);
167    
168                    if (value == null) {
169                            return defaultValue;
170                    }
171    
172                    if (value instanceof Long) {
173                            return (Long)value;
174                    }
175    
176                    if (value instanceof String[]) {
177                            String[] array = (String[])value;
178    
179                            if (array.length == 0) {
180                                    return defaultValue;
181                            }
182    
183                            return GetterUtil.getLong(array[0], defaultValue);
184                    }
185    
186                    return GetterUtil.getLong(String.valueOf(value), defaultValue);
187            }
188    
189            public static short getShort(Map<String, ?> map, String key) {
190                    return getShort(map, key, GetterUtil.DEFAULT_SHORT);
191            }
192    
193            public static short getShort(
194                    Map<String, ?> map, String key, short defaultValue) {
195    
196                    Object value = map.get(key);
197    
198                    if (value == null) {
199                            return defaultValue;
200                    }
201    
202                    if (value instanceof Short) {
203                            return (Short)value;
204                    }
205    
206                    if (value instanceof String[]) {
207                            String[] array = (String[])value;
208    
209                            if (array.length == 0) {
210                                    return defaultValue;
211                            }
212    
213                            return GetterUtil.getShort(array[0], defaultValue);
214                    }
215    
216                    return GetterUtil.getShort(String.valueOf(value), defaultValue);
217            }
218    
219            public static String getString(Map<String, ?> map, String key) {
220                    return getString(map, key, GetterUtil.DEFAULT_STRING);
221            }
222    
223            public static String getString(
224                    Map<String, ?> map, String key, String defaultValue) {
225    
226                    Object value = map.get(key);
227    
228                    if (value == null) {
229                            return defaultValue;
230                    }
231    
232                    if (value instanceof String) {
233                            return GetterUtil.getString((String)value, defaultValue);
234                    }
235    
236                    if (value instanceof String[]) {
237                            String[] array = (String[])value;
238    
239                            if (array.length == 0 ) {
240                                    return defaultValue;
241                            }
242    
243                            return GetterUtil.getString(array[0], defaultValue);
244                    }
245    
246                    return GetterUtil.getString(String.valueOf(value), defaultValue);
247            }
248    
249            public static <K, V> void merge(
250                    Map<K, V> master, Map<? super K, ? super V> copy) {
251    
252                    copy.putAll(master);
253            }
254    
255            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
256                    String[] params) {
257    
258                    return toLinkedHashMap(params, StringPool.COLON);
259            }
260    
261            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
262                    String[] params, String delimiter) {
263    
264                    LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
265    
266                    for (String param : params) {
267                            String[] kvp = StringUtil.split(param, delimiter);
268    
269                            if (kvp.length == 2) {
270                                    map.put(kvp[0], kvp[1]);
271                            }
272                            else if (kvp.length == 3) {
273                                    String type = kvp[2];
274    
275                                    if (StringUtil.equalsIgnoreCase(type, "boolean") ||
276                                            type.equals(Boolean.class.getName())) {
277    
278                                            map.put(kvp[0], Boolean.valueOf(kvp[1]));
279                                    }
280                                    else if (StringUtil.equalsIgnoreCase(type, "double") ||
281                                                     type.equals(Double.class.getName())) {
282    
283                                            map.put(kvp[0], new Double(kvp[1]));
284                                    }
285                                    else if (StringUtil.equalsIgnoreCase(type, "int") ||
286                                                     type.equals(Integer.class.getName())) {
287    
288                                            map.put(kvp[0], new Integer(kvp[1]));
289                                    }
290                                    else if (StringUtil.equalsIgnoreCase(type, "long") ||
291                                                     type.equals(Long.class.getName())) {
292    
293                                            map.put(kvp[0], new Long(kvp[1]));
294                                    }
295                                    else if (StringUtil.equalsIgnoreCase(type, "short") ||
296                                                     type.equals(Short.class.getName())) {
297    
298                                            map.put(kvp[0], new Short(kvp[1]));
299                                    }
300                                    else if (type.equals(String.class.getName())) {
301                                            map.put(kvp[0], kvp[1]);
302                                    }
303                                    else {
304                                            try {
305                                                    Class<?> clazz = Class.forName(type);
306    
307                                                    Constructor<?> constructor = clazz.getConstructor(
308                                                            String.class);
309    
310                                                    map.put(kvp[0], constructor.newInstance(kvp[1]));
311                                            }
312                                            catch (Exception e) {
313                                                    _log.error(e.getMessage(), e);
314                                            }
315                                    }
316                            }
317                    }
318    
319                    return (LinkedHashMap<String, T>)map;
320            }
321    
322            public static String toString(Map<?, ?> map) {
323                    return toString(map, null, null);
324            }
325    
326            public static String toString(
327                    Map<?, ?> map, String hideIncludesRegex, String hideExcludesRegex) {
328    
329                    if ((map == null) || map.isEmpty()) {
330                            return StringPool.OPEN_CURLY_BRACE + StringPool.CLOSE_CURLY_BRACE;
331                    }
332    
333                    StringBundler sb = new StringBundler(map.size() * 4 + 1);
334    
335                    sb.append(StringPool.OPEN_CURLY_BRACE);
336    
337                    for (Map.Entry<?, ?> entry : map.entrySet()) {
338                            Object key = entry.getKey();
339                            Object value = entry.getValue();
340    
341                            String keyString = String.valueOf(key);
342    
343                            if (hideIncludesRegex != null) {
344                                    if (!keyString.matches(hideIncludesRegex)) {
345                                            value = "********";
346                                    }
347                            }
348    
349                            if (hideExcludesRegex != null) {
350                                    if (keyString.matches(hideExcludesRegex)) {
351                                            value = "********";
352                                    }
353                            }
354    
355                            sb.append(keyString);
356                            sb.append(StringPool.EQUAL);
357    
358                            if (value instanceof Map<?, ?>) {
359                                    sb.append(MapUtil.toString((Map<?, ?>)value));
360                            }
361                            else if (value instanceof String[]) {
362                                    String valueString = StringUtil.merge(
363                                            (String[])value, StringPool.COMMA_AND_SPACE);
364    
365                                    sb.append(
366                                            StringPool.OPEN_BRACKET.concat(valueString).concat(
367                                                    StringPool.CLOSE_BRACKET));
368                            }
369                            else {
370                                    sb.append(value);
371                            }
372    
373                            sb.append(StringPool.COMMA_AND_SPACE);
374                    }
375    
376                    sb.setStringAt(StringPool.CLOSE_CURLY_BRACE, sb.index() - 1);
377    
378                    return sb.toString();
379            }
380    
381            private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
382    
383    }