1 33 package net.sf.jga.fn; 34 35 import java.io.Serializable ; 36 import net.sf.jga.fn.adaptor.Bind1st; 37 import net.sf.jga.fn.adaptor.Bind2nd; 38 import net.sf.jga.fn.adaptor.ChainBinary; 39 import net.sf.jga.fn.adaptor.ComposeBinary; 40 import net.sf.jga.fn.adaptor.ComposeUnary; 41 import net.sf.jga.fn.adaptor.Distribute; 42 import net.sf.jga.fn.adaptor.Generate1st; 43 import net.sf.jga.fn.adaptor.Generate2nd; 44 import net.sf.jga.fn.adaptor.Identity; 45 46 55 56 abstract public class BinaryFunctor<T1,T2,R> extends Functor<R> implements Serializable ,Visitable { 57 60 abstract public R fn(T1 arg1, T2 arg2); 61 62 67 public UnaryFunctor<T2,R> bind1st (T1 value){ 68 return new Bind1st<T1,T2,R>(value, this); 69 } 70 71 76 public UnaryFunctor<T1,R> bind2nd (T2 value){ 77 return new Bind2nd<T1,T2,R>(value, this); 78 } 79 80 85 public Generator<R> bind (T1 value1, T2 value2) { 86 return bind1st(value1).bind(value2); 87 } 88 89 95 public <F> UnaryFunctor<F,R> compose (UnaryFunctor<F,T1> g, UnaryFunctor<F,T2> h) { 96 return new ComposeUnary<F,T1,T2,R>(g,h,this); 97 } 98 99 105 public <F1,F2> BinaryFunctor<F1,F2,R> 106 compose (BinaryFunctor<F1,F2,T1> g, BinaryFunctor<F1,F2,T2> h) 107 { 108 return new ComposeBinary<F1,F2,T1,T2,R>(g,h,this); 109 } 110 111 119 public <F1,F2> BinaryFunctor<F1,F2,R> distribute (UnaryFunctor<F1,T1> g, UnaryFunctor<F2,T2> h) 120 { 121 return new Distribute<F1,F2,T1,T2,R>(g,h,this); 122 } 123 124 129 130 public UnaryFunctor<T2,R> generate1st(Generator<T1> gen) { 131 return new Generate1st<T1,T2,R>(this, gen); 132 } 133 134 139 140 public UnaryFunctor<T1,R> generate2nd(Generator<T2> gen) { 141 return new Generate2nd<T1,T2,R>(this, gen); 142 } 143 144 149 150 public Generator<R> generate(Generator<T1> gen1, Generator<T2> gen2) { 151 return generate1st(gen1).generate(gen2); 152 } 153 154 158 public R eval(Object ... args) { 159 return fn((T1) args[0], (T2)args[1]); 164 } 165 166 170 173 public void accept(Visitor v) {} 174 } 175 | Popular Tags |