1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * @version $Revision: 1166333 $ $Date: 2011-09-07 21:36:42 +0200 (Wed, 07 Sep 2011) $
28 * @author Rodney Waldhoff
29 */
30 public final class IsEmpty<A> implements UnaryPredicate<A>, Serializable {
31
32 // class variables
33 // ------------------------------------------------------------------------
34
35 /**
36 * Basic IsEmpty instance.
37 */
38 public static final IsEmpty<Object> INSTANCE = new IsEmpty<Object>();
39
40 /**
41 * serialVersionUID declaration.
42 */
43 private static final long serialVersionUID = 6555097970639642373L;
44
45 // constructor
46 // ------------------------------------------------------------------------
47 /**
48 * Create a new IsEmpty.
49 */
50 public IsEmpty() {
51 }
52
53 // instance methods
54 // ------------------------------------------------------------------------
55 /**
56 * {@inheritDoc}
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 * {@inheritDoc}
79 */
80 public boolean equals(Object that) {
81 return that instanceof IsEmpty<?>;
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public int hashCode() {
88 return "IsEmpty".hashCode();
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 public String toString() {
95 return "IsEmpty()";
96 }
97
98 /**
99 * Test a collection.
100 * @param col to test
101 * @return boolean
102 */
103 private boolean testCollection(Collection<?> col) {
104 return col.isEmpty();
105 }
106
107 /**
108 * Test a map.
109 * @param map to test
110 * @return boolean
111 */
112 private boolean testMap(Map<?, ?> map) {
113 return map.isEmpty();
114 }
115
116 /**
117 * Test a string.
118 * @param str to test
119 * @return boolean
120 */
121 private boolean testString(String str) {
122 return 0 == str.length();
123 }
124
125 /**
126 * Test an array.
127 * @param array to test
128 * @return boolean
129 */
130 private boolean testArray(Object array) {
131 return 0 == Array.getLength(array);
132 }
133
134 // static
135 // ------------------------------------------------------------------------
136 /**
137 * Get an IsEmpty instance.
138 * @return IsEmpty
139 */
140 public static <A> IsEmpty<A> instance() {
141 return new IsEmpty<A>();
142 }
143
144 }