1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import java.util.Map;
18
19
20 public class MapUtils extends org.apache.commons.collections.MapUtils
21 {
22
23
24
25
26
27 public static Map mapWithKeysAndValues(Class mapClass, Object[] keys, Object[] values)
28 {
29 Collection keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST);
30 Collection valuesCollection = (values != null ? Arrays.asList(values) : Collections.EMPTY_LIST);
31 return mapWithKeysAndValues(mapClass, keyCollection.iterator(), valuesCollection.iterator());
32 }
33
34
35
36
37
38 public static Map mapWithKeysAndValues(Class mapClass, Collection keys, Collection values)
39 {
40 keys = (keys != null ? keys : Collections.EMPTY_LIST);
41 values = (values != null ? values : Collections.EMPTY_LIST);
42 return mapWithKeysAndValues(mapClass, keys.iterator(), values.iterator());
43 }
44
45
46
47
48
49
50
51
52
53
54 public static Map mapWithKeysAndValues(Class mapClass, Iterator keys, Iterator values)
55 {
56 Map m = null;
57
58 if (mapClass == null)
59 {
60 throw new IllegalArgumentException("Map class must not be null!");
61 }
62
63 try
64 {
65 m = (Map) mapClass.newInstance();
66 }
67 catch (Exception ex)
68 {
69 throw new RuntimeException(ex);
70 }
71
72 if (keys != null && values != null)
73 {
74 while (keys.hasNext() && values.hasNext())
75 {
76 m.put(keys.next(), values.next());
77 }
78 }
79
80 return m;
81 }
82
83
84
85
86
87
88
89
90
91 public static String toString(Map props, boolean newline)
92 {
93 if (props == null || props.isEmpty())
94 {
95 return "{}";
96 }
97
98 StringBuffer buf = new StringBuffer(props.size() * 32);
99 buf.append('{');
100
101 if (newline)
102 {
103 buf.append(SystemUtils.LINE_SEPARATOR);
104 }
105
106 Object[] entries = props.entrySet().toArray();
107 int i;
108
109 for (i = 0; i < entries.length - 1; i++)
110 {
111 Map.Entry property = (Map.Entry) entries[i];
112 buf.append(property.getKey());
113 buf.append('=');
114 buf.append(PropertiesUtils.maskedPropertyValue(property));
115
116 if (newline)
117 {
118 buf.append(SystemUtils.LINE_SEPARATOR);
119 }
120 else
121 {
122 buf.append(',').append(' ');
123 }
124 }
125
126
127 Map.Entry lastProperty = (Map.Entry) entries[i];
128 buf.append(lastProperty.getKey().toString());
129 buf.append('=');
130 buf.append(PropertiesUtils.maskedPropertyValue(lastProperty));
131
132 if (newline)
133 {
134 buf.append(SystemUtils.LINE_SEPARATOR);
135 }
136
137 buf.append('}');
138 return buf.toString();
139 }
140
141 }