View Javadoc

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;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.BinaryPredicate;
22  import org.apache.commons.functor.UnaryPredicate;
23  import org.apache.commons.functor.adapter.RightBoundPredicate;
24  
25  /**
26   * {@link #test Tests}
27   * <code>true</code> iff its arguments are
28   * {@link Object#equals equal} or both
29   * <code>null</code>.
30   * <p>
31   * This relation is
32   * an equivalence relation on
33   * the set of objects that adhere to the
34   * <code>Object.equals</code> contract.
35   * </p>
36   *
37   * @param <L> the left argument type.
38   * @param <R> the right argument type.
39   * @version $Revision: 1160389 $ $Date: 2011-08-22 21:31:15 +0200 (Mon, 22 Aug 2011) $
40   * @author Rodney Waldhoff
41   */
42  public final class IsEqual<L, R> implements BinaryPredicate<L, R>, Serializable {
43      // static attributes
44      // ------------------------------------------------------------------------
45      /**
46       * Basic IsEqual<Object, Object> instance.
47       */
48      public static final IsEqual<Object, Object> INSTANCE = IsEqual.<Object, Object>instance();
49      /**
50       * serialVersionUID declaration.
51       */
52      private static final long serialVersionUID = -6777016236280121159L;
53  
54      // constructor
55      // ------------------------------------------------------------------------
56      /**
57       * Create a new IsEqual.
58       */
59      public IsEqual() {
60      }
61  
62      // predicate interface
63      // ------------------------------------------------------------------------
64      /**
65       * {@inheritDoc}
66       */
67      public boolean test(L left, R right) {
68          return left == right || left != null && left.equals(right);
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public boolean equals(Object that) {
75          return that instanceof IsEqual<?, ?>;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      public int hashCode() {
82          return "IsEqual".hashCode();
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      public String toString() {
89          return "IsEqual";
90      }
91  
92      // static methods
93      // ------------------------------------------------------------------------
94      /**
95       * Get an IsEqual instance.
96       * @param <L> the left argument type.
97       * @param <R> the right argument type.
98       * @return IsEqual
99       */
100     public static <L, R> IsEqual<L, R> instance() {
101         return new IsEqual<L, R>();
102     }
103 
104     /**
105      * Get an IsEqual UnaryPredicate.
106      * @param <L> the left argument type.
107      * @param <R> the right argument type.
108      * @param object bound comparison object
109      * @return UnaryPredicate<L>
110      */
111     public static <L, R> UnaryPredicate<L> to(R object) {
112         return new RightBoundPredicate<L>(new IsEqual<L, R>(), object);
113     }
114 }