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: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 35 */ 36 public final class Identity<T> implements UnaryFunction<T, T>, UnaryPredicate<T>, Serializable { 37 // static attributes 38 // ------------------------------------------------------------------------ 39 /** 40 * A generic {@code Identity<Object>} instance. 41 */ 42 public static final Identity<Object> INSTANCE = new Identity<Object>(); 43 44 /** 45 * serialVersionUID declaration. 46 */ 47 private static final long serialVersionUID = 4145504259013789494L; 48 49 // constructor 50 // ------------------------------------------------------------------------ 51 52 /** 53 * Create a new Identity. 54 */ 55 public Identity() { 56 } 57 58 // function interface 59 // ------------------------------------------------------------------------ 60 61 /** 62 * {@inheritDoc} 63 */ 64 public T evaluate(T obj) { 65 return obj; 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 public boolean test(Object obj) { 72 return test((Boolean) obj); 73 } 74 75 /** 76 * Test a Boolean object by returning its <code>booleanValue</code>. 77 * @param bool Boolean 78 * @return boolean 79 */ 80 public boolean test(Boolean bool) { 81 return bool.booleanValue(); 82 } 83 84 /** 85 * {@inheritDoc} 86 */ 87 @Override 88 public boolean equals(Object that) { 89 return (that instanceof Identity<?>); 90 } 91 92 /** 93 * {@inheritDoc} 94 */ 95 @Override 96 public int hashCode() { 97 return "Identity".hashCode(); 98 } 99 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public String toString() { 105 return "Identity"; 106 } 107 108 // static methods 109 // ------------------------------------------------------------------------ 110 111 /** 112 * Get an Identity instance. 113 * @param <T> the identity returned value type. 114 * @return Identity 115 */ 116 public static <T> Identity<T> instance() { 117 return new Identity<T>(); 118 } 119 120 }