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