1 |
| package net.sourceforge.pmd.util; |
2 |
| |
3 |
| import java.util.HashMap; |
4 |
| import java.util.HashSet; |
5 |
| import java.util.Iterator; |
6 |
| import java.util.Map; |
7 |
| import java.util.Set; |
8 |
| import java.util.Map.Entry; |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| public class CollectionUtil { |
17 |
| |
18 |
| public static final TypeMap collectionInterfacesByNames = new TypeMap( new Class[] { |
19 |
| java.util.List.class, |
20 |
| java.util.Collection.class, |
21 |
| java.util.Map.class, |
22 |
| java.util.Set.class, |
23 |
| }); |
24 |
| |
25 |
| public static final TypeMap collectionClassesByNames = new TypeMap( new Class[] { |
26 |
| java.util.ArrayList.class, |
27 |
| java.util.LinkedList.class, |
28 |
| java.util.Vector.class, |
29 |
| java.util.HashMap.class, |
30 |
| java.util.LinkedHashMap.class, |
31 |
| java.util.TreeMap.class, |
32 |
| java.util.TreeSet.class, |
33 |
| java.util.HashSet.class, |
34 |
| java.util.LinkedHashSet.class |
35 |
| }); |
36 |
| |
37 |
0
| private CollectionUtil() {};
|
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
4
| public static Class getCollectionTypeFor(String shortName) {
|
46 |
4
| Class cls = collectionClassesByNames.typeFor(shortName);
|
47 |
3
| if (cls != null) return cls;
|
48 |
| |
49 |
1
| return collectionInterfacesByNames.typeFor(shortName);
|
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
30
| public static boolean isCollectionType(String typeName, boolean includeInterfaces) {
|
61 |
| |
62 |
16
| if (collectionClassesByNames.contains(typeName)) return true;
|
63 |
| |
64 |
14
| return includeInterfaces && collectionInterfacesByNames.contains(typeName);
|
65 |
| } |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
21
| public static boolean isCollectionType(Class clazzType, boolean includeInterfaces) {
|
76 |
| |
77 |
21
| if (collectionClassesByNames.contains(clazzType)) {
|
78 |
17
| return true;
|
79 |
| } |
80 |
| |
81 |
4
| return includeInterfaces && collectionInterfacesByNames.contains(clazzType);
|
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
135
| public static Set asSet(Object[] items) {
|
91 |
| |
92 |
135
| Set set = new HashSet(items.length);
|
93 |
135
| for (int i=0; i<items.length; i++) {
|
94 |
992
| set.add(items[i]);
|
95 |
| } |
96 |
135
| return set;
|
97 |
| } |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
452
| public static Map mapFrom(Object[][] keyValueSets) {
|
107 |
452
| Map map = new HashMap(keyValueSets.length);
|
108 |
452
| for (int i=0; i<keyValueSets.length; i++) {
|
109 |
2252
| map.put(keyValueSets[i][0], keyValueSets[i][1]);
|
110 |
| } |
111 |
452
| return map;
|
112 |
| } |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
6
| public static Map invertedMapFrom(Map source) {
|
121 |
6
| Map map = new HashMap(source.size());
|
122 |
6
| Iterator iter = source.entrySet().iterator();
|
123 |
6
| Entry entry;
|
124 |
6
| while (iter.hasNext()) {
|
125 |
22
| entry = (Entry)iter.next();
|
126 |
22
| map.put(entry.getValue(), entry.getKey());
|
127 |
| } |
128 |
6
| return map;
|
129 |
| } |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
9
| public static final boolean arraysAreEqual(Object value, Object otherValue) {
|
140 |
9
| if (value instanceof Object[]) {
|
141 |
9
| if (otherValue instanceof Object[]) return valuesAreTransitivelyEqual((Object[])value, (Object[])otherValue);
|
142 |
0
| return false;
|
143 |
| } |
144 |
0
| return false;
|
145 |
| } |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
9
| public static final boolean valuesAreTransitivelyEqual(Object[] thisArray, Object[] thatArray) {
|
156 |
0
| if (thisArray == thatArray) return true;
|
157 |
0
| if ((thisArray == null) || (thatArray == null)) return false;
|
158 |
0
| if (thisArray.length != thatArray.length) return false;
|
159 |
9
| for (int i = 0; i < thisArray.length; i++) {
|
160 |
0
| if (!areEqual(thisArray[i], thatArray[i])) return false;
|
161 |
| } |
162 |
9
| return true;
|
163 |
| } |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
98
| public static final boolean areEqual(Object value, Object otherValue) {
|
173 |
34
| if (value == otherValue) return true;
|
174 |
0
| if (value == null) return false;
|
175 |
0
| if (otherValue == null) return false;
|
176 |
| |
177 |
8
| if (value.getClass().getComponentType() != null) return arraysAreEqual(value, otherValue);
|
178 |
56
| return value.equals(otherValue);
|
179 |
| } |
180 |
| } |