1   /**
2    * Copyright (c) 2000-2007 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 String getString(Map map, String key) {
60          return getString(map, key, GetterUtil.DEFAULT_STRING);
61      }
62  
63      public static String getString(Map map, String key, String defaultValue) {
64          if (map.containsKey(key)) {
65              Object value = map.get(key);
66  
67              if (value instanceof String[]) {
68                  String[] array = (String[])value;
69  
70                  if (array.length > 0) {
71                      return GetterUtil.getString(array[0], defaultValue);
72                  }
73              }
74              else if (value instanceof String) {
75                  return GetterUtil.getString((String)value, defaultValue);
76              }
77              else {
78                  return defaultValue;
79              }
80          }
81  
82          return defaultValue;
83      }
84  
85      public static void merge(Map master, Map copy) {
86          Iterator itr = master.entrySet().iterator();
87  
88          while (itr.hasNext()) {
89              Map.Entry entry = (Map.Entry)itr.next();
90  
91              Object key = entry.getKey();
92              Object value = entry.getValue();
93  
94              copy.put(key, value);
95          }
96      }
97  
98      public static LinkedHashMap toLinkedHashMap(String[] params) {
99          LinkedHashMap map = new LinkedHashMap();
100 
101         for (int i = 0; i < params.length; i++) {
102             String[] kvp = StringUtil.split(params[i], StringPool.COLON);
103 
104             map.put(kvp[0], kvp[1]);
105         }
106 
107         return map;
108     }
109 
110 }