1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.core.collection;
18
19 import java.io.Serializable;
20 import java.lang.reflect.Array;
21 import java.util.Collection;
22 import java.util.Map;
23
24 import org.apache.commons.functor.UnaryPredicate;
25
26
27
28
29
30 public final class IsEmpty<A> implements UnaryPredicate<A>, Serializable {
31
32
33
34
35
36
37
38 public static final IsEmpty<Object> INSTANCE = new IsEmpty<Object>();
39
40
41
42
43 private static final long serialVersionUID = 6555097970639642373L;
44
45
46
47
48
49
50 public IsEmpty() {
51 }
52
53
54
55
56
57
58 public boolean test(A obj) {
59 if (obj instanceof Collection<?>) {
60 return testCollection((Collection<?>) obj);
61 }
62 if (obj instanceof Map<?, ?>) {
63 return testMap((Map<?, ?>) obj);
64 }
65 if (obj instanceof String) {
66 return testString((String) obj);
67 }
68 if (null != obj && obj.getClass().isArray()) {
69 return testArray(obj);
70 }
71 if (null == obj) {
72 throw new IllegalArgumentException("Argument must not be null");
73 }
74 throw new IllegalArgumentException("Expected Collection, Map, String or Array, found " + obj.getClass());
75 }
76
77
78
79
80 public boolean equals(Object that) {
81 return that instanceof IsEmpty<?>;
82 }
83
84
85
86
87 public int hashCode() {
88 return "IsEmpty".hashCode();
89 }
90
91
92
93
94 public String toString() {
95 return "IsEmpty()";
96 }
97
98
99
100
101
102
103 private boolean testCollection(Collection<?> col) {
104 return col.isEmpty();
105 }
106
107
108
109
110
111
112 private boolean testMap(Map<?, ?> map) {
113 return map.isEmpty();
114 }
115
116
117
118
119
120
121 private boolean testString(String str) {
122 return 0 == str.length();
123 }
124
125
126
127
128
129
130 private boolean testArray(Object array) {
131 return 0 == Array.getLength(array);
132 }
133
134
135
136
137
138
139
140 public static <A> IsEmpty<A> instance() {
141 return new IsEmpty<A>();
142 }
143
144 }