1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.lang.reflect.Constructor;
21  
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  /**
27   * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   * @author Raymond Augé
31   */
32  public class MapUtil {
33  
34      public static<K, V> void copy(
35          Map<K, V> master, Map<? super K, ? super V> copy) {
36  
37          copy.clear();
38  
39          merge(master, copy);
40      }
41  
42      public static boolean getBoolean(Map<String, ?> map, String key) {
43          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
44      }
45  
46      public static boolean getBoolean(
47          Map<String, ?> map, String key, boolean defaultValue) {
48  
49          return GetterUtil.getBoolean(
50              getString(map, key, String.valueOf(defaultValue)), defaultValue);
51      }
52  
53      public static int getInteger(Map<String, ?> map, String key) {
54          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
55      }
56  
57      public static int getInteger(
58          Map<String, ?> map, String key, int defaultValue) {
59  
60          return GetterUtil.getInteger(
61              getString(map, key, String.valueOf(defaultValue)), defaultValue);
62      }
63  
64      public static long getLong(Map<Long, Long> map, long key) {
65          return getLong(map, key, GetterUtil.DEFAULT_LONG);
66      }
67  
68      public static long getLong(
69          Map<Long, Long> map, long key, long defaultValue) {
70  
71          Long keyObj = new Long(key);
72  
73          if (map.containsKey(keyObj)) {
74              return map.get(keyObj);
75          }
76  
77          return defaultValue;
78      }
79  
80      public static long getLong(Map<String, ?> map, String key) {
81          return getLong(map, key, GetterUtil.DEFAULT_LONG);
82      }
83  
84      public static long getLong(
85          Map<String, ?> map, String key, long defaultValue) {
86  
87          return GetterUtil.getLong(
88              getString(map, key, String.valueOf(defaultValue)), defaultValue);
89      }
90  
91      public static short getShort(Map<String, ?> map, String key) {
92          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
93      }
94  
95      public static short getShort(
96          Map<String, ?> map, String key, short defaultValue) {
97  
98          return GetterUtil.getShort(
99              getString(map, key, String.valueOf(defaultValue)), defaultValue);
100     }
101 
102     public static String getString(Map<String, ?> map, String key) {
103         return getString(map, key, GetterUtil.DEFAULT_STRING);
104     }
105 
106     public static String getString(
107         Map<String, ?> map, String key, String defaultValue) {
108 
109         if (map.containsKey(key)) {
110             Object value = map.get(key);
111 
112             if (value instanceof String[]) {
113                 String[] array = (String[])value;
114 
115                 if (array.length > 0) {
116                     return GetterUtil.getString(array[0], defaultValue);
117                 }
118             }
119             else if (value instanceof String) {
120                 return GetterUtil.getString((String)value, defaultValue);
121             }
122         }
123 
124         return defaultValue;
125     }
126 
127     public static<K, V> void merge(
128         Map<K, V> master, Map<? super K, ? super V> copy) {
129 
130         copy.putAll(master);
131     }
132 
133     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
134         String[] params) {
135 
136         return toLinkedHashMap(params, StringPool.COLON);
137     }
138 
139     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
140         String[] params, String delimiter) {
141 
142         LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
143 
144         for (int i = 0; i < params.length; i++) {
145             String[] kvp = StringUtil.split(params[i], delimiter);
146 
147             if (kvp.length == 2) {
148                 map.put(kvp[0], kvp[1]);
149             }
150             else if (kvp.length == 3) {
151                 String type = kvp[2];
152 
153                 if (type.equalsIgnoreCase("boolean") ||
154                     type.equals(Boolean.class.getName())) {
155 
156                     map.put(kvp[0], new Boolean(kvp[1]));
157                 }
158                 else if (type.equalsIgnoreCase("double") ||
159                     type.equals(Double.class.getName())) {
160 
161                     map.put(kvp[0], new Double(kvp[1]));
162                 }
163                 else if (type.equalsIgnoreCase("int") ||
164                     type.equals(Integer.class.getName())) {
165 
166                     map.put(kvp[0], new Integer(kvp[1]));
167                 }
168                 else if (type.equalsIgnoreCase("long") ||
169                     type.equals(Long.class.getName())) {
170 
171                     map.put(kvp[0], new Long(kvp[1]));
172                 }
173                 else if (type.equalsIgnoreCase("short") ||
174                     type.equals(Short.class.getName())) {
175 
176                     map.put(kvp[0], new Short(kvp[1]));
177                 }
178                 else if (type.equals(String.class.getName())) {
179                     map.put(kvp[0], kvp[1]);
180                 }
181                 else {
182                     try {
183                         Class<?> classObj = Class.forName(type);
184 
185                         Constructor<?> constructor = classObj.getConstructor(
186                             new Class<?>[] {String.class});
187 
188                         map.put(kvp[0], constructor.newInstance(kvp[1]));
189                     }
190                     catch (Exception e) {
191                         _log.error(e, e);
192                     }
193                 }
194             }
195         }
196 
197         return (LinkedHashMap<String, T>) map;
198     }
199 
200     public static String toString(Map<?, ?> map) {
201         StringBundler sb = new StringBundler(map.size() * 4 + 1);
202 
203         sb.append(StringPool.OPEN_CURLY_BRACE);
204 
205         Iterator<?> itr = map.entrySet().iterator();
206 
207         while (itr.hasNext()) {
208             Map.Entry<Object, Object> entry =
209                 (Map.Entry<Object, Object>)itr.next();
210 
211             Object key = entry.getKey();
212             Object value = entry.getValue();
213 
214             sb.append(key);
215             sb.append(StringPool.EQUAL);
216 
217             if (value instanceof Map<?, ?>) {
218                 sb.append(MapUtil.toString((Map<?, ?>)value));
219             }
220             else if (value instanceof String[]) {
221                 String valueString = StringUtil.merge(
222                     (String[])value, StringPool.COMMA_AND_SPACE);
223 
224                 sb.append(
225                     StringPool.OPEN_BRACKET.concat(valueString).concat(
226                         StringPool.CLOSE_BRACKET));
227             }
228             else {
229                 sb.append(value);
230             }
231 
232             if (itr.hasNext()) {
233                 sb.append(StringPool.COMMA_AND_SPACE);
234             }
235         }
236 
237         sb.append(StringPool.CLOSE_CURLY_BRACE);
238 
239         return sb.toString();
240     }
241 
242     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
243 
244 }