| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.functor.core.composite; |
| 18 | |
|
| 19 | |
import java.io.Serializable; |
| 20 | |
|
| 21 | |
import org.apache.commons.functor.BinaryFunction; |
| 22 | |
import org.apache.commons.functor.BinaryProcedure; |
| 23 | |
import org.apache.commons.functor.UnaryProcedure; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public class TransformedBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable { |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
private static final long serialVersionUID = -7559182250073101798L; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | private static final class Helper<X, L, R> implements BinaryProcedure<L, R>, Serializable { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private static final long serialVersionUID = -4229496007094829301L; |
| 45 | |
private BinaryFunction<? super L, ? super R, ? extends X> function; |
| 46 | |
private UnaryProcedure<? super X> procedure; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private Helper(BinaryFunction<? super L, ? super R, ? extends X> function, |
| 54 | 0 | UnaryProcedure<? super X> procedure) { |
| 55 | 0 | this.function = function; |
| 56 | 0 | this.procedure = procedure; |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public void run(L left, R right) { |
| 63 | 0 | procedure.run(function.evaluate(left, right)); |
| 64 | 0 | } |
| 65 | |
} |
| 66 | |
|
| 67 | |
private final Helper<?, L, R> helper; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public <X> TransformedBinaryProcedure(BinaryFunction<? super L, ? super R, ? extends X> function, |
| 76 | 0 | UnaryProcedure<? super X> procedure) { |
| 77 | 0 | this.helper = new Helper<X, L, R>(function, procedure); |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public final void run(L left, R right) { |
| 84 | 0 | helper.run(left, right); |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public final boolean equals(Object obj) { |
| 92 | 0 | return obj == this || obj instanceof TransformedBinaryProcedure<?, ?> |
| 93 | |
&& equals((TransformedBinaryProcedure<?, ?>) obj); |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public final boolean equals(TransformedBinaryProcedure<?, ?> that) { |
| 102 | 0 | return that != null && that.helper.function.equals(this.helper.function) |
| 103 | |
&& that.helper.procedure.equals(this.helper.procedure); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public int hashCode() { |
| 111 | 0 | int result = "TransformedBinaryProcedure".hashCode(); |
| 112 | 0 | result <<= 2; |
| 113 | 0 | result |= helper.procedure.hashCode(); |
| 114 | 0 | result <<= 2; |
| 115 | 0 | result |= helper.function.hashCode(); |
| 116 | 0 | return result; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
@Override |
| 123 | |
public String toString() { |
| 124 | 0 | return "TransformedBinaryProcedure<" + helper.function + "; " + helper.procedure + ">"; |
| 125 | |
} |
| 126 | |
} |