001    /**
002     * Copyright (c) 2000-2012 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.Iterator;
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<K, V> master, Map<? super K, ? super V> copy) {
034    
035                    copy.clear();
036    
037                    merge(master, copy);
038            }
039    
040            public static boolean getBoolean(Map<String, ?> map, String key) {
041                    return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
042            }
043    
044            public static boolean getBoolean(
045                    Map<String, ?> map, String key, boolean defaultValue) {
046    
047                    return GetterUtil.getBoolean(
048                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
049            }
050    
051            public static double getDouble(Map<String, ?> map, String key) {
052                    return getDouble(map, key, GetterUtil.DEFAULT_DOUBLE);
053            }
054    
055            public static double getDouble(
056                    Map<String, ?> map, String key, double defaultValue) {
057    
058                    return GetterUtil.getDouble(
059                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
060            }
061    
062            public static int getInteger(Map<String, ?> map, String key) {
063                    return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
064            }
065    
066            public static int getInteger(
067                    Map<String, ?> map, String key, int defaultValue) {
068    
069                    return GetterUtil.getInteger(
070                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
071            }
072    
073            public static long getLong(Map<Long, Long> map, long key) {
074                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
075            }
076    
077            public static long getLong(
078                    Map<Long, Long> map, long key, long defaultValue) {
079    
080                    Long value = map.get(new Long(key));
081    
082                    if (value == null) {
083                            return defaultValue;
084                    }
085                    else {
086                            return value;
087                    }
088            }
089    
090            public static long getLong(Map<String, ?> map, String key) {
091                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
092            }
093    
094            public static long getLong(
095                    Map<String, ?> map, String key, long defaultValue) {
096    
097                    return GetterUtil.getLong(
098                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
099            }
100    
101            public static short getShort(Map<String, ?> map, String key) {
102                    return getShort(map, key, GetterUtil.DEFAULT_SHORT);
103            }
104    
105            public static short getShort(
106                    Map<String, ?> map, String key, short defaultValue) {
107    
108                    return GetterUtil.getShort(
109                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
110            }
111    
112            public static String getString(Map<String, ?> map, String key) {
113                    return getString(map, key, GetterUtil.DEFAULT_STRING);
114            }
115    
116            public static String getString(
117                    Map<String, ?> map, String key, String defaultValue) {
118    
119                    Object value = map.get(key);
120    
121                    if (value != null) {
122                            if (value instanceof String[]) {
123                                    String[] array = (String[])value;
124    
125                                    if (array.length > 0) {
126                                            return GetterUtil.getString(array[0], defaultValue);
127                                    }
128                            }
129                            else if (value instanceof String) {
130                                    return GetterUtil.getString((String)value, defaultValue);
131                            }
132                            else {
133                                    return GetterUtil.getString(
134                                            String.valueOf(value), defaultValue);
135                            }
136                    }
137    
138                    return defaultValue;
139            }
140    
141            public static <K, V> void merge(
142                    Map<K, V> master, Map<? super K, ? super V> copy) {
143    
144                    copy.putAll(master);
145            }
146    
147            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
148                    String[] params) {
149    
150                    return toLinkedHashMap(params, StringPool.COLON);
151            }
152    
153            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
154                    String[] params, String delimiter) {
155    
156                    LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
157    
158                    for (int i = 0; i < params.length; i++) {
159                            String[] kvp = StringUtil.split(params[i], delimiter);
160    
161                            if (kvp.length == 2) {
162                                    map.put(kvp[0], kvp[1]);
163                            }
164                            else if (kvp.length == 3) {
165                                    String type = kvp[2];
166    
167                                    if (type.equalsIgnoreCase("boolean") ||
168                                            type.equals(Boolean.class.getName())) {
169    
170                                            map.put(kvp[0], new Boolean(kvp[1]));
171                                    }
172                                    else if (type.equalsIgnoreCase("double") ||
173                                                     type.equals(Double.class.getName())) {
174    
175                                            map.put(kvp[0], new Double(kvp[1]));
176                                    }
177                                    else if (type.equalsIgnoreCase("int") ||
178                                                     type.equals(Integer.class.getName())) {
179    
180                                            map.put(kvp[0], new Integer(kvp[1]));
181                                    }
182                                    else if (type.equalsIgnoreCase("long") ||
183                                                     type.equals(Long.class.getName())) {
184    
185                                            map.put(kvp[0], new Long(kvp[1]));
186                                    }
187                                    else if (type.equalsIgnoreCase("short") ||
188                                                     type.equals(Short.class.getName())) {
189    
190                                            map.put(kvp[0], new Short(kvp[1]));
191                                    }
192                                    else if (type.equals(String.class.getName())) {
193                                            map.put(kvp[0], kvp[1]);
194                                    }
195                                    else {
196                                            try {
197                                                    Class<?> clazz = Class.forName(type);
198    
199                                                    Constructor<?> constructor = clazz.getConstructor(
200                                                            new Class<?>[] {String.class});
201    
202                                                    map.put(kvp[0], constructor.newInstance(kvp[1]));
203                                            }
204                                            catch (Exception e) {
205                                                    _log.error(e.getMessage(), e);
206                                            }
207                                    }
208                            }
209                    }
210    
211                    return (LinkedHashMap<String, T>) map;
212            }
213    
214            public static String toString(Map<?, ?> map) {
215                    StringBundler sb = new StringBundler(map.size() * 4 + 1);
216    
217                    sb.append(StringPool.OPEN_CURLY_BRACE);
218    
219                    Iterator<?> itr = map.entrySet().iterator();
220    
221                    while (itr.hasNext()) {
222                            Map.Entry<Object, Object> entry =
223                                    (Map.Entry<Object, Object>)itr.next();
224    
225                            Object key = entry.getKey();
226                            Object value = entry.getValue();
227    
228                            sb.append(key);
229                            sb.append(StringPool.EQUAL);
230    
231                            if (value instanceof Map<?, ?>) {
232                                    sb.append(MapUtil.toString((Map<?, ?>)value));
233                            }
234                            else if (value instanceof String[]) {
235                                    String valueString = StringUtil.merge(
236                                            (String[])value, StringPool.COMMA_AND_SPACE);
237    
238                                    sb.append(
239                                            StringPool.OPEN_BRACKET.concat(valueString).concat(
240                                                    StringPool.CLOSE_BRACKET));
241                            }
242                            else {
243                                    sb.append(value);
244                            }
245    
246                            if (itr.hasNext()) {
247                                    sb.append(StringPool.COMMA_AND_SPACE);
248                            }
249                    }
250    
251                    sb.append(StringPool.CLOSE_CURLY_BRACE);
252    
253                    return sb.toString();
254            }
255    
256            private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
257    
258    }