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
|
package com.temp.shared.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Utility methods around Collections / Arrays to reduce the code in other
* places (e.g. the Activities).
*
* @author georgs
*/
public class CollectionsUtils {
public static <T> List<T> deNull(List<T> list) {
if (list != null) { // can not "in line" as compiler gets confused!
return list;
}
return Collections.emptyList();
}
public static <T> List<T> noEmpty(List<T> list) {
return isEmpty(list) ? null : list;
}
public static boolean isEmpty(Collection<?> collection) {
return (collection == null) || collection.isEmpty();
}
public static boolean isEmpty(Map<?, ?> collection) {
return (collection == null) || collection.isEmpty();
}
public static boolean isEmpty(Object[] array) {
return (array == null) || (array.length == 0);
}
/**
* Use type inference to create a map - from Guava
*/
public static <V> List<V> newArrayList() {
return new ArrayList<V>();
}
/**
* Use type inference to create a map - from Guava
*/
public static <K, V> Map<K, V> newHashMap() {
return new HashMap<K, V>();
}
public static Map<String, String> newMap(String... keyValuePairs) {
Map<String, String> map = newHashMap();
if (!isEmpty(keyValuePairs)) {
if ((keyValuePairs.length & 1) == 1) { // Odd == Not Paired!
throw new IllegalArgumentException("To construct a String String Map, must provide Strings in pairs (key, value)");
}
for (int i = 0; i < keyValuePairs.length;) {
String key = keyValuePairs[i++];
String value = keyValuePairs[i++];
addNonEmptiesTo(map, key, value);
}
}
return map;
}
public static Map<String, String> addNonEmptiesTo(Map<String, String> map, String key, String value) {
map.put(Assert.notNullOrEmpty("key", key), Assert.notNullOrEmpty("value", value));
return map;
}
public static <K, V> Map<K, V> addNonNullsTo(Map<K, V> map, K key, V value) {
Assert.notNull("key", key);
Assert.notNull("value", value);
map.put(key, value);
return map;
}
}
|