1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 value = map.get(new Long(key));
72  
73          if (value == null) {
74              return defaultValue;
75          }
76          else {
77              return value;
78          }
79      }
80  
81      public static long getLong(Map<String, ?> map, String key) {
82          return getLong(map, key, GetterUtil.DEFAULT_LONG);
83      }
84  
85      public static long getLong(
86          Map<String, ?> map, String key, long defaultValue) {
87  
88          return GetterUtil.getLong(
89              getString(map, key, String.valueOf(defaultValue)), defaultValue);
90      }
91  
92      public static short getShort(Map<String, ?> map, String key) {
93          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
94      }
95  
96      public static short getShort(
97          Map<String, ?> map, String key, short defaultValue) {
98  
99          return GetterUtil.getShort(
100             getString(map, key, String.valueOf(defaultValue)), defaultValue);
101     }
102 
103     public static String getString(Map<String, ?> map, String key) {
104         return getString(map, key, GetterUtil.DEFAULT_STRING);
105     }
106 
107     public static String getString(
108         Map<String, ?> map, String key, String defaultValue) {
109 
110         Object value = map.get(key);
111 
112         if (value != null) {
113             if (value instanceof String[]) {
114                 String[] array = (String[])value;
115 
116                 if (array.length > 0) {
117                     return GetterUtil.getString(array[0], defaultValue);
118                 }
119             }
120             else if (value instanceof String) {
121                 return GetterUtil.getString((String)value, defaultValue);
122             }
123             else {
124                 return GetterUtil.getString(
125                     String.valueOf(value), defaultValue);
126             }
127         }
128 
129         return defaultValue;
130     }
131 
132     public static <K, V> void merge(
133         Map<K, V> master, Map<? super K, ? super V> copy) {
134 
135         copy.putAll(master);
136     }
137 
138     public static <T> LinkedHashMap<String, T> toLinkedHashMap(
139         String[] params) {
140 
141         return toLinkedHashMap(params, StringPool.COLON);
142     }
143 
144     public static <T> LinkedHashMap<String, T> toLinkedHashMap(
145         String[] params, String delimiter) {
146 
147         LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
148 
149         for (int i = 0; i < params.length; i++) {
150             String[] kvp = StringUtil.split(params[i], delimiter);
151 
152             if (kvp.length == 2) {
153                 map.put(kvp[0], kvp[1]);
154             }
155             else if (kvp.length == 3) {
156                 String type = kvp[2];
157 
158                 if (type.equalsIgnoreCase("boolean") ||
159                     type.equals(Boolean.class.getName())) {
160 
161                     map.put(kvp[0], new Boolean(kvp[1]));
162                 }
163                 else if (type.equalsIgnoreCase("double") ||
164                     type.equals(Double.class.getName())) {
165 
166                     map.put(kvp[0], new Double(kvp[1]));
167                 }
168                 else if (type.equalsIgnoreCase("int") ||
169                     type.equals(Integer.class.getName())) {
170 
171                     map.put(kvp[0], new Integer(kvp[1]));
172                 }
173                 else if (type.equalsIgnoreCase("long") ||
174                     type.equals(Long.class.getName())) {
175 
176                     map.put(kvp[0], new Long(kvp[1]));
177                 }
178                 else if (type.equalsIgnoreCase("short") ||
179                     type.equals(Short.class.getName())) {
180 
181                     map.put(kvp[0], new Short(kvp[1]));
182                 }
183                 else if (type.equals(String.class.getName())) {
184                     map.put(kvp[0], kvp[1]);
185                 }
186                 else {
187                     try {
188                         Class<?> classObj = Class.forName(type);
189 
190                         Constructor<?> constructor = classObj.getConstructor(
191                             new Class<?>[] {String.class});
192 
193                         map.put(kvp[0], constructor.newInstance(kvp[1]));
194                     }
195                     catch (Exception e) {
196                         _log.error(e, e);
197                     }
198                 }
199             }
200         }
201 
202         return (LinkedHashMap<String, T>) map;
203     }
204 
205     public static String toString(Map<?, ?> map) {
206         StringBundler sb = new StringBundler(map.size() * 4 + 1);
207 
208         sb.append(StringPool.OPEN_CURLY_BRACE);
209 
210         Iterator<?> itr = map.entrySet().iterator();
211 
212         while (itr.hasNext()) {
213             Map.Entry<Object, Object> entry =
214                 (Map.Entry<Object, Object>)itr.next();
215 
216             Object key = entry.getKey();
217             Object value = entry.getValue();
218 
219             sb.append(key);
220             sb.append(StringPool.EQUAL);
221 
222             if (value instanceof Map<?, ?>) {
223                 sb.append(MapUtil.toString((Map<?, ?>)value));
224             }
225             else if (value instanceof String[]) {
226                 String valueString = StringUtil.merge(
227                     (String[])value, StringPool.COMMA_AND_SPACE);
228 
229                 sb.append(
230                     StringPool.OPEN_BRACKET.concat(valueString).concat(
231                         StringPool.CLOSE_BRACKET));
232             }
233             else {
234                 sb.append(value);
235             }
236 
237             if (itr.hasNext()) {
238                 sb.append(StringPool.COMMA_AND_SPACE);
239             }
240         }
241 
242         sb.append(StringPool.CLOSE_CURLY_BRACE);
243 
244         return sb.toString();
245     }
246 
247     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
248 
249 }