| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Constant |
|
| 1.4;1.4 |
| 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.BinaryFunction; | |
| 22 | import org.apache.commons.functor.BinaryPredicate; | |
| 23 | import org.apache.commons.functor.Function; | |
| 24 | import org.apache.commons.functor.Predicate; | |
| 25 | import org.apache.commons.functor.UnaryFunction; | |
| 26 | import org.apache.commons.functor.UnaryPredicate; | |
| 27 | ||
| 28 | /** | |
| 29 | * {@link #evaluate Evaluates} to constant value. | |
| 30 | * <p> | |
| 31 | * {@link #test Tests} to a constant value, assuming | |
| 32 | * a boolean of Boolean value is supplied. | |
| 33 | * | |
| 34 | * Note that although this class implements | |
| 35 | * {@link Serializable}, a given instance will | |
| 36 | * only be truly <code>Serializable</code> if the | |
| 37 | * constant <code>Object</code> is. Attempts to serialize | |
| 38 | * an instance whose value is not | |
| 39 | * <code>Serializable</code> will result in an exception. | |
| 40 | * </p> | |
| 41 | * @param <T> the returned value type. | |
| 42 | * @version $Revision: 1160381 $ $Date: 2011-08-22 21:23:26 +0200 (Mon, 22 Aug 2011) $ | |
| 43 | * @author Rodney Waldhoff | |
| 44 | */ | |
| 45 | public final class Constant<T> implements Function<T>, UnaryFunction<Object, T>, BinaryFunction<Object, Object, T>, | |
| 46 | Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object>, Serializable { | |
| 47 | ||
| 48 | // static attributes | |
| 49 | // ------------------------------------------------------------------------ | |
| 50 | /** | |
| 51 | * Constant for <code>true</code>. | |
| 52 | */ | |
| 53 | 2 | public static final Constant<Boolean> TRUE = of(true); |
| 54 | ||
| 55 | /** | |
| 56 | * Constant for <code>false</code>. | |
| 57 | */ | |
| 58 | 2 | public static final Constant<Boolean> FALSE = of(false); |
| 59 | ||
| 60 | /** | |
| 61 | * serialVersionUID declaration. | |
| 62 | */ | |
| 63 | private static final long serialVersionUID = -8754373778528773039L; | |
| 64 | ||
| 65 | // attributes | |
| 66 | // ------------------------------------------------------------------------ | |
| 67 | /** | |
| 68 | * The constant value. | |
| 69 | */ | |
| 70 | private final T value; | |
| 71 | ||
| 72 | // constructor | |
| 73 | // ------------------------------------------------------------------------ | |
| 74 | ||
| 75 | /** | |
| 76 | * Create a new Constant. | |
| 77 | * @param value Object | |
| 78 | */ | |
| 79 | 568 | public Constant(T value) { |
| 80 | 568 | this.value = value; |
| 81 | 568 | } |
| 82 | ||
| 83 | // function interface | |
| 84 | // ------------------------------------------------------------------------ | |
| 85 | /** | |
| 86 | * {@inheritDoc} | |
| 87 | */ | |
| 88 | public T evaluate() { | |
| 89 | 3532 | return value; |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * {@inheritDoc} | |
| 94 | */ | |
| 95 | public T evaluate(Object obj) { | |
| 96 | 150 | return evaluate(); |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * {@inheritDoc} | |
| 101 | */ | |
| 102 | public T evaluate(Object left, Object right) { | |
| 103 | 34 | return evaluate(); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * {@inheritDoc} | |
| 108 | */ | |
| 109 | public boolean test() { | |
| 110 | 3326 | return ((Boolean) evaluate()).booleanValue(); |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * {@inheritDoc} | |
| 115 | */ | |
| 116 | public boolean test(Object obj) { | |
| 117 | 1172 | return test(); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * {@inheritDoc} | |
| 122 | */ | |
| 123 | public boolean test(Object left, Object right) { | |
| 124 | 1068 | return test(); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * {@inheritDoc} | |
| 129 | */ | |
| 130 | public boolean equals(Object that) { | |
| 131 | 2420 | 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 | 596 | return (null != that && (null == this.value ? null == that.value : this.value.equals(that.value))); |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * {@inheritDoc} | |
| 145 | */ | |
| 146 | public int hashCode() { | |
| 147 | 3848 | int hash = "Constant".hashCode(); |
| 148 | 3848 | if (null != value) { |
| 149 | 3818 | hash ^= value.hashCode(); |
| 150 | } | |
| 151 | 3848 | return hash; |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * {@inheritDoc} | |
| 156 | */ | |
| 157 | public String toString() { | |
| 158 | 3320 | return "Constant<" + String.valueOf(value) + ">"; |
| 159 | } | |
| 160 | ||
| 161 | // static methods | |
| 162 | // ------------------------------------------------------------------------ | |
| 163 | ||
| 164 | /** | |
| 165 | * Get a <code>Constant</code> that always | |
| 166 | * returns <code>true</code>. | |
| 167 | * @return a <code>Constant</code> that always | |
| 168 | * returns <code>true</code> | |
| 169 | */ | |
| 170 | public static Constant<Boolean> truePredicate() { | |
| 171 | 258 | return TRUE; |
| 172 | } | |
| 173 | ||
| 174 | /** | |
| 175 | * Get a <code>Constant</code> that always | |
| 176 | * returns <code>false</code>. | |
| 177 | * @return a <code>Constant</code> that always | |
| 178 | * returns <code>false</code> | |
| 179 | */ | |
| 180 | public static Constant<Boolean> falsePredicate() { | |
| 181 | 98 | return FALSE; |
| 182 | } | |
| 183 | ||
| 184 | /** | |
| 185 | * Get a <code>Constant</code> that always | |
| 186 | * returns <i>value</i>. | |
| 187 | * @param value the constant value | |
| 188 | * @return a <code>Constant</code> that always | |
| 189 | * returns <i>value</i> | |
| 190 | */ | |
| 191 | public static Constant<Boolean> predicate(boolean value) { | |
| 192 | 10 | return value ? TRUE : FALSE; |
| 193 | } | |
| 194 | ||
| 195 | /** | |
| 196 | * Get a Constant instance for the specified value. | |
| 197 | * @param <T> the constant value | |
| 198 | * @param value T | |
| 199 | * @return Constant<T> | |
| 200 | */ | |
| 201 | public static <T> Constant<T> of(T value) { | |
| 202 | 540 | return new Constant<T>(value); |
| 203 | } | |
| 204 | ||
| 205 | } |