-
Notifications
You must be signed in to change notification settings - Fork 26.3k
/
CollectionUtils.java
239 lines (217 loc) · 7.55 KB
/
CollectionUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
public class CollectionUtils {
private static final Comparator<String> SIMPLE_NAME_COMPARATOR = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (s1 == null && s2 == null) {
return 0;
}
if (s1 == null) {
return -1;
}
if (s2 == null) {
return 1;
}
int i1 = s1.lastIndexOf('.');
if (i1 >= 0) {
s1 = s1.substring(i1 + 1);
}
int i2 = s2.lastIndexOf('.');
if (i2 >= 0) {
s2 = s2.substring(i2 + 1);
}
return s1.compareToIgnoreCase(s2);
}
};
private CollectionUtils() {
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> List<T> sort(List<T> list) {
if (isNotEmpty(list)) {
Collections.sort((List) list);
}
return list;
}
public static List<String> sortSimpleName(List<String> list) {
if (list != null && list.size() > 0) {
Collections.sort(list, SIMPLE_NAME_COMPARATOR);
}
return list;
}
public static Map<String, Map<String, String>> splitAll(Map<String, List<String>> list, String separator) {
if (list == null) {
return null;
}
Map<String, Map<String, String>> result = new HashMap<>();
for (Map.Entry<String, List<String>> entry : list.entrySet()) {
result.put(entry.getKey(), split(entry.getValue(), separator));
}
return result;
}
public static Map<String, List<String>> joinAll(Map<String, Map<String, String>> map, String separator) {
if (map == null) {
return null;
}
Map<String, List<String>> result = new HashMap<>();
for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) {
result.put(entry.getKey(), join(entry.getValue(), separator));
}
return result;
}
public static Map<String, String> split(List<String> list, String separator) {
if (list == null) {
return null;
}
Map<String, String> map = new HashMap<>();
if (list.isEmpty()) {
return map;
}
for (String item : list) {
int index = item.indexOf(separator);
if (index == -1) {
map.put(item, "");
} else {
map.put(item.substring(0, index), item.substring(index + 1));
}
}
return map;
}
public static List<String> join(Map<String, String> map, String separator) {
if (map == null) {
return null;
}
List<String> list = new ArrayList<>();
if (map.size() == 0) {
return list;
}
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (StringUtils.isEmpty(value)) {
list.add(key);
} else {
list.add(key + separator + value);
}
}
return list;
}
public static String join(List<String> list, String separator) {
StringBuilder sb = new StringBuilder();
for (String ele : list) {
if (sb.length() > 0) {
sb.append(separator);
}
sb.append(ele);
}
return sb.toString();
}
public static boolean mapEquals(Map<?, ?> map1, Map<?, ?> map2) {
if (map1 == null && map2 == null) {
return true;
}
if (map1 == null || map2 == null) {
return false;
}
if (map1.size() != map2.size()) {
return false;
}
for (Map.Entry<?, ?> entry : map1.entrySet()) {
Object key = entry.getKey();
Object value1 = entry.getValue();
Object value2 = map2.get(key);
if (!objectEquals(value1, value2)) {
return false;
}
}
return true;
}
private static boolean objectEquals(Object obj1, Object obj2) {
if (obj1 == null && obj2 == null) {
return true;
}
if (obj1 == null || obj2 == null) {
return false;
}
return obj1.equals(obj2);
}
public static Map<String, String> toStringMap(String... pairs) {
Map<String, String> parameters = new HashMap<>();
if (pairs.length > 0) {
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("pairs must be even.");
}
for (int i = 0; i < pairs.length; i = i + 2) {
parameters.put(pairs[i], pairs[i + 1]);
}
}
return parameters;
}
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> toMap(Object... pairs) {
Map<K, V> ret = new HashMap<>();
if (pairs == null || pairs.length == 0) {
return ret;
}
if (pairs.length % 2 != 0) {
throw new IllegalArgumentException("Map pairs can not be odd number.");
}
int len = pairs.length / 2;
for (int i = 0; i < len; i++) {
ret.put((K) pairs[2 * i], (V) pairs[2 * i + 1]);
}
return ret;
}
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
public static boolean isEmptyMap(Map map) {
return map == null || map.size() == 0;
}
public static boolean isNotEmptyMap(Map map) {
return !isEmptyMap(map);
}
/**
* Convert to multiple values to be {@link HashSet}
*
* @param values one or more values
* @param <T> the type of <code>values</code>
* @return read-only {@link HashSet}
*/
public static <T> Set<T> asHashSet(T... values) {
if (values == null || values.length < 1) {
return emptySet();
}
return unmodifiableSet(new HashSet(asList(values)));
}
}