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 47 48 public class ShiftRight<T extends Number > extends BinaryFunctor<T,Integer ,T> { 49 50 static final long serialVersionUID = 4910289709792083209L; 51 52 transient private IntegerArithmetic<T> _math; 55 56 private Class <T> _type; 58 59 67 public ShiftRight(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, Integer y) { 86 try { 87 return getMath().signedShiftRight(x, y); 88 } 89 catch (ClassCastException ex) { 90 String msg = "ClassCastException: Cannot compute shiftRight of {0}[{1}] and {2}[{3}]"; 91 String err = MessageFormat.format(msg, new Object []{ x.getClass(), x, y.getClass(), y}); 92 throw new EvaluationException(err, ex); 93 } 94 } 95 96 100 public void accept(net.sf.jga.fn.Visitor v) { 101 if (v instanceof ShiftRight.Visitor) 102 ((ShiftRight.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 Arithmetic 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 "ShiftRight"; 125 } 126 127 129 132 public interface Visitor extends net.sf.jga.fn.Visitor { 133 public void visit(ShiftRight host); 134 } 135 } 136 | Popular Tags |