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.LinkedHashMap;
23  import java.util.Map;
24  
25  /**
26   * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   * @author Raymond Augé
30   */
31  public class MapUtil {
32  
33      public static<K, V> void copy(
34          Map<K, V> master, Map<? super K, ? super V> copy) {
35  
36          copy.clear();
37  
38          merge(master, copy);
39      }
40  
41      public static boolean getBoolean(Map<String, ?> map, String key) {
42          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
43      }
44  
45      public static boolean getBoolean(
46          Map<String, ?> map, String key, boolean defaultValue) {
47  
48          return GetterUtil.getBoolean(
49              getString(map, key, String.valueOf(defaultValue)), defaultValue);
50      }
51  
52      public static int getInteger(Map<String, ?> map, String key) {
53          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
54      }
55  
56      public static int getInteger(
57          Map<String, ?> map, String key, int defaultValue) {
58  
59          return GetterUtil.getInteger(
60              getString(map, key, String.valueOf(defaultValue)), defaultValue);
61      }
62  
63      public static long getLong(Map<Long, Long> map, long key) {
64          return getLong(map, key, GetterUtil.DEFAULT_LONG);
65      }
66  
67      public static long getLong(
68          Map<Long, Long> map, long key, long defaultValue) {
69  
70          Long keyObj = new Long(key);
71  
72          if (map.containsKey(keyObj)) {
73              return map.get(keyObj);
74          }
75  
76          return defaultValue;
77      }
78  
79      public static long getLong(Map<String, ?> map, String key) {
80          return getLong(map, key, GetterUtil.DEFAULT_LONG);
81      }
82  
83      public static long getLong(
84          Map<String, ?> map, String key, long defaultValue) {
85  
86          return GetterUtil.getLong(
87              getString(map, key, String.valueOf(defaultValue)), defaultValue);
88      }
89  
90      public static short getShort(Map<String, ?> map, String key) {
91          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
92      }
93  
94      public static short getShort(
95          Map<String, ?> map, String key, short defaultValue) {
96  
97          return GetterUtil.getShort(
98              getString(map, key, String.valueOf(defaultValue)), defaultValue);
99      }
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         if (map.containsKey(key)) {
109             Object value = map.get(key);
110 
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         }
122 
123         return defaultValue;
124     }
125 
126     public static<K, V> void merge(
127         Map<K, V> master, Map<? super K, ? super V> copy) {
128 
129         copy.putAll(master);
130     }
131 
132     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
133         String[] params) {
134 
135         return toLinkedHashMap(params, StringPool.COLON);
136     }
137 
138     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
139         String[] params, String delimiter) {
140 
141         LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
142 
143         for (int i = 0; i < params.length; i++) {
144             String[] kvp = StringUtil.split(params[i], delimiter);
145 
146             if (kvp.length == 2) {
147                 map.put(kvp[0], kvp[1]);
148             }
149             else if (kvp.length == 3) {
150                 String type = kvp[2];
151 
152                 if (type.equalsIgnoreCase("boolean") ||
153                     type.equals(Boolean.class.getName())) {
154 
155                     map.put(kvp[0], new Boolean(kvp[1]));
156                 }
157                 else if (type.equalsIgnoreCase("double") ||
158                     type.equals(Double.class.getName())) {
159 
160                     map.put(kvp[0], new Double(kvp[1]));
161                 }
162                 else if (type.equalsIgnoreCase("int") ||
163                     type.equals(Integer.class.getName())) {
164 
165                     map.put(kvp[0], new Integer(kvp[1]));
166                 }
167                 else if (type.equalsIgnoreCase("long") ||
168                     type.equals(Long.class.getName())) {
169 
170                     map.put(kvp[0], new Long(kvp[1]));
171                 }
172                 else if (type.equalsIgnoreCase("short") ||
173                     type.equals(Short.class.getName())) {
174 
175                     map.put(kvp[0], new Short(kvp[1]));
176                 }
177                 else if (type.equals(String.class.getName())) {
178                     map.put(kvp[0], kvp[1]);
179                 }
180                 else {
181                     try {
182                         Class<?> classObj = Class.forName(type);
183 
184                         Constructor<?> constructor = classObj.getConstructor(
185                             new Class<?>[] {String.class});
186 
187                         map.put(kvp[0], constructor.newInstance(kvp[1]));
188                     }
189                     catch (Exception e) {
190                         _log.error(e, e);
191                     }
192                 }
193             }
194         }
195 
196         return (LinkedHashMap<String, T>) map;
197     }
198 
199     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
200 
201 }