| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.functor.core.algorithm; |
| 18 | |
|
| 19 | |
import java.io.Serializable; |
| 20 | |
import java.util.Stack; |
| 21 | |
|
| 22 | |
import org.apache.commons.functor.BinaryFunction; |
| 23 | |
import org.apache.commons.functor.UnaryFunction; |
| 24 | |
import org.apache.commons.functor.UnaryProcedure; |
| 25 | |
import org.apache.commons.functor.generator.Generator; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class FoldRight<T> implements UnaryFunction<Generator<T>, T>, BinaryFunction<Generator<T>, T, T>, Serializable { |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private static final long serialVersionUID = 4671387086700391506L; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private static class FoldRightHelper<T> implements UnaryProcedure<T> { |
| 45 | 4 | private final Stack<T> stk = new Stack<T>(); |
| 46 | |
private final BinaryFunction<? super T, ? super T, ? extends T> function; |
| 47 | |
private final T seed; |
| 48 | |
private final boolean hasSeed; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public FoldRightHelper(BinaryFunction<? super T, ? super T, ? extends T> function) { |
| 54 | 2 | this(null, function); |
| 55 | 2 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 4 | FoldRightHelper(T seed, BinaryFunction<? super T, ? super T, ? extends T> function) { |
| 62 | 4 | this.seed = seed; |
| 63 | 4 | hasSeed = seed != null ? true : false; |
| 64 | 4 | this.function = function; |
| 65 | 4 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void run(T obj) { |
| 71 | 40 | stk.push(obj); |
| 72 | 40 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
T getResult() { |
| 80 | 4 | T right = seed; |
| 81 | 4 | if (!hasSeed) { |
| 82 | 2 | if (stk.isEmpty()) { |
| 83 | 0 | return null; |
| 84 | |
} |
| 85 | 2 | right = stk.pop(); |
| 86 | |
} |
| 87 | 42 | while (!stk.isEmpty()) { |
| 88 | 38 | right = function.evaluate(stk.pop(), right); |
| 89 | |
} |
| 90 | 4 | return right; |
| 91 | |
} |
| 92 | |
|
| 93 | |
} |
| 94 | |
|
| 95 | |
private final BinaryFunction<? super T, ? super T, ? extends T> function; |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 2 | public FoldRight(BinaryFunction<? super T, ? super T, ? extends T> function) { |
| 102 | 2 | this.function = function; |
| 103 | 2 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public final T evaluate(Generator<T> obj) { |
| 110 | 2 | FoldRightHelper<T> helper = new FoldRightHelper<T>(function); |
| 111 | 2 | obj.run(helper); |
| 112 | 2 | return helper.getResult(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public final T evaluate(Generator<T> left, T right) { |
| 121 | 2 | FoldRightHelper<T> helper = new FoldRightHelper<T>(right, function); |
| 122 | 2 | left.run(helper); |
| 123 | 2 | return helper.getResult(); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public final boolean equals(Object obj) { |
| 130 | 0 | if (obj == this) { |
| 131 | 0 | return true; |
| 132 | |
} |
| 133 | 0 | if (!(obj instanceof FoldRight<?>)) { |
| 134 | 0 | return false; |
| 135 | |
} |
| 136 | 0 | return ((FoldRight<?>) obj).function.equals(function); |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public int hashCode() { |
| 143 | 0 | return "FoldRight".hashCode() << 2 ^ function.hashCode(); |
| 144 | |
} |
| 145 | |
|
| 146 | |
} |