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: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
43   */
44  public final class Constant<T> implements Function<T>, UnaryFunction<Object, T>, BinaryFunction<Object, Object, T>,
45          Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object>, Serializable {
46  
47      // static attributes
48      // ------------------------------------------------------------------------
49      /**
50       * Constant for <code>true</code>.
51       */
52      public static final Constant<Boolean> TRUE = of(Boolean.TRUE);
53  
54      /**
55       * Constant for <code>false</code>.
56       */
57      public static final Constant<Boolean> FALSE = of(Boolean.FALSE);
58  
59      /**
60       * serialVersionUID declaration.
61       */
62      private static final long serialVersionUID = -8754373778528773039L;
63  
64      // attributes
65      // ------------------------------------------------------------------------
66      /**
67       * The constant value.
68       */
69      private final T value;
70  
71      // constructor
72      // ------------------------------------------------------------------------
73  
74      /**
75       * Create a new Constant.
76       * @param value Object
77       */
78      public Constant(T value) {
79          this.value = value;
80      }
81  
82      // function interface
83      // ------------------------------------------------------------------------
84      /**
85       * {@inheritDoc}
86       */
87      public T evaluate() {
88          return value;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      public T evaluate(Object obj) {
95          return evaluate();
96      }
97  
98      /**
99       * {@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 }