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 BitwiseOr<T extends Number > extends BinaryFunctor<T,T,T> { 48 49 static final long serialVersionUID = -213586968622137607L; 50 51 transient private IntegerArithmetic<T> _math; 53 54 private Class <T> _type; 56 57 65 public BitwiseOr(Class <T> c) { 66 _type = c; 67 getMath(); 68 } 69 70 73 public Class <T> getType() { 74 return _type; 75 } 76 77 79 83 public T fn(T x, T y) { 84 try { 85 return getMath().or(x, y); 86 } 87 catch (ClassCastException ex) { 88 String msg = "ClassCastException: Cannot compute {0}[{1}] | {2}[{3}]"; 89 String err = MessageFormat.format(msg, new Object []{ x.getClass(), x, y.getClass(), y}); 90 throw new EvaluationException(err, ex); 91 } 92 } 93 94 98 public void accept(net.sf.jga.fn.Visitor v) { 99 if (v instanceof BitwiseOr.Visitor) 100 ((BitwiseOr.Visitor)v).visit(this); 101 else 102 v.visit(this); 103 } 104 105 107 private IntegerArithmetic<T> getMath() { 108 if (_math == null) { 109 _math = ArithmeticFactory.getIntegralArithmetic(_type); 110 if (_math == null) { 111 String msg = "No implementation of IntegerArithmetic registered for {0}"; 112 throw new IllegalArgumentException (MessageFormat.format(msg, new Object []{_type})); 113 } 114 } 115 116 return _math; 117 } 118 119 121 public String toString() { 122 return "BitwiseOr"; 123 } 124 125 127 131 public interface Visitor extends net.sf.jga.fn.Visitor { 132 public void visit(BitwiseOr host); 133 } 134 135 } 136 | Popular Tags |