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 short getShort(Map<String, ?> map, String key) {
80          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
81      }
82  
83      public static short getShort(
84          Map<String, ?> map, String key, short defaultValue) {
85  
86          return GetterUtil.getShort(
87              getString(map, key, String.valueOf(defaultValue)), defaultValue);
88      }
89  
90      public static String getString(Map<String, ?> map, String key) {
91          return getString(map, key, GetterUtil.DEFAULT_STRING);
92      }
93  
94      public static String getString(
95          Map<String, ?> map, String key, String defaultValue) {
96  
97          if (map.containsKey(key)) {
98              Object value = map.get(key);
99  
100             if (value instanceof String[]) {
101                 String[] array = (String[])value;
102 
103                 if (array.length > 0) {
104                     return GetterUtil.getString(array[0], defaultValue);
105                 }
106             }
107             else if (value instanceof String) {
108                 return GetterUtil.getString((String)value, defaultValue);
109             }
110         }
111 
112         return defaultValue;
113     }
114 
115     public static<K, V> void merge(
116         Map<K, V> master, Map<? super K, ? super V> copy) {
117 
118         copy.putAll(master);
119     }
120 
121     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
122         String[] params) {
123 
124         return toLinkedHashMap(params, StringPool.COLON);
125     }
126 
127     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
128         String[] params, String delimiter) {
129 
130         LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
131 
132         for (int i = 0; i < params.length; i++) {
133             String[] kvp = StringUtil.split(params[i], delimiter);
134 
135             if (kvp.length == 2) {
136                 map.put(kvp[0], kvp[1]);
137             }
138             else if (kvp.length == 3) {
139                 String type = kvp[2];
140 
141                 if (type.equalsIgnoreCase("boolean") ||
142                     type.equals(Boolean.class.getName())) {
143 
144                     map.put(kvp[0], new Boolean(kvp[1]));
145                 }
146                 else if (type.equalsIgnoreCase("double") ||
147                     type.equals(Double.class.getName())) {
148 
149                     map.put(kvp[0], new Double(kvp[1]));
150                 }
151                 else if (type.equalsIgnoreCase("int") ||
152                     type.equals(Integer.class.getName())) {
153 
154                     map.put(kvp[0], new Integer(kvp[1]));
155                 }
156                 else if (type.equalsIgnoreCase("long") ||
157                     type.equals(Long.class.getName())) {
158 
159                     map.put(kvp[0], new Long(kvp[1]));
160                 }
161                 else if (type.equalsIgnoreCase("short") ||
162                     type.equals(Short.class.getName())) {
163 
164                     map.put(kvp[0], new Short(kvp[1]));
165                 }
166                 else if (type.equals(String.class.getName())) {
167                     map.put(kvp[0], kvp[1]);
168                 }
169                 else {
170                     try {
171                         Class<?> classObj = Class.forName(type);
172 
173                         Constructor<?> constructor = classObj.getConstructor(
174                             new Class<?>[] {String.class});
175 
176                         map.put(kvp[0], constructor.newInstance(kvp[1]));
177                     }
178                     catch (Exception e) {
179                         _log.error(e, e);
180                     }
181                 }
182             }
183         }
184 
185         return (LinkedHashMap<String, T>) map;
186     }
187 
188     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
189 
190 }