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 java.io.Serializable;
20
21 import org.apache.commons.functor.UnaryFunction;
22 import org.apache.commons.functor.UnaryProcedure;
23 import org.apache.commons.lang3.Validate;
24
25 /**
26 * Adapts a
27 * {@link UnaryProcedure UnaryProcedure}
28 * to the
29 * {@link UnaryFunction UnaryFunction} interface
30 * by always returning <code>null</code>.
31 * <p/>
32 * Note that although this class implements
33 * {@link Serializable}, a given instance will
34 * only be truly <code>Serializable</code> if the
35 * underlying procedure is. Attempts to serialize
36 * an instance whose delegate is not
37 * <code>Serializable</code> will result in an exception.
38 *
39 * @param <A> the argument type.
40 * @param <T> the returned value type.
41 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
42 */
43 public final class UnaryProcedureUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
44 /**
45 * serialVersionUID declaration.
46 */
47 private static final long serialVersionUID = 6153848695167906659L;
48 /** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */
49 private final UnaryProcedure<? super A> procedure;
50
51 /**
52 * Create a new UnaryProcedureUnaryFunction.
53 * @param procedure to adapt
54 */
55 public UnaryProcedureUnaryFunction(UnaryProcedure<? super A> procedure) {
56 this.procedure = Validate.notNull(procedure, "UnaryProcedure argument was null");
57 }
58
59 /**
60 * {@inheritDoc}
61 */
62 public T evaluate(A obj) {
63 procedure.run(obj);
64 return null;
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 public boolean equals(Object that) {
72 return that == this || (that instanceof UnaryProcedureUnaryFunction<?, ?>
73 && equals((UnaryProcedureUnaryFunction<?, ?>) that));
74 }
75
76 /**
77 * Learn whether a given UnaryProcedureUnaryFunction is equal to this.
78 * @param that UnaryProcedureUnaryFunction to test
79 * @return boolean
80 */
81 public boolean equals(UnaryProcedureUnaryFunction<?, ?> that) {
82 return null != that && procedure.equals(that.procedure);
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public int hashCode() {
90 int hash = "UnaryProcedureUnaryFunction".hashCode();
91 hash ^= procedure.hashCode();
92 return hash;
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public String toString() {
100 return "UnaryProcedureUnaryFunction<" + procedure + ">";
101 }
102
103 /**
104 * Adapt a UnaryProcedure to the UnaryFunction interface.
105 * @param <A> the argument type.
106 * @param <T> the returned value type.
107 * @param procedure to adapt
108 * @return UnaryProcedureUnaryFunction
109 */
110 public static <A, T> UnaryProcedureUnaryFunction<A, T> adapt(UnaryProcedure<? super A> procedure) {
111 return null == procedure ? null : new UnaryProcedureUnaryFunction<A, T>(procedure);
112 }
113
114 }