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