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.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.lang.reflect.Constructor;
29  
30  import java.util.Iterator;
31  import java.util.LinkedHashMap;
32  import java.util.Map;
33  
34  /**
35   * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Raymond Augé
39   *
40   */
41  public class MapUtil {
42  
43      public static void copy(Map master, Map copy) {
44          copy.clear();
45  
46          merge(master, copy);
47      }
48  
49      public static boolean getBoolean(Map map, String key) {
50          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
51      }
52  
53      public static boolean getBoolean(
54          Map map, String key, boolean defaultValue) {
55  
56          return GetterUtil.getBoolean(
57              getString(map, key, String.valueOf(defaultValue)), defaultValue);
58      }
59  
60      public static int getInteger(Map map, String key) {
61          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
62      }
63  
64      public static int getInteger(Map map, String key, int defaultValue) {
65          return GetterUtil.getInteger(
66              getString(map, key, String.valueOf(defaultValue)), defaultValue);
67      }
68  
69      public static long getLong(Map map, long key) {
70          return getLong(map, key, GetterUtil.DEFAULT_LONG);
71      }
72  
73      public static long getLong(Map map, long key, long defaultValue) {
74          Long keyObj = new Long(key);
75  
76          if (map.containsKey(keyObj)) {
77              Object value = map.get(keyObj);
78  
79              if (value instanceof Long) {
80                  return ((Long)value).longValue();
81              }
82          }
83  
84          return defaultValue;
85      }
86  
87      public static short getShort(Map map, String key) {
88          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
89      }
90  
91      public static short getShort(Map map, String key, short defaultValue) {
92          return GetterUtil.getShort(
93              getString(map, key, String.valueOf(defaultValue)), defaultValue);
94      }
95  
96      public static String getString(Map map, String key) {
97          return getString(map, key, GetterUtil.DEFAULT_STRING);
98      }
99  
100     public static String getString(Map map, String key, String defaultValue) {
101         if (map.containsKey(key)) {
102             Object value = map.get(key);
103 
104             if (value instanceof String[]) {
105                 String[] array = (String[])value;
106 
107                 if (array.length > 0) {
108                     return GetterUtil.getString(array[0], defaultValue);
109                 }
110             }
111             else if (value instanceof String) {
112                 return GetterUtil.getString((String)value, defaultValue);
113             }
114             else {
115                 return defaultValue;
116             }
117         }
118 
119         return defaultValue;
120     }
121 
122     public static void merge(Map master, Map copy) {
123         Iterator itr = master.entrySet().iterator();
124 
125         while (itr.hasNext()) {
126             Map.Entry entry = (Map.Entry)itr.next();
127 
128             Object key = entry.getKey();
129             Object value = entry.getValue();
130 
131             copy.put(key, value);
132         }
133     }
134 
135     public static LinkedHashMap toLinkedHashMap(String[] params) {
136         return toLinkedHashMap(params, StringPool.COLON);
137     }
138 
139     public static LinkedHashMap toLinkedHashMap(
140         String[] params, String delimiter) {
141 
142         LinkedHashMap map = new LinkedHashMap();
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 map;
198     }
199 
200     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
201 
202 }