View Javadoc
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.adapter;
18  
19  import org.apache.commons.functor.NullaryFunction;
20  import org.apache.commons.functor.NullaryProcedure;
21  import org.apache.commons.lang3.Validate;
22  
23  /**
24   * Adapts a
25   * {@link NullaryProcedure NullaryProcedure}
26   * to the
27   * {@link NullaryFunction NullaryFunction} interface
28   * by always returning <code>null</code>.
29   *
30   * @param <T> the returned value type.
31   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
32   */
33  public final class NullaryProcedureNullaryFunction<T> implements NullaryFunction<T> {
34      /** The {@link NullaryProcedure NullaryProcedure} I'm wrapping. */
35      private final NullaryProcedure procedure;
36  
37      /**
38       * Create a new NullaryProcedureNullaryFunction.
39       * @param procedure to adapt
40       */
41      public NullaryProcedureNullaryFunction(NullaryProcedure procedure) {
42          this.procedure = Validate.notNull(procedure, "NullaryProcedure argument was null");
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      public T evaluate() {
49          procedure.run();
50          return null;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public boolean equals(Object obj) {
58          if (obj == this) {
59              return true;
60          }
61          if (!(obj instanceof NullaryProcedureNullaryFunction<?>)) {
62              return false;
63          }
64          NullaryProcedureNullaryFunction<?> that = (NullaryProcedureNullaryFunction<?>) obj;
65          return this.procedure.equals(that.procedure);
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public int hashCode() {
73          int hash = "NullaryProcedureNullaryFunction".hashCode();
74          hash ^= procedure.hashCode();
75          return hash;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String toString() {
83          return "NullaryProcedureNullaryFunction<" + procedure + ">";
84      }
85  
86      /**
87       * Adapt a NullaryProcedure as a NullaryFunction.
88       * @param <T> the returned value type.
89       * @param procedure to adapt
90       * @return NullaryProcedureNullaryFunction<T>
91       */
92      public static <T> NullaryProcedureNullaryFunction<T> adapt(NullaryProcedure procedure) {
93          return null == procedure ? null : new NullaryProcedureNullaryFunction<T>(procedure);
94      }
95  
96  }