/
CollectionsUtil.java
111 lines (98 loc) · 3.1 KB
/
CollectionsUtil.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
package nallar.tickthreading.util;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Ordering;
import nallar.tickthreading.Log;
public enum CollectionsUtil {
;
private static final String defaultDelimiter = ",";
public static List<String> split(String input) {
return split(input, defaultDelimiter);
}
public static List<?> newList(List<?> input, Function<Object, ?> function) {
List<Object> newList = new ArrayList<Object>(input.size());
for (Object o : input) {
newList.add(function.apply(o));
}
return newList;
}
public static List<String> split(String input, String delimiter) {
if (input == null || input.isEmpty()) {
return Collections.emptyList();
}
return new ArrayList<String>(Arrays.asList(input.split(delimiter)));
}
public static <T> List<T> toObjects(Iterable<String> stringIterable, Class<T> type) {
Constructor<?> constructor;
try {
constructor = type.getConstructor(String.class);
} catch (NoSuchMethodException e) {
Log.severe("Failed to convert string list to " + type, e);
return Collections.emptyList();
}
List<Object> objects = new ArrayList<Object>();
for (String s : stringIterable) {
try {
objects.add(constructor.newInstance(s));
} catch (Exception e) {
Log.severe("Failed to convert string list to " + type + " with string " + s, e);
}
}
return (List<T>) objects;
}
public static String join(Iterable iterable) {
return join(iterable, defaultDelimiter);
}
public static String join(Iterable iterable, String delimiter) {
StringBuilder stringBuilder = new StringBuilder();
boolean join = false;
for (Object o : iterable) {
if (join) {
stringBuilder.append(delimiter);
}
stringBuilder.append(o);
join = true;
}
return stringBuilder.toString();
}
public static String joinMap(Map<?, ?> map) {
if (map.isEmpty()) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
boolean notFirst = false;
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (notFirst) {
stringBuilder.append(',');
}
stringBuilder.append(entry.getKey().toString()).append(':').append(entry.getValue().toString());
notFirst = true;
}
return stringBuilder.toString();
}
@SuppressWarnings ("unchecked")
public static <K, V> Map<K, V> map(Object... objects) {
HashMap map = new HashMap();
Object key = null;
for (final Object object : objects) {
if (key == null) {
key = object;
} else {
map.put(key, object);
key = null;
}
}
return map;
}
public static <T> List<T> sortedKeys(Map<T, ? extends Comparable<?>> map, int elements) {
List<T> list = Ordering.natural().reverse().onResultOf(Functions.forMap(map)).immutableSortedCopy(map.keySet());
return list.size() > elements ? list.subList(0, elements) : list;
}
}