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} the reference (==) equality of its arguments.
27   *
28   * @param <L> the left argument type.
29   * @param <R> the right argument type.
30   * @version $Revision: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $
31   */
32  public final class IsSame<L, R> implements BinaryPredicate<L, R>, Serializable {
33      // static attributes
34      // ------------------------------------------------------------------------
35      /**
36       * Basic IsSame<Object, Object> instance.
37       */
38      public static final IsSame<Object, Object> INSTANCE = IsSame.<Object, Object>instance();
39      /**
40       * serialVersionUID declaration.
41       */
42      private static final long serialVersionUID = 7024585699909734072L;
43  
44      // constructor
45      // ------------------------------------------------------------------------
46      /**
47       * Create a new IsSame.
48       */
49      public IsSame() {
50      }
51  
52      // predicate interface
53      // ------------------------------------------------------------------------
54      /**
55       * {@inheritDoc}
56       */
57      public boolean test(L left, R right) {
58          return left == right;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public boolean equals(Object that) {
66          return that instanceof IsSame<?, ?>;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public int hashCode() {
74          return "IsSame".hashCode();
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public String toString() {
82          return "IsSame";
83      }
84  
85      // static methods
86      // ------------------------------------------------------------------------
87      /**
88       * Get an IsSame instance.
89       * @param <L> the left argument type.
90       * @param <R> the right argument type.
91       * @return IsSame
92       */
93      public static <L, R> IsSame<L, R> instance() {
94          return new IsSame<L, R>();
95      }
96  
97      /**
98       * Get an IsSame UnaryPredicate.
99       * @param <L> the left argument type.
100      * @param <R> the right argument type.
101      * @param object bound comparison object
102      * @return UnaryPredicate<L>
103      */
104     public static <L, R> UnaryPredicate<L> as(R object) {
105         return new RightBoundPredicate<L>(new IsSame<L, R>(), object);
106     }
107 }