1 33 package net.sf.jga.fn.adaptor; 34 35 import net.sf.jga.fn.UnaryFunctor; 36 37 47 48 public class ConditionalUnary<T,R> extends UnaryFunctor<T,R> { 49 50 static final long serialVersionUID = -8509676654602764401L; 51 52 private UnaryFunctor<T,Boolean > _test; 54 55 private UnaryFunctor<T,R> _then; 57 58 private UnaryFunctor<T,R> _else; 60 61 66 public ConditionalUnary(UnaryFunctor<T,Boolean > test, UnaryFunctor<T,R> trueFn, 67 UnaryFunctor<T,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 UnaryFunctor<T,Boolean > getCondition() { return _test; } 83 84 88 public UnaryFunctor<T,R> getTrueFunctor() { return _then; } 89 90 94 public UnaryFunctor<T,R> getFalseFunctor() { return _else; } 95 96 98 104 public R fn(T x) { 105 return _test.fn(x) ? _then.fn(x) : _else.fn(x); 106 } 107 108 112 public void accept(net.sf.jga.fn.Visitor v) { 113 if (v instanceof ConditionalUnary.Visitor) 114 ((ConditionalUnary.Visitor)v).visit(this); 115 else 116 v.visit(this); 117 } 118 119 121 public String toString() { 122 return "ConditionalUnary["+_test+"?"+_then+":"+_else+"]"; 123 } 124 125 127 130 public interface Visitor extends net.sf.jga.fn.Visitor { 131 public void visit(ConditionalUnary host); 132 } 133 } 134 | Popular Tags |