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
23 import org.apache.commons.functor.BinaryPredicate;
24 import org.apache.commons.functor.UnaryPredicate;
25 import org.apache.commons.functor.adapter.RightBoundPredicate;
26
27 /**
28 * A {@link BinaryPredicate} that checks to see if the
29 * specified object is an element of the specified
30 * Collection.
31 *
32 * @since 1.0
33 * @version $Revision: 1170771 $ $Date: 2011-09-14 21:07:22 +0200 (Wed, 14 Sep 2011) $
34 * @author Jason Horman (jason@jhorman.org)
35 * @author Rodney Waldhoff
36 */
37 public final class IsElementOf<L, R> implements BinaryPredicate<L, R>, Serializable {
38 // static members
39 //---------------------------------------------------------------
40
41 /**
42 * serialVersionUID declaration.
43 */
44 private static final long serialVersionUID = -7639051806015321070L;
45 private static final IsElementOf<Object, Object> INSTANCE = new IsElementOf<Object, Object>();
46
47 // constructors
48 //---------------------------------------------------------------
49 /**
50 * Create a new IsElementOf.
51 */
52 public IsElementOf() {
53 }
54
55 // instance methods
56 //---------------------------------------------------------------
57 /**
58 * {@inheritDoc}
59 */
60 public boolean test(L obj, R col) {
61 if (col instanceof Collection<?>) {
62 return testCollection(obj, (Collection<?>) col);
63 }
64 if (null != col && col.getClass().isArray()) {
65 return testArray(obj, col);
66 }
67 if (null == col) {
68 throw new IllegalArgumentException("Right side argument must not be null.");
69 }
70 throw new IllegalArgumentException("Expected Collection or Array, found " + col.getClass());
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 public boolean equals(Object obj) {
77 return (obj instanceof IsElementOf<?, ?>);
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 public int hashCode() {
84 return "IsElementOf".hashCode();
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 public String toString() {
91 return "IsElementOf";
92 }
93
94 /**
95 * Test a collection.
96 * @param obj to find
97 * @param col to search
98 * @return boolean
99 */
100 private boolean testCollection(Object obj, Collection<?> col) {
101 return col.contains(obj);
102 }
103
104 /**
105 * Test an array.
106 * @param obj to find
107 * @param array to search
108 * @return boolean
109 */
110 private boolean testArray(Object obj, Object array) {
111 for (int i = 0, m = Array.getLength(array); i < m; i++) {
112 Object value = Array.get(array, i);
113 if (obj == value) {
114 return true;
115 }
116 if (obj != null && obj.equals(value)) {
117 return true;
118 }
119 }
120 return false;
121 }
122
123 // static methods
124 //---------------------------------------------------------------
125 /**
126 * Get an IsElementOf instance.
127 * @return IsElementOf
128 */
129 public static IsElementOf<Object, Object> instance() {
130 return INSTANCE;
131 }
132
133 /**
134 * Get an IsElementOf(collection|array) UnaryPredicate.
135 * @param obj collection/array to search
136 * @return UnaryPredicate
137 */
138 public static <A> UnaryPredicate<A> instance(Object obj) {
139 if (null == obj) {
140 throw new NullPointerException("Argument must not be null");
141 } else if (obj instanceof Collection<?>) {
142 return new RightBoundPredicate<A>(new IsElementOf<A, Object>(), obj);
143 } else if (obj.getClass().isArray()) {
144 return new RightBoundPredicate<A>(new IsElementOf<A, Object>(), obj);
145 } else {
146 throw new IllegalArgumentException("Expected Collection or Array, found " + obj.getClass());
147 }
148 }
149
150 }