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