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.BinaryProcedure;
20 import org.apache.commons.functor.UnaryProcedure;
21 import org.apache.commons.lang3.Validate;
22
23 /**
24 * Adapts a BinaryProcedure as a UnaryProcedure by sending the same argument to both sides of the BinaryProcedure.
25 * @param <A> the argument type.
26 * @version $Revision: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $
27 */
28 public final class BinaryProcedureUnaryProcedure<A> implements UnaryProcedure<A> {
29 /**
30 * The adapted procedure.
31 */
32 private final BinaryProcedure<? super A, ? super A> procedure;
33
34 /**
35 * Create a new BinaryProcedureUnaryProcedure.
36 * @param procedure to adapt
37 */
38 public BinaryProcedureUnaryProcedure(BinaryProcedure<? super A, ? super A> procedure) {
39 this.procedure = Validate.notNull(procedure, "BinaryProcedure argument was null");
40 }
41
42 /**
43 * {@inheritDoc}
44 */
45 public void run(A obj) {
46 procedure.run(obj, obj);
47 };
48
49 /**
50 * {@inheritDoc}
51 */
52 @Override
53 public boolean equals(Object obj) {
54 return obj == this || obj instanceof BinaryProcedureUnaryProcedure<?>
55 && equals((BinaryProcedureUnaryProcedure<?>) obj);
56 }
57
58 /**
59 * Learn whether another BinaryProcedureUnaryProcedure is equal to
60 * <code>this</code>.
61 *
62 * @param that BinaryProcedureUnaryProcedure to check
63 *
64 * @return whether equal
65 */
66 public boolean equals(BinaryProcedureUnaryProcedure<?> that) {
67 return that != null && that.procedure.equals(this.procedure);
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 @Override
74 public int hashCode() {
75 return ("BinaryProcedureUnaryProcedure".hashCode() << 2) | procedure.hashCode();
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public String toString() {
83 return "BinaryProcedureUnaryProcedure<" + procedure + ">";
84 }
85
86 /**
87 * Adapt a BinaryProcedure as a UnaryProcedure.
88 * @param <A> argument type
89 * @param procedure BinaryProcedure to adapt
90 * @return UnaryProcedure<A>
91 */
92 public static <A> UnaryProcedure<A> adapt(BinaryProcedure<? super A, ? super A> procedure) {
93 return null == procedure ? null : new BinaryProcedureUnaryProcedure<A>(procedure);
94 }
95
96 }