1 33 package net.sf.jga.fn.adaptor; 34 35 import net.sf.jga.fn.BinaryFunctor; 36 37 47 48 public class ConditionalBinary<T1,T2,R> extends BinaryFunctor<T1,T2,R> { 49 50 static final long serialVersionUID = -2682605534388798188L; 51 52 private BinaryFunctor<T1,T2,Boolean > _test; 54 55 private BinaryFunctor<T1,T2,R> _then; 57 58 private BinaryFunctor<T1,T2,R> _else; 60 61 66 public ConditionalBinary(BinaryFunctor<T1,T2,Boolean > test, BinaryFunctor<T1,T2,R> trueFn, 67 BinaryFunctor<T1,T2,R> falseFn) 68 { 69 if (test == null || trueFn == null || falseFn == null) { 70 throw new IllegalArgumentException ("Three functors are required"); 71 } 72 73 _test = test; 74 _then = trueFn; 75 _else = falseFn; 76 } 77 78 82 public BinaryFunctor<T1,T2,Boolean > getCondition() { return _test; } 83 84 88 public BinaryFunctor<T1,T2,R> getTrueFunctor() { return _then; } 89 90 94 public BinaryFunctor<T1,T2,R> getFalseFunctor() { return _else; } 95 96 98 104 public R fn(T1 x, T2 y) { 105 return _test.fn(x,y) ? _then.fn(x,y) : _else.fn(x,y); 106 } 107 108 112 public void accept(net.sf.jga.fn.Visitor v) { 113 if (v instanceof ConditionalBinary.Visitor) 114 ((ConditionalBinary.Visitor)v).visit(this); 115 else 116 v.visit(this); 117 } 118 119 121 public String toString() { 122 return "ConditionalBinary["+_test+"?"+_then+":"+_else+"]"; 123 } 124 125 127 130 public interface Visitor extends net.sf.jga.fn.Visitor { 131 public void visit(ConditionalBinary host); 132 } 133 } 134 | Popular Tags |