View Javadoc

    1   /*
    2    * Created on Mar 5, 2004
    3    *
    4    */
    5   package org.codehaus.groovy.runtime.typehandling;
    6
    7   /***
    8    * Long NumberMath operations
    9    *
    10   * @author Steve Goetze
    11   */
    12  public class LongMath extends NumberMath {
    13
    14  	protected static LongMath instance = new LongMath();
    15
    16  	private LongMath() {}
    17
    18  	protected Number absImpl(Number number) {
    19  		return new Long(Math.abs(number.longValue()));
    20  	}
    21
    22  	protected Number addImpl(Number left, Number right) {
    23  		return new Long(left.longValue() + right.longValue());
    24  	}
    25
    26  	protected Number subtractImpl(Number left, Number right) {
    27  		return new Long(left.longValue() - right.longValue());
    28  	}
    29
    30  	protected Number multiplyImpl(Number left, Number right) {
    31  		return new Long(left.longValue() * right.longValue());
    32  	}
    33
    34  	protected Number divideImpl(Number left, Number right) {
    35  		return BigDecimalMath.instance.divideImpl(left, right);
    36  	}
    37
    38  	protected int compareToImpl(Number left, Number right) {
    39  		long leftVal = left.longValue();
    40  		long rightVal = right.longValue();
    41  		return (leftVal<rightVal ? -1 : (leftVal==rightVal ? 0 : 1));
    42  	}
    43
    44  	protected Number intdivImpl(Number left, Number right) {
    45          return new Long(left.longValue() / right.longValue());
    46  	}
    47
    48      protected Number modImpl(Number left, Number right) {
    49          return new Long(left.longValue() % right.longValue());
    50      }
    51
    52      protected Number negateImpl(Number left) {
    53          return new Long(-left.longValue());
    54      }
    55
    56      protected Number bitNegateImpl(Number left) {
    57          return new Long(~left.longValue());
    58      }
    59
    60      protected Number orImpl(Number left, Number right) {
    61          return new Long(left.longValue() | right.longValue());
    62      }
    63
    64      protected Number andImpl(Number left, Number right) {
    65          return new Long(left.longValue() & right.longValue());
    66      }
    67
    68      protected Number xorImpl(Number left, Number right) {
    69          return new Long(left.longValue() ^ right.longValue());
    70      }
    71
    72      protected Number leftShiftImpl(Number left, Number right) {
    73          return new Long(left.longValue() << right.longValue());
    74      }
    75
    76      protected Number rightShiftImpl(Number left, Number right) {
    77          return new Long(left.longValue() >> right.longValue());
    78      }
    79
    80      protected Number rightShiftUnsignedImpl(Number left, Number right) {
    81          return new Long(left.longValue() >>> right.longValue());
    82      }
    83
    84      protected Number bitAndImpl(Number left, Number right) {
    85          return new Long(left.longValue() & right.longValue());
    86      }
    87  }