Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BinaryProcedureUnaryProcedure |
|
| 1.8571428571428572;1.857 |
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 | ||
22 | /** | |
23 | * Adapts a BinaryProcedure as a UnaryProcedure by sending the same argument to both sides of the BinaryProcedure. | |
24 | * @param <A> the argument type. | |
25 | * @version $Revision: 1160375 $ $Date: 2011-08-22 21:17:24 +0200 (Mon, 22 Aug 2011) $ | |
26 | * @author Matt Benson | |
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 | 18 | public BinaryProcedureUnaryProcedure(BinaryProcedure<? super A, ? super A> procedure) { |
39 | 18 | if (null == procedure) { |
40 | 0 | throw new IllegalArgumentException("BinaryProcedure argument was null"); |
41 | } | |
42 | 18 | this.procedure = procedure; |
43 | 18 | } |
44 | ||
45 | /** | |
46 | * {@inheritDoc} | |
47 | */ | |
48 | public void run(A obj) { | |
49 | 2 | procedure.run(obj, obj); |
50 | 2 | }; |
51 | ||
52 | /** | |
53 | * {@inheritDoc} | |
54 | */ | |
55 | @Override | |
56 | public boolean equals(Object obj) { | |
57 | 20 | return obj == this || obj instanceof BinaryProcedureUnaryProcedure<?> |
58 | && equals((BinaryProcedureUnaryProcedure<?>) obj); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Learn whether another BinaryProcedureUnaryProcedure is equal to | |
63 | * <code>this</code>. | |
64 | * | |
65 | * @param that BinaryProcedureUnaryProcedure to check | |
66 | * | |
67 | * @return whether equal | |
68 | */ | |
69 | public boolean equals(BinaryProcedureUnaryProcedure<?> that) { | |
70 | 12 | return that != null && that.procedure.equals(this.procedure); |
71 | } | |
72 | ||
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | @Override | |
77 | public int hashCode() { | |
78 | 20 | return ("BinaryProcedureUnaryProcedure".hashCode() << 2) | procedure.hashCode(); |
79 | } | |
80 | ||
81 | /** | |
82 | * {@inheritDoc} | |
83 | */ | |
84 | @Override | |
85 | public String toString() { | |
86 | 16 | return "BinaryProcedureUnaryProcedure<" + procedure + ">"; |
87 | } | |
88 | ||
89 | /** | |
90 | * Adapt a BinaryProcedure as a UnaryProcedure. | |
91 | * @param <A> argument type | |
92 | * @param procedure BinaryProcedure to adapt | |
93 | * @return UnaryProcedure<A> | |
94 | */ | |
95 | public static <A> UnaryProcedure<A> adapt(BinaryProcedure<? super A, ? super A> procedure) { | |
96 | 4 | return null == procedure ? null : new BinaryProcedureUnaryProcedure<A>(procedure); |
97 | } | |
98 | ||
99 | } |