1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  
25  import java.lang.reflect.Constructor;
26  
27  import java.util.Iterator;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  
31  /**
32   * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Raymond Augé
36   *
37   */
38  public class MapUtil {
39  
40      public static void copy(Map master, Map copy) {
41          copy.clear();
42  
43          merge(master, copy);
44      }
45  
46      public static boolean getBoolean(Map map, String key) {
47          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
48      }
49  
50      public static boolean getBoolean(
51          Map map, String key, boolean defaultValue) {
52  
53          return GetterUtil.getBoolean(
54              getString(map, key, String.valueOf(defaultValue)), defaultValue);
55      }
56  
57      public static int getInteger(Map map, String key) {
58          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
59      }
60  
61      public static int getInteger(Map map, String key, int defaultValue) {
62          return GetterUtil.getInteger(
63              getString(map, key, String.valueOf(defaultValue)), defaultValue);
64      }
65  
66      public static long getLong(Map map, long key) {
67          return getLong(map, key, GetterUtil.DEFAULT_LONG);
68      }
69  
70      public static long getLong(Map map, long key, long defaultValue) {
71          Long keyObj = new Long(key);
72  
73          if (map.containsKey(keyObj)) {
74              Object value = map.get(keyObj);
75  
76              if (value instanceof Long) {
77                  return ((Long)value).longValue();
78              }
79          }
80  
81          return defaultValue;
82      }
83  
84      public static short getShort(Map map, String key) {
85          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
86      }
87  
88      public static short getShort(Map map, String key, short defaultValue) {
89          return GetterUtil.getShort(
90              getString(map, key, String.valueOf(defaultValue)), defaultValue);
91      }
92  
93      public static String getString(Map map, String key) {
94          return getString(map, key, GetterUtil.DEFAULT_STRING);
95      }
96  
97      public static String getString(Map map, String key, String defaultValue) {
98          if (map.containsKey(key)) {
99              Object value = map.get(key);
100 
101             if (value instanceof String[]) {
102                 String[] array = (String[])value;
103 
104                 if (array.length > 0) {
105                     return GetterUtil.getString(array[0], defaultValue);
106                 }
107             }
108             else if (value instanceof String) {
109                 return GetterUtil.getString((String)value, defaultValue);
110             }
111             else {
112                 return defaultValue;
113             }
114         }
115 
116         return defaultValue;
117     }
118 
119     public static void merge(Map master, Map copy) {
120         Iterator itr = master.entrySet().iterator();
121 
122         while (itr.hasNext()) {
123             Map.Entry entry = (Map.Entry)itr.next();
124 
125             Object key = entry.getKey();
126             Object value = entry.getValue();
127 
128             copy.put(key, value);
129         }
130     }
131 
132     public static LinkedHashMap toLinkedHashMap(String[] params) {
133         return toLinkedHashMap(params, StringPool.COLON);
134     }
135 
136     public static LinkedHashMap toLinkedHashMap(
137         String[] params, String delimiter) {
138 
139         LinkedHashMap map = new LinkedHashMap();
140 
141         for (int i = 0; i < params.length; i++) {
142             String[] kvp = StringUtil.split(params[i], delimiter);
143 
144             if (kvp.length == 2) {
145                 map.put(kvp[0], kvp[1]);
146             }
147             else if (kvp.length == 3) {
148                 String type = kvp[2];
149 
150                 if (type.equalsIgnoreCase("boolean") ||
151                     type.equals(Boolean.class.getName())) {
152 
153                     map.put(kvp[0], new Boolean(kvp[1]));
154                 }
155                 else if (type.equalsIgnoreCase("double") ||
156                     type.equals(Double.class.getName())) {
157 
158                     map.put(kvp[0], new Double(kvp[1]));
159                 }
160                 else if (type.equalsIgnoreCase("int") ||
161                     type.equals(Integer.class.getName())) {
162 
163                     map.put(kvp[0], new Integer(kvp[1]));
164                 }
165                 else if (type.equalsIgnoreCase("long") ||
166                     type.equals(Long.class.getName())) {
167 
168                     map.put(kvp[0], new Long(kvp[1]));
169                 }
170                 else if (type.equalsIgnoreCase("short") ||
171                     type.equals(Short.class.getName())) {
172 
173                     map.put(kvp[0], new Short(kvp[1]));
174                 }
175                 else if (type.equals(String.class.getName())) {
176                     map.put(kvp[0], kvp[1]);
177                 }
178                 else {
179                     try {
180                         Class classObj = Class.forName(type);
181 
182                         Constructor constructor = classObj.getConstructor(
183                             new Class[] {String.class});
184 
185                         map.put(kvp[0], constructor.newInstance(kvp[1]));
186                     }
187                     catch (Exception e) {
188                         _log.error(e, e);
189                     }
190                 }
191             }
192         }
193 
194         return map;
195     }
196 
197     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
198 
199 }