Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BinaryFunctionBinaryProcedure |
|
| 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 java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.BinaryFunction; | |
22 | import org.apache.commons.functor.BinaryProcedure; | |
23 | ||
24 | /** | |
25 | * Adapts a {@link BinaryFunction BinaryFunction} | |
26 | * to the {@link BinaryProcedure BinaryProcedure} | |
27 | * interface by ignoring the value returned | |
28 | * by the function. | |
29 | * <p/> | |
30 | * Note that although this class implements | |
31 | * {@link Serializable}, a given instance will | |
32 | * only be truly <code>Serializable</code> if the | |
33 | * underlying function is. Attempts to serialize | |
34 | * an instance whose delegate is not | |
35 | * <code>Serializable</code> will result in an exception. | |
36 | * | |
37 | * @param <L> the left argument type. | |
38 | * @param <R> the right argument type. | |
39 | * @version $Revision: 1156760 $ $Date: 2011-08-11 21:34:07 +0200 (Thu, 11 Aug 2011) $ | |
40 | * @author Rodney Waldhoff | |
41 | */ | |
42 | public final class BinaryFunctionBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable { | |
43 | ||
44 | /** | |
45 | * serialVersionUID declaration. | |
46 | */ | |
47 | private static final long serialVersionUID = 4498276997127058865L; | |
48 | /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */ | |
49 | private final BinaryFunction<? super L, ? super R, ?> function; | |
50 | ||
51 | /** | |
52 | * Create an {@link BinaryProcedure BinaryProcedure} wrapping | |
53 | * the given {@link BinaryFunction BinaryFunction}. | |
54 | * @param function the {@link BinaryFunction BinaryFunction} to wrap | |
55 | */ | |
56 | 30 | public BinaryFunctionBinaryProcedure(BinaryFunction<? super L, ? super R, ?> function) { |
57 | 30 | this.function = function; |
58 | 30 | } |
59 | ||
60 | /** | |
61 | * {@link BinaryFunction#evaluate Evaluate} my function, but | |
62 | * ignore its returned value. | |
63 | * {@inheritDoc} | |
64 | */ | |
65 | public void run(L left, R right) { | |
66 | 14 | function.evaluate(left, right); |
67 | 14 | } |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | public boolean equals(Object that) { | |
73 | 28 | return that == this |
74 | || (that instanceof BinaryFunctionBinaryProcedure<?, ?> | |
75 | && equals((BinaryFunctionBinaryProcedure<?, ?>) that)); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Learn whether a given BinaryFunctionBinaryPredicate is equal to this. | |
80 | * @param that BinaryFunctionBinaryPredicate to compare | |
81 | * @return boolean | |
82 | */ | |
83 | public boolean equals(BinaryFunctionBinaryProcedure<?, ?> that) { | |
84 | 14 | return null != that && (null == function ? null == that.function : function.equals(that.function)); |
85 | } | |
86 | ||
87 | /** | |
88 | * {@inheritDoc} | |
89 | */ | |
90 | public int hashCode() { | |
91 | 30 | int hash = "BinaryFunctionBinaryProcedure".hashCode(); |
92 | 30 | if (null != function) { |
93 | 30 | hash ^= function.hashCode(); |
94 | } | |
95 | 30 | return hash; |
96 | } | |
97 | ||
98 | /** | |
99 | * {@inheritDoc} | |
100 | */ | |
101 | public String toString() { | |
102 | 22 | return "BinaryFunctionBinaryProcedure<" + function + ">"; |
103 | } | |
104 | ||
105 | /** | |
106 | * Adapt the given, possibly-<code>null</code>, | |
107 | * {@link BinaryFunction BinaryFunction} to the | |
108 | * {@link BinaryProcedure BinaryProcedure} interface. | |
109 | * When the given <code>BinaryFunction</code> is <code>null</code>, | |
110 | * returns <code>null</code>. | |
111 | * | |
112 | * @param <L> left type | |
113 | * @param <R> right type | |
114 | * @param function the possibly-<code>null</code> | |
115 | * {@link BinaryFunction BinaryFunction} to adapt | |
116 | * @return a <code>BinaryFunctionBinaryProcedure</code> wrapping the given | |
117 | * {@link BinaryFunction BinaryFunction}, or <code>null</code> | |
118 | * if the given <code>BinaryFunction</code> is <code>null</code> | |
119 | */ | |
120 | public static <L, R> BinaryFunctionBinaryProcedure<L, R> adapt(BinaryFunction<? super L, ? super R, ?> function) { | |
121 | 6 | return null == function ? null : new BinaryFunctionBinaryProcedure<L, R>(function); |
122 | } | |
123 | ||
124 | } |