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.util.Iterator;
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   */
40  public class MapUtil {
41  
42      public static void copy(Map master, Map copy) {
43          copy.clear();
44  
45          merge(master, copy);
46      }
47  
48      public static boolean getBoolean(Map map, String key) {
49          return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
50      }
51  
52      public static boolean getBoolean(
53          Map map, String key, boolean defaultValue) {
54  
55          return GetterUtil.getBoolean(
56              getString(map, key, String.valueOf(defaultValue)), defaultValue);
57      }
58  
59      public static int getInteger(Map map, String key) {
60          return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
61      }
62  
63      public static int getInteger(Map map, String key, int defaultValue) {
64          return GetterUtil.getInteger(
65              getString(map, key, String.valueOf(defaultValue)), defaultValue);
66      }
67  
68      public static long getLong(Map map, long key) {
69          return getLong(map, key, GetterUtil.DEFAULT_LONG);
70      }
71  
72      public static long getLong(Map map, long key, long defaultValue) {
73          Long keyObj = new Long(key);
74  
75          if (map.containsKey(keyObj)) {
76              Object value = map.get(keyObj);
77  
78              if (value instanceof Long) {
79                  return ((Long)value).longValue();
80              }
81          }
82  
83          return defaultValue;
84      }
85  
86      public static short getShort(Map map, String key) {
87          return getShort(map, key, GetterUtil.DEFAULT_SHORT);
88      }
89  
90      public static short getShort(Map map, String key, short defaultValue) {
91          return GetterUtil.getShort(
92              getString(map, key, String.valueOf(defaultValue)), defaultValue);
93      }
94  
95      public static String getString(Map map, String key) {
96          return getString(map, key, GetterUtil.DEFAULT_STRING);
97      }
98  
99      public static String getString(Map map, String key, String defaultValue) {
100         if (map.containsKey(key)) {
101             Object value = map.get(key);
102 
103             if (value instanceof String[]) {
104                 String[] array = (String[])value;
105 
106                 if (array.length > 0) {
107                     return GetterUtil.getString(array[0], defaultValue);
108                 }
109             }
110             else if (value instanceof String) {
111                 return GetterUtil.getString((String)value, defaultValue);
112             }
113             else {
114                 return defaultValue;
115             }
116         }
117 
118         return defaultValue;
119     }
120 
121     public static void merge(Map master, Map copy) {
122         Iterator itr = master.entrySet().iterator();
123 
124         while (itr.hasNext()) {
125             Map.Entry entry = (Map.Entry)itr.next();
126 
127             Object key = entry.getKey();
128             Object value = entry.getValue();
129 
130             copy.put(key, value);
131         }
132     }
133 
134     public static LinkedHashMap toLinkedHashMap(String[] params) {
135         return toLinkedHashMap(params, StringPool.COLON);
136     }
137 
138     public static LinkedHashMap toLinkedHashMap(
139         String[] params, String delimiter) {
140 
141         LinkedHashMap map = new LinkedHashMap();
142 
143         for (int i = 0; i < params.length; i++) {
144             String[] kvp = StringUtil.split(params[i], delimiter);
145 
146             map.put(kvp[0], kvp[1]);
147         }
148 
149         return map;
150     }
151 
152 }