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     */
017    package org.apache.commons.functor.core;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.BinaryFunction;
022    import org.apache.commons.functor.BinaryPredicate;
023    import org.apache.commons.functor.Function;
024    import org.apache.commons.functor.Predicate;
025    import org.apache.commons.functor.UnaryFunction;
026    import 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: 1160381 $ $Date: 2011-08-22 21:23:26 +0200 (Mon, 22 Aug 2011) $
043     * @author Rodney Waldhoff
044     */
045    public final class Constant<T> implements Function<T>, UnaryFunction<Object, T>, BinaryFunction<Object, Object, T>,
046            Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object>, Serializable {
047    
048        // static attributes
049        // ------------------------------------------------------------------------
050        /**
051         * Constant for <code>true</code>.
052         */
053        public static final Constant<Boolean> TRUE = of(true);
054    
055        /**
056         * Constant for <code>false</code>.
057         */
058        public static final Constant<Boolean> FALSE = of(false);
059    
060        /**
061         * serialVersionUID declaration.
062         */
063        private static final long serialVersionUID = -8754373778528773039L;
064    
065        // attributes
066        // ------------------------------------------------------------------------
067        /**
068         * The constant value.
069         */
070        private final T value;
071    
072        // constructor
073        // ------------------------------------------------------------------------
074    
075        /**
076         * Create a new Constant.
077         * @param value Object
078         */
079        public Constant(T value) {
080            this.value = value;
081        }
082    
083        // function interface
084        // ------------------------------------------------------------------------
085        /**
086         * {@inheritDoc}
087         */
088        public T evaluate() {
089            return value;
090        }
091    
092        /**
093         * {@inheritDoc}
094         */
095        public T evaluate(Object obj) {
096            return evaluate();
097        }
098    
099        /**
100         * {@inheritDoc}
101         */
102        public T evaluate(Object left, Object right) {
103            return evaluate();
104        }
105    
106        /**
107         * {@inheritDoc}
108         */
109        public boolean test() {
110            return ((Boolean) evaluate()).booleanValue();
111        }
112    
113        /**
114         * {@inheritDoc}
115         */
116        public boolean test(Object obj) {
117            return test();
118        }
119    
120        /**
121         * {@inheritDoc}
122         */
123        public boolean test(Object left, Object right) {
124            return test();
125        }
126    
127        /**
128         * {@inheritDoc}
129         */
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        public int hashCode() {
147            int hash = "Constant".hashCode();
148            if (null != value) {
149                hash ^= value.hashCode();
150            }
151            return hash;
152        }
153    
154        /**
155         * {@inheritDoc}
156         */
157        public String toString() {
158            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            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            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            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            return new Constant<T>(value);
203        }
204    
205    }