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