1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  
31  import java.lang.reflect.Constructor;
32  
33  import java.util.Iterator;
34  import java.util.LinkedHashMap;
35  import java.util.Map;
36  
37  /**
38   * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Raymond Augé
42   *
43   */
44  public class MapUtil {
45  
46      public static void copy(Map master, Map copy) {
47          copy.clear();
48  
49          merge(master, copy);
50      }
51  
52      public static boolean getBoolean(Map map, String key) {
53          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
54      }
55  
56      public static boolean getBoolean(
57          Map map, String key, boolean defaultValue) {
58  
59          return GetterUtil.getBoolean(
60              getString(map, key, String.valueOf(defaultValue)), defaultValue);
61      }
62  
63      public static int getInteger(Map map, String key) {
64          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
65      }
66  
67      public static int getInteger(Map map, String key, int defaultValue) {
68          return GetterUtil.getInteger(
69              getString(map, key, String.valueOf(defaultValue)), defaultValue);
70      }
71  
72      public static long getLong(Map map, long key) {
73          return getLong(map, key, GetterUtil.DEFAULT_LONG);
74      }
75  
76      public static long getLong(Map map, long key, long defaultValue) {
77          Long keyObj = new Long(key);
78  
79          if (map.containsKey(keyObj)) {
80              Object value = map.get(keyObj);
81  
82              if (value instanceof Long) {
83                  return ((Long)value).longValue();
84              }
85          }
86  
87          return defaultValue;
88      }
89  
90      public static short getShort(Map map, String key) {
91          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
92      }
93  
94      public static short getShort(Map map, String key, short defaultValue) {
95          return GetterUtil.getShort(
96              getString(map, key, String.valueOf(defaultValue)), defaultValue);
97      }
98  
99      public static String getString(Map map, String key) {
100         return getString(map, key, GetterUtil.DEFAULT_STRING);
101     }
102 
103     public static String getString(Map map, String key, String defaultValue) {
104         if (map.containsKey(key)) {
105             Object value = map.get(key);
106 
107             if (value instanceof String[]) {
108                 String[] array = (String[])value;
109 
110                 if (array.length > 0) {
111                     return GetterUtil.getString(array[0], defaultValue);
112                 }
113             }
114             else if (value instanceof String) {
115                 return GetterUtil.getString((String)value, defaultValue);
116             }
117             else {
118                 return defaultValue;
119             }
120         }
121 
122         return defaultValue;
123     }
124 
125     public static void merge(Map master, Map copy) {
126         Iterator itr = master.entrySet().iterator();
127 
128         while (itr.hasNext()) {
129             Map.Entry entry = (Map.Entry)itr.next();
130 
131             Object key = entry.getKey();
132             Object value = entry.getValue();
133 
134             copy.put(key, value);
135         }
136     }
137 
138     public static LinkedHashMap toLinkedHashMap(String[] params) {
139         return toLinkedHashMap(params, StringPool.COLON);
140     }
141 
142     public static LinkedHashMap toLinkedHashMap(
143         String[] params, String delimiter) {
144 
145         LinkedHashMap map = new LinkedHashMap();
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 map;
201     }
202 
203     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
204 
205 }