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