View Javadoc

1   /*
2    *  Copyright 2001-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.collections15.functors.predicate;
17  
18  import net.sf.collections15.Predicate;
19  
20  import java.io.Serializable;
21  
22  
23  /***
24   * <code>Predicate</code> implementation that evaluates to <code>true</code> if
25   * the input is the same object as the one stored in this
26   * <code>Predicate</code>.
27   *
28   * @author Stephen Colebourne
29   * @author Chris Lambrou (port to Java 5.0)
30   * @see EqualPredicate
31   * @since Collections15 1.0
32   */
33  public final class SamePredicate <E> implements Predicate<E>, Serializable
34  {
35  
36      static final long serialVersionUID = 8104837018908641088L;
37  
38      /***
39       * The value to compare input objects to.
40       */
41      private final E value;
42  
43      /***
44       * Returns an instance that compares input objects to the specified value.
45       *
46       * @param value The object to compare input objects to.
47       *
48       * @return An instance that compares input objects to the specified value.
49       *
50       * @throws IllegalArgumentException Thrown if the specified value is
51       *                                  <code>null</code>.
52       */
53      public static <T> SamePredicate<T> getInstance(T value)
54      {
55          return new SamePredicate<T>(value);
56      }
57  
58      /***
59       * Creates a new instance that compares input objects to the specified
60       * value.
61       *
62       * @param value The object to compare input objects to.
63       *
64       * @throws IllegalArgumentException Thrown if the specified value is
65       *                                  <code>null</code>.
66       */
67      protected SamePredicate(E value)
68      {
69          if (value == null) {
70              throw new IllegalArgumentException("null value specified");
71          }
72          this.value = value;
73      }
74  
75      /***
76       * Evaluates the predicate returning <code>true</code> if the input object
77       * is the same object as the stored value.
78       *
79       * @param object the input object
80       *
81       * @return true if input object equals stored value
82       */
83      public boolean evaluate(E object)
84      {
85          return value == object;
86      }
87  
88      /***
89       * Gets the value.
90       *
91       * @return The value against which input objects are compared.
92       *
93       * @since Collections15 1.0
94       */
95      public E getValue()
96      {
97          return value;
98      }
99  
100 }