1 33 package net.sf.jga.fn.arithmetic; 34 35 import java.text.MessageFormat ; 36 import net.sf.jga.fn.EvaluationException; 37 import net.sf.jga.fn.UnaryFunctor; 38 39 46 47 public class Negate<T extends Number > extends UnaryFunctor<T,T> { 48 49 static final long serialVersionUID = -7631446216663885944L; 50 51 transient private Arithmetic<T> _math; 54 55 private Class <T> _type; 57 58 66 public Negate(Class <T> c) { 67 _type = c; 68 getMath(); 69 } 70 71 74 public Class <T> getType() { 75 return _type; 76 } 77 78 80 84 public T fn(T x) { 85 return getMath().negate(x); 86 } 87 88 92 public void accept(net.sf.jga.fn.Visitor v) { 93 if (v instanceof Negate.Visitor) 94 ((Negate.Visitor)v).visit(this); 95 else 96 v.visit(this); 97 } 98 99 101 private Arithmetic<T> getMath() { 102 if (_math == null) { 103 _math = ArithmeticFactory.getArithmetic(_type); 104 if (_math == null) { 105 String msg = "No implementation of Arithmetic registered for {0}"; 106 throw new IllegalArgumentException (MessageFormat.format(msg, new Object []{_type})); 107 } 108 } 109 110 return _math; 111 } 112 113 115 public String toString() { 116 return "Negate"; 117 } 118 119 121 124 public interface Visitor extends net.sf.jga.fn.Visitor { 125 public void visit(Negate host); 126 } 127 } 128 | Popular Tags |