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.UnaryFunction;
22 import org.apache.commons.functor.UnaryPredicate;
23
24 /**
25 * {@link #evaluate Evaluates} to its input argument.
26 *
27 * {@link #test Tests} to the <code>boolean</code>
28 * value of the <code>Boolean</code>-valued parameter.
29 * The {@link #test} method throws an exception if
30 * the parameter isn't a non-<code>null</code>
31 * <code>Boolean</code>.
32 *
33 * @param <T> the returned value type.
34 * @version $Revision: 1160385 $ $Date: 2011-08-22 21:28:37 +0200 (Mon, 22 Aug 2011) $
35 * @author Rodney Waldhoff
36 */
37 public final class Identity<T> implements UnaryFunction<T, T>, UnaryPredicate<T>, Serializable {
38 // static attributes
39 // ------------------------------------------------------------------------
40 /**
41 * A generic {@code Identity<Object>} instance.
42 */
43 public static final Identity<Object> INSTANCE = new Identity<Object>();
44
45 /**
46 * serialVersionUID declaration.
47 */
48 private static final long serialVersionUID = 4145504259013789494L;
49
50 // constructor
51 // ------------------------------------------------------------------------
52
53 /**
54 * Create a new Identity.
55 */
56 public Identity() {
57 }
58
59 // function interface
60 // ------------------------------------------------------------------------
61
62 /**
63 * {@inheritDoc}
64 */
65 public T evaluate(T obj) {
66 return obj;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 public boolean test(Object obj) {
73 return test((Boolean) obj);
74 }
75
76 /**
77 * Test a Boolean object by returning its <code>booleanValue</code>.
78 * @param bool Boolean
79 * @return boolean
80 */
81 public boolean test(Boolean bool) {
82 return bool.booleanValue();
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 public boolean equals(Object that) {
89 return (that instanceof Identity<?>);
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 public int hashCode() {
96 return "Identity".hashCode();
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 public String toString() {
103 return "Identity";
104 }
105
106 // static methods
107 // ------------------------------------------------------------------------
108
109 /**
110 * Get an Identity instance.
111 * @param <T> the identity returned value type.
112 * @return Identity
113 */
114 public static <T> Identity<T> instance() {
115 return new Identity<T>();
116 }
117
118 }