1 33 package net.sf.jga.fn.arithmetic; 34 35 import java.text.MessageFormat ; 36 import net.sf.jga.fn.UnaryFunctor; 37 import net.sf.jga.fn.EvaluationException; 38 39 46 47 public class BitwiseNot<T extends Number > extends UnaryFunctor<T,T> { 48 49 static final long serialVersionUID = 6788747054561165216L; 50 51 54 transient private IntegerArithmetic<T> _math; 55 56 private Class <T> _type; 58 59 67 public BitwiseNot(Class <T> c) { 68 _type = c; 69 getMath(); 70 } 71 72 75 public Class <T> getType() { 76 return _type; 77 } 78 79 81 85 public T fn(T x) { 86 try { 87 return getMath().not(x); 88 } 89 catch (ClassCastException ex) { 90 String msg = "ClassCastException: Cannot compute ~{0}[{1}]"; 91 String err = MessageFormat.format(msg, new Object []{ x.getClass(), x}); 92 throw new EvaluationException(err, ex); 93 } 94 } 95 96 100 public void accept(net.sf.jga.fn.Visitor v) { 101 if (v instanceof BitwiseNot.Visitor) 102 ((BitwiseNot.Visitor)v).visit(this); 103 else 104 v.visit(this); 105 } 106 107 109 private IntegerArithmetic<T> getMath() { 110 if (_math == null) { 111 _math = ArithmeticFactory.getIntegralArithmetic(_type); 112 if (_math == null) { 113 String msg = "No implementation of IntegerArithmetic registered for {0}"; 114 throw new IllegalArgumentException (MessageFormat.format(msg, new Object []{_type})); 115 } 116 } 117 118 return _math; 119 } 120 121 123 public String toString() { 124 return "BitwiseNot"; 125 } 126 127 129 133 public interface Visitor extends net.sf.jga.fn.Visitor { 134 public void visit(BitwiseNot host); 135 } 136 } 137 | Popular Tags |