001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 int getInteger(Map<String, ?> map, String key) {
052                    return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
053            }
054    
055            public static int getInteger(
056                    Map<String, ?> map, String key, int defaultValue) {
057    
058                    return GetterUtil.getInteger(
059                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
060            }
061    
062            public static long getLong(Map<Long, Long> map, long key) {
063                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
064            }
065    
066            public static long getLong(
067                    Map<Long, Long> map, long key, long defaultValue) {
068    
069                    Long value = map.get(new Long(key));
070    
071                    if (value == null) {
072                            return defaultValue;
073                    }
074                    else {
075                            return value;
076                    }
077            }
078    
079            public static long getLong(Map<String, ?> map, String key) {
080                    return getLong(map, key, GetterUtil.DEFAULT_LONG);
081            }
082    
083            public static long getLong(
084                    Map<String, ?> map, String key, long defaultValue) {
085    
086                    return GetterUtil.getLong(
087                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
088            }
089    
090            public static short getShort(Map<String, ?> map, String key) {
091                    return getShort(map, key, GetterUtil.DEFAULT_SHORT);
092            }
093    
094            public static short getShort(
095                    Map<String, ?> map, String key, short defaultValue) {
096    
097                    return GetterUtil.getShort(
098                            getString(map, key, String.valueOf(defaultValue)), defaultValue);
099            }
100    
101            public static String getString(Map<String, ?> map, String key) {
102                    return getString(map, key, GetterUtil.DEFAULT_STRING);
103            }
104    
105            public static String getString(
106                    Map<String, ?> map, String key, String defaultValue) {
107    
108                    Object value = map.get(key);
109    
110                    if (value != null) {
111                            if (value instanceof String[]) {
112                                    String[] array = (String[])value;
113    
114                                    if (array.length > 0) {
115                                            return GetterUtil.getString(array[0], defaultValue);
116                                    }
117                            }
118                            else if (value instanceof String) {
119                                    return GetterUtil.getString((String)value, defaultValue);
120                            }
121                            else {
122                                    return GetterUtil.getString(
123                                            String.valueOf(value), defaultValue);
124                            }
125                    }
126    
127                    return defaultValue;
128            }
129    
130            public static <K, V> void merge(
131                    Map<K, V> master, Map<? super K, ? super V> copy) {
132    
133                    copy.putAll(master);
134            }
135    
136            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
137                    String[] params) {
138    
139                    return toLinkedHashMap(params, StringPool.COLON);
140            }
141    
142            public static <T> LinkedHashMap<String, T> toLinkedHashMap(
143                    String[] params, String delimiter) {
144    
145                    LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
146    
147                    for (int i = 0; i < params.length; i++) {
148                            String[] kvp = StringUtil.split(params[i], delimiter);
149    
150                            if (kvp.length == 2) {
151                                    map.put(kvp[0], kvp[1]);
152                            }
153                            else if (kvp.length == 3) {
154                                    String type = kvp[2];
155    
156                                    if (type.equalsIgnoreCase("boolean") ||
157                                            type.equals(Boolean.class.getName())) {
158    
159                                            map.put(kvp[0], new Boolean(kvp[1]));
160                                    }
161                                    else if (type.equalsIgnoreCase("double") ||
162                                            type.equals(Double.class.getName())) {
163    
164                                            map.put(kvp[0], new Double(kvp[1]));
165                                    }
166                                    else if (type.equalsIgnoreCase("int") ||
167                                            type.equals(Integer.class.getName())) {
168    
169                                            map.put(kvp[0], new Integer(kvp[1]));
170                                    }
171                                    else if (type.equalsIgnoreCase("long") ||
172                                            type.equals(Long.class.getName())) {
173    
174                                            map.put(kvp[0], new Long(kvp[1]));
175                                    }
176                                    else if (type.equalsIgnoreCase("short") ||
177                                            type.equals(Short.class.getName())) {
178    
179                                            map.put(kvp[0], new Short(kvp[1]));
180                                    }
181                                    else if (type.equals(String.class.getName())) {
182                                            map.put(kvp[0], kvp[1]);
183                                    }
184                                    else {
185                                            try {
186                                                    Class<?> classObj = Class.forName(type);
187    
188                                                    Constructor<?> constructor = classObj.getConstructor(
189                                                            new Class<?>[] {String.class});
190    
191                                                    map.put(kvp[0], constructor.newInstance(kvp[1]));
192                                            }
193                                            catch (Exception e) {
194                                                    _log.error(e, e);
195                                            }
196                                    }
197                            }
198                    }
199    
200                    return (LinkedHashMap<String, T>) map;
201            }
202    
203            public static String toString(Map<?, ?> map) {
204                    StringBundler sb = new StringBundler(map.size() * 4 + 1);
205    
206                    sb.append(StringPool.OPEN_CURLY_BRACE);
207    
208                    Iterator<?> itr = map.entrySet().iterator();
209    
210                    while (itr.hasNext()) {
211                            Map.Entry<Object, Object> entry =
212                                    (Map.Entry<Object, Object>)itr.next();
213    
214                            Object key = entry.getKey();
215                            Object value = entry.getValue();
216    
217                            sb.append(key);
218                            sb.append(StringPool.EQUAL);
219    
220                            if (value instanceof Map<?, ?>) {
221                                    sb.append(MapUtil.toString((Map<?, ?>)value));
222                            }
223                            else if (value instanceof String[]) {
224                                    String valueString = StringUtil.merge(
225                                            (String[])value, StringPool.COMMA_AND_SPACE);
226    
227                                    sb.append(
228                                            StringPool.OPEN_BRACKET.concat(valueString).concat(
229                                                    StringPool.CLOSE_BRACKET));
230                            }
231                            else {
232                                    sb.append(value);
233                            }
234    
235                            if (itr.hasNext()) {
236                                    sb.append(StringPool.COMMA_AND_SPACE);
237                            }
238                    }
239    
240                    sb.append(StringPool.CLOSE_CURLY_BRACE);
241    
242                    return sb.toString();
243            }
244    
245            private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
246    
247    }