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 long getLong(Map map, long key) {
60          return getLong(map, key, GetterUtil.DEFAULT_LONG);
61      }
62  
63      public static long getLong(Map map, long key, long defaultValue) {
64          Long keyObj = new Long(key);
65  
66          if (map.containsKey(keyObj)) {
67              Object value = map.get(keyObj);
68  
69              if (value instanceof Long) {
70                  return ((Long)value).longValue();
71              }
72          }
73  
74          return defaultValue;
75      }
76  
77      public static String getString(Map map, String key) {
78          return getString(map, key, GetterUtil.DEFAULT_STRING);
79      }
80  
81      public static String getString(Map map, String key, String defaultValue) {
82          if (map.containsKey(key)) {
83              Object value = map.get(key);
84  
85              if (value instanceof String[]) {
86                  String[] array = (String[])value;
87  
88                  if (array.length > 0) {
89                      return GetterUtil.getString(array[0], defaultValue);
90                  }
91              }
92              else if (value instanceof String) {
93                  return GetterUtil.getString((String)value, defaultValue);
94              }
95              else {
96                  return defaultValue;
97              }
98          }
99  
100         return defaultValue;
101     }
102 
103     public static void merge(Map master, Map copy) {
104         Iterator itr = master.entrySet().iterator();
105 
106         while (itr.hasNext()) {
107             Map.Entry entry = (Map.Entry)itr.next();
108 
109             Object key = entry.getKey();
110             Object value = entry.getValue();
111 
112             copy.put(key, value);
113         }
114     }
115 
116     public static LinkedHashMap toLinkedHashMap(String[] params) {
117         LinkedHashMap map = new LinkedHashMap();
118 
119         for (int i = 0; i < params.length; i++) {
120             String[] kvp = StringUtil.split(params[i], StringPool.COLON);
121 
122             map.put(kvp[0], kvp[1]);
123         }
124 
125         return map;
126     }
127 
128 }