1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.LinkedHashMap;
31  import java.util.Map;
32  
33  /**
34   * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Raymond Augé
38   */
39  public class MapUtil {
40  
41      public static<K, V> void copy(
42          Map<K, V> master, Map<? super K, ? super V> copy) {
43  
44          copy.clear();
45  
46          merge(master, copy);
47      }
48  
49      public static boolean getBoolean(Map<String, ?> map, String key) {
50          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
51      }
52  
53      public static boolean getBoolean(
54          Map<String, ?> 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<String, ?> map, String key) {
61          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
62      }
63  
64      public static int getInteger(
65          Map<String, ?> map, String key, int defaultValue) {
66  
67          return GetterUtil.getInteger(
68              getString(map, key, String.valueOf(defaultValue)), defaultValue);
69      }
70  
71      public static long getLong(Map<Long, Long> map, long key) {
72          return getLong(map, key, GetterUtil.DEFAULT_LONG);
73      }
74  
75      public static long getLong(
76          Map<Long, Long> map, long key, long defaultValue) {
77  
78          Long keyObj = new Long(key);
79  
80          if (map.containsKey(keyObj)) {
81              return map.get(keyObj);
82          }
83  
84          return defaultValue;
85      }
86  
87      public static short getShort(Map<String, ?> map, String key) {
88          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
89      }
90  
91      public static short getShort(
92          Map<String, ?> map, String key, short defaultValue) {
93  
94          return GetterUtil.getShort(
95              getString(map, key, String.valueOf(defaultValue)), defaultValue);
96      }
97  
98      public static String getString(Map<String, ?> map, String key) {
99          return getString(map, key, GetterUtil.DEFAULT_STRING);
100     }
101 
102     public static String getString(
103         Map<String, ?> map, String key, String defaultValue) {
104 
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         }
119 
120         return defaultValue;
121     }
122 
123     public static<K, V> void merge(
124         Map<K, V> master, Map<? super K, ? super V> copy) {
125 
126         copy.putAll(master);
127     }
128 
129     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
130         String[] params) {
131 
132         return toLinkedHashMap(params, StringPool.COLON);
133     }
134 
135     public static<T> LinkedHashMap<String, T> toLinkedHashMap(
136         String[] params, String delimiter) {
137 
138         LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
139 
140         for (int i = 0; i < params.length; i++) {
141             String[] kvp = StringUtil.split(params[i], delimiter);
142 
143             if (kvp.length == 2) {
144                 map.put(kvp[0], kvp[1]);
145             }
146             else if (kvp.length == 3) {
147                 String type = kvp[2];
148 
149                 if (type.equalsIgnoreCase("boolean") ||
150                     type.equals(Boolean.class.getName())) {
151 
152                     map.put(kvp[0], new Boolean(kvp[1]));
153                 }
154                 else if (type.equalsIgnoreCase("double") ||
155                     type.equals(Double.class.getName())) {
156 
157                     map.put(kvp[0], new Double(kvp[1]));
158                 }
159                 else if (type.equalsIgnoreCase("int") ||
160                     type.equals(Integer.class.getName())) {
161 
162                     map.put(kvp[0], new Integer(kvp[1]));
163                 }
164                 else if (type.equalsIgnoreCase("long") ||
165                     type.equals(Long.class.getName())) {
166 
167                     map.put(kvp[0], new Long(kvp[1]));
168                 }
169                 else if (type.equalsIgnoreCase("short") ||
170                     type.equals(Short.class.getName())) {
171 
172                     map.put(kvp[0], new Short(kvp[1]));
173                 }
174                 else if (type.equals(String.class.getName())) {
175                     map.put(kvp[0], kvp[1]);
176                 }
177                 else {
178                     try {
179                         Class<?> classObj = Class.forName(type);
180 
181                         Constructor<?> constructor = classObj.getConstructor(
182                             new Class<?>[] {String.class});
183 
184                         map.put(kvp[0], constructor.newInstance(kvp[1]));
185                     }
186                     catch (Exception e) {
187                         _log.error(e, e);
188                     }
189                 }
190             }
191         }
192 
193         return (LinkedHashMap<String, T>) map;
194     }
195 
196     private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
197 
198 }