001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019
020 import java.lang.reflect.Constructor;
021
022 import java.util.LinkedHashMap;
023 import java.util.Map;
024
025
029 public class MapUtil {
030
031 public static <K, V> void copy(
032 Map<K, V> master, Map<? super K, ? super V> copy) {
033
034 copy.clear();
035
036 merge(master, copy);
037 }
038
039 public static boolean getBoolean(Map<String, ?> map, String key) {
040 return getBoolean(map, key, GetterUtil.DEFAULT_BOOLEAN);
041 }
042
043 public static boolean getBoolean(
044 Map<String, ?> map, String key, boolean defaultValue) {
045
046 return GetterUtil.getBoolean(
047 getString(map, key, String.valueOf(defaultValue)), defaultValue);
048 }
049
050 public static double getDouble(Map<String, ?> map, String key) {
051 return getDouble(map, key, GetterUtil.DEFAULT_DOUBLE);
052 }
053
054 public static double getDouble(
055 Map<String, ?> map, String key, double defaultValue) {
056
057 return GetterUtil.getDouble(
058 getString(map, key, String.valueOf(defaultValue)), defaultValue);
059 }
060
061 public static int getInteger(Map<String, ?> map, String key) {
062 return getInteger(map, key, GetterUtil.DEFAULT_INTEGER);
063 }
064
065 public static int getInteger(
066 Map<String, ?> map, String key, int defaultValue) {
067
068 return GetterUtil.getInteger(
069 getString(map, key, String.valueOf(defaultValue)), defaultValue);
070 }
071
072 public static long getLong(Map<Long, Long> map, long key) {
073 return getLong(map, key, GetterUtil.DEFAULT_LONG);
074 }
075
076 public static long getLong(
077 Map<Long, Long> map, long key, long defaultValue) {
078
079 Long value = map.get(key);
080
081 if (value == null) {
082 return defaultValue;
083 }
084 else {
085 return value;
086 }
087 }
088
089 public static long getLong(Map<String, ?> map, String key) {
090 return getLong(map, key, GetterUtil.DEFAULT_LONG);
091 }
092
093 public static long getLong(
094 Map<String, ?> map, String key, long defaultValue) {
095
096 return GetterUtil.getLong(
097 getString(map, key, String.valueOf(defaultValue)), defaultValue);
098 }
099
100 public static short getShort(Map<String, ?> map, String key) {
101 return getShort(map, key, GetterUtil.DEFAULT_SHORT);
102 }
103
104 public static short getShort(
105 Map<String, ?> map, String key, short defaultValue) {
106
107 return GetterUtil.getShort(
108 getString(map, key, String.valueOf(defaultValue)), defaultValue);
109 }
110
111 public static String getString(Map<String, ?> map, String key) {
112 return getString(map, key, GetterUtil.DEFAULT_STRING);
113 }
114
115 public static String getString(
116 Map<String, ?> map, String key, String defaultValue) {
117
118 Object value = map.get(key);
119
120 if (value != null) {
121 if (value instanceof String[]) {
122 String[] array = (String[])value;
123
124 if (array.length > 0) {
125 return GetterUtil.getString(array[0], defaultValue);
126 }
127 }
128 else if (value instanceof String) {
129 return GetterUtil.getString((String)value, defaultValue);
130 }
131 else {
132 return GetterUtil.getString(
133 String.valueOf(value), defaultValue);
134 }
135 }
136
137 return defaultValue;
138 }
139
140 public static <K, V> void merge(
141 Map<K, V> master, Map<? super K, ? super V> copy) {
142
143 copy.putAll(master);
144 }
145
146 public static <T> LinkedHashMap<String, T> toLinkedHashMap(
147 String[] params) {
148
149 return toLinkedHashMap(params, StringPool.COLON);
150 }
151
152 public static <T> LinkedHashMap<String, T> toLinkedHashMap(
153 String[] params, String delimiter) {
154
155 LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
156
157 for (String param : params) {
158 String[] kvp = StringUtil.split(param, delimiter);
159
160 if (kvp.length == 2) {
161 map.put(kvp[0], kvp[1]);
162 }
163 else if (kvp.length == 3) {
164 String type = kvp[2];
165
166 if (type.equalsIgnoreCase("boolean") ||
167 type.equals(Boolean.class.getName())) {
168
169 map.put(kvp[0], Boolean.valueOf(kvp[1]));
170 }
171 else if (type.equalsIgnoreCase("double") ||
172 type.equals(Double.class.getName())) {
173
174 map.put(kvp[0], new Double(kvp[1]));
175 }
176 else if (type.equalsIgnoreCase("int") ||
177 type.equals(Integer.class.getName())) {
178
179 map.put(kvp[0], new Integer(kvp[1]));
180 }
181 else if (type.equalsIgnoreCase("long") ||
182 type.equals(Long.class.getName())) {
183
184 map.put(kvp[0], new Long(kvp[1]));
185 }
186 else if (type.equalsIgnoreCase("short") ||
187 type.equals(Short.class.getName())) {
188
189 map.put(kvp[0], new Short(kvp[1]));
190 }
191 else if (type.equals(String.class.getName())) {
192 map.put(kvp[0], kvp[1]);
193 }
194 else {
195 try {
196 Class<?> clazz = Class.forName(type);
197
198 Constructor<?> constructor = clazz.getConstructor(
199 String.class);
200
201 map.put(kvp[0], constructor.newInstance(kvp[1]));
202 }
203 catch (Exception e) {
204 _log.error(e.getMessage(), e);
205 }
206 }
207 }
208 }
209
210 return (LinkedHashMap<String, T>)map;
211 }
212
213 public static String toString(Map<?, ?> map) {
214 return toString(map, null, null);
215 }
216
217 public static String toString(
218 Map<?, ?> map, String hideIncludesRegex, String hideExcludesRegex) {
219
220 if ((map == null) || map.isEmpty()) {
221 return StringPool.OPEN_CURLY_BRACE + StringPool.CLOSE_CURLY_BRACE;
222 }
223
224 StringBundler sb = new StringBundler(map.size() * 4 + 1);
225
226 sb.append(StringPool.OPEN_CURLY_BRACE);
227
228 for (Map.Entry<?, ?> entry : map.entrySet()) {
229 Object key = entry.getKey();
230 Object value = entry.getValue();
231
232 String keyString = String.valueOf(key);
233
234 if (hideIncludesRegex != null) {
235 if (!keyString.matches(hideIncludesRegex)) {
236 value = "********";
237 }
238 }
239
240 if (hideExcludesRegex != null) {
241 if (keyString.matches(hideExcludesRegex)) {
242 value = "********";
243 }
244 }
245
246 sb.append(keyString);
247 sb.append(StringPool.EQUAL);
248
249 if (value instanceof Map<?, ?>) {
250 sb.append(MapUtil.toString((Map<?, ?>)value));
251 }
252 else if (value instanceof String[]) {
253 String valueString = StringUtil.merge(
254 (String[])value, StringPool.COMMA_AND_SPACE);
255
256 sb.append(
257 StringPool.OPEN_BRACKET.concat(valueString).concat(
258 StringPool.CLOSE_BRACKET));
259 }
260 else {
261 sb.append(value);
262 }
263
264 sb.append(StringPool.COMMA_AND_SPACE);
265 }
266
267 sb.setStringAt(StringPool.CLOSE_CURLY_BRACE, sb.index() - 1);
268
269 return sb.toString();
270 }
271
272 private static Log _log = LogFactoryUtil.getLog(MapUtil.class);
273
274 }