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