001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.functor.core; 018 019import java.io.Serializable; 020 021import org.apache.commons.functor.BinaryFunction; 022import org.apache.commons.functor.BinaryPredicate; 023import org.apache.commons.functor.Function; 024import org.apache.commons.functor.Predicate; 025import org.apache.commons.functor.UnaryFunction; 026import org.apache.commons.functor.UnaryPredicate; 027 028/** 029 * {@link #evaluate Evaluates} to constant value. 030 * <p> 031 * {@link #test Tests} to a constant value, assuming 032 * a boolean of Boolean value is supplied. 033 * 034 * Note that although this class implements 035 * {@link Serializable}, a given instance will 036 * only be truly <code>Serializable</code> if the 037 * constant <code>Object</code> is. Attempts to serialize 038 * an instance whose value is not 039 * <code>Serializable</code> will result in an exception. 040 * </p> 041 * @param <T> the returned value type. 042 * @version $Revision: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $ 043 */ 044public final class Constant<T> implements Function<T>, UnaryFunction<Object, T>, BinaryFunction<Object, Object, T>, 045 Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object>, Serializable { 046 047 // static attributes 048 // ------------------------------------------------------------------------ 049 /** 050 * Constant for <code>true</code>. 051 */ 052 public static final Constant<Boolean> TRUE = of(Boolean.TRUE); 053 054 /** 055 * Constant for <code>false</code>. 056 */ 057 public static final Constant<Boolean> FALSE = of(Boolean.FALSE); 058 059 /** 060 * serialVersionUID declaration. 061 */ 062 private static final long serialVersionUID = -8754373778528773039L; 063 064 // attributes 065 // ------------------------------------------------------------------------ 066 /** 067 * The constant value. 068 */ 069 private final T value; 070 071 // constructor 072 // ------------------------------------------------------------------------ 073 074 /** 075 * Create a new Constant. 076 * @param value Object 077 */ 078 public Constant(T value) { 079 this.value = value; 080 } 081 082 // function interface 083 // ------------------------------------------------------------------------ 084 /** 085 * {@inheritDoc} 086 */ 087 public T evaluate() { 088 return value; 089 } 090 091 /** 092 * {@inheritDoc} 093 */ 094 public T evaluate(Object obj) { 095 return evaluate(); 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 public T evaluate(Object left, Object right) { 102 return evaluate(); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 public boolean test() { 109 return ((Boolean) evaluate()).booleanValue(); 110 } 111 112 /** 113 * {@inheritDoc} 114 */ 115 public boolean test(Object obj) { 116 return test(); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 public boolean test(Object left, Object right) { 123 return test(); 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 public boolean equals(Object that) { 131 return that == this || (that instanceof Constant<?> && equals((Constant<?>) that)); 132 } 133 134 /** 135 * Learn whether another Constant is equal to this. 136 * @param that Constant to test 137 * @return boolean 138 */ 139 public boolean equals(Constant<?> that) { 140 return (null != that && (null == this.value ? null == that.value : this.value.equals(that.value))); 141 } 142 143 /** 144 * {@inheritDoc} 145 */ 146 @Override 147 public int hashCode() { 148 int hash = "Constant".hashCode(); 149 if (null != value) { 150 hash ^= value.hashCode(); 151 } 152 return hash; 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public String toString() { 160 return "Constant<" + String.valueOf(value) + ">"; 161 } 162 163 // static methods 164 // ------------------------------------------------------------------------ 165 166 /** 167 * Get a <code>Constant</code> that always 168 * returns <code>true</code>. 169 * @return a <code>Constant</code> that always 170 * returns <code>true</code> 171 */ 172 public static Constant<Boolean> truePredicate() { 173 return TRUE; 174 } 175 176 /** 177 * Get a <code>Constant</code> that always 178 * returns <code>false</code>. 179 * @return a <code>Constant</code> that always 180 * returns <code>false</code> 181 */ 182 public static Constant<Boolean> falsePredicate() { 183 return FALSE; 184 } 185 186 /** 187 * Get a <code>Constant</code> that always 188 * returns <i>value</i>. 189 * @param value the constant value 190 * @return a <code>Constant</code> that always 191 * returns <i>value</i> 192 */ 193 public static Constant<Boolean> predicate(boolean value) { 194 return value ? TRUE : FALSE; 195 } 196 197 /** 198 * Get a Constant instance for the specified value. 199 * @param <T> the constant value 200 * @param value T 201 * @return Constant<T> 202 */ 203 public static <T> Constant<T> of(T value) { 204 return new Constant<T>(value); 205 } 206 207}