1 33 package net.sf.jga.fn.arithmetic; 34 35 import java.text.MessageFormat ; 36 import net.sf.jga.fn.BinaryFunctor; 37 import net.sf.jga.fn.EvaluationException; 38 39 46 47 public class BitwiseXor<T extends Number > extends BinaryFunctor<T,T,T> { 48 49 static final long serialVersionUID = -2706252341597908781L; 50 51 transient private IntegerArithmetic<T> _math; 54 55 private Class <T> _type; 57 58 66 public BitwiseXor(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, T y) { 85 try { 86 return getMath().xor(x, y); 87 } 88 catch (ClassCastException ex) { 89 String msg = "ClassCastException: Cannot compute {0}[{1}] | {2}[{3}]"; 90 String err = MessageFormat.format(msg, new Object []{ x.getClass(), x, y.getClass(), y}); 91 throw new EvaluationException(err, ex); 92 } 93 } 94 95 99 public void accept(net.sf.jga.fn.Visitor v) { 100 if (v instanceof BitwiseXor.Visitor) 101 ((BitwiseXor.Visitor)v).visit(this); 102 else 103 v.visit(this); 104 } 105 106 108 private IntegerArithmetic<T> getMath() { 109 if (_math == null) { 110 _math = ArithmeticFactory.getIntegralArithmetic(_type); 111 if (_math == null) { 112 String msg = "No implementation of IntegerArithmetic registered for {0}"; 113 throw new IllegalArgumentException (MessageFormat.format(msg, new Object []{_type})); 114 } 115 } 116 117 return _math; 118 } 119 120 122 public String toString() { 123 return "BitwiseXor"; 124 } 125 126 128 132 public interface Visitor extends net.sf.jga.fn.Visitor { 133 public void visit(BitwiseXor host); 134 } 135 136 } 137 | Popular Tags |