001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026:
027: import java.util.Iterator;
028: import java.util.LinkedHashMap;
029: import java.util.Map;
030:
031: /**
032: * <a href="MapUtil.java.html"><b><i>View Source</i></b></a>
033: *
034: * @author Brian Wing Shun Chan
035: * @author Raymond Augé
036: *
037: */
038: public class MapUtil {
039:
040: public static void copy(Map master, Map copy) {
041: copy.clear();
042:
043: merge(master, copy);
044: }
045:
046: public static boolean getBoolean(Map map, String key) {
047: return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
048: }
049:
050: public static boolean getBoolean(Map map, String key,
051: boolean defaultValue) {
052:
053: return GetterUtil.getBoolean(getString(map, key, String
054: .valueOf(defaultValue)), defaultValue);
055: }
056:
057: public static long getLong(Map map, long key) {
058: return getLong(map, key, GetterUtil.DEFAULT_LONG);
059: }
060:
061: public static long getLong(Map map, long key, long defaultValue) {
062: Long keyObj = new Long(key);
063:
064: if (map.containsKey(keyObj)) {
065: Object value = map.get(keyObj);
066:
067: if (value instanceof Long) {
068: return ((Long) value).longValue();
069: }
070: }
071:
072: return defaultValue;
073: }
074:
075: public static String getString(Map map, String key) {
076: return getString(map, key, GetterUtil.DEFAULT_STRING);
077: }
078:
079: public static String getString(Map map, String key,
080: String defaultValue) {
081: if (map.containsKey(key)) {
082: Object value = map.get(key);
083:
084: if (value instanceof String[]) {
085: String[] array = (String[]) value;
086:
087: if (array.length > 0) {
088: return GetterUtil.getString(array[0], defaultValue);
089: }
090: } else if (value instanceof String) {
091: return GetterUtil.getString((String) value,
092: defaultValue);
093: } else {
094: return defaultValue;
095: }
096: }
097:
098: return defaultValue;
099: }
100:
101: public static void merge(Map master, Map copy) {
102: Iterator itr = master.entrySet().iterator();
103:
104: while (itr.hasNext()) {
105: Map.Entry entry = (Map.Entry) itr.next();
106:
107: Object key = entry.getKey();
108: Object value = entry.getValue();
109:
110: copy.put(key, value);
111: }
112: }
113:
114: public static LinkedHashMap toLinkedHashMap(String[] params) {
115: LinkedHashMap map = new LinkedHashMap();
116:
117: for (int i = 0; i < params.length; i++) {
118: String[] kvp = StringUtil
119: .split(params[i], StringPool.COLON);
120:
121: map.put(kvp[0], kvp[1]);
122: }
123:
124: return map;
125: }
126:
127: }
|