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 import org.apache.commons.lang3.Validate;
26
27 /**
28 * @param <A> the predicate argument type.
29 * @version $Revision: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $
30 */
31 public final class IsEmpty<A> implements UnaryPredicate<A>, Serializable {
32
33 // class variables
34 // ------------------------------------------------------------------------
35
36 /**
37 * Basic IsEmpty instance.
38 */
39 public static final IsEmpty<Object> INSTANCE = new IsEmpty<Object>();
40
41 /**
42 * serialVersionUID declaration.
43 */
44 private static final long serialVersionUID = 6555097970639642373L;
45
46 // constructor
47 // ------------------------------------------------------------------------
48 /**
49 * Create a new IsEmpty.
50 */
51 public IsEmpty() {
52 }
53
54 // instance methods
55 // ------------------------------------------------------------------------
56 /**
57 * {@inheritDoc}
58 */
59 public boolean test(A obj) {
60 Validate.notNull(obj, "Argument must not be null");
61 if (obj instanceof Collection<?>) {
62 return testCollection((Collection<?>) obj);
63 }
64 if (obj instanceof Map<?, ?>) {
65 return testMap((Map<?, ?>) obj);
66 }
67 if (obj instanceof String) {
68 return testString((String) obj);
69 }
70 if (obj.getClass().isArray()) {
71 return testArray(obj);
72 }
73 throw new IllegalArgumentException("Expected Collection, Map, String or Array, found " + obj.getClass());
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public boolean equals(Object that) {
81 return that instanceof IsEmpty<?>;
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 @Override
88 public int hashCode() {
89 return "IsEmpty".hashCode();
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 public String toString() {
97 return "IsEmpty()";
98 }
99
100 /**
101 * Test a collection.
102 * @param col to test
103 * @return boolean
104 */
105 private boolean testCollection(Collection<?> col) {
106 return col.isEmpty();
107 }
108
109 /**
110 * Test a map.
111 * @param map to test
112 * @return boolean
113 */
114 private boolean testMap(Map<?, ?> map) {
115 return map.isEmpty();
116 }
117
118 /**
119 * Test a string.
120 * @param str to test
121 * @return boolean
122 */
123 private boolean testString(String str) {
124 return 0 == str.length();
125 }
126
127 /**
128 * Test an array.
129 * @param array to test
130 * @return boolean
131 */
132 private boolean testArray(Object array) {
133 return 0 == Array.getLength(array);
134 }
135
136 // static
137 // ------------------------------------------------------------------------
138 /**
139 * Get an IsEmpty instance.
140 *
141 * @param <A> the predicate argument type.
142 * @return IsEmpty
143 */
144 public static <A> IsEmpty<A> instance() {
145 return new IsEmpty<A>();
146 }
147
148 }