View Javadoc

    1   /*
    2    * Created on Mar 5, 2004
    3    *
    4    */
    5   package org.codehaus.groovy.runtime;
    6
    7   /***
    8    * Integer NumberMath operations
    9    *
    10   * @author Steve Goetze
    11   */
    12  public class IntegerMath extends NumberMath {
    13
    14  	protected static IntegerMath instance = new IntegerMath();
    15
    16  	private IntegerMath() {}
    17
    18  	protected Number absImpl(Number number) {
    19  		return new Integer(Math.abs(number.intValue()));
    20  	}
    21
    22  	protected Number addImpl(Number left, Number right) {
    23  		return new Integer(left.intValue() + right.intValue());
    24  	}
    25
    26  	protected Number subtractImpl(Number left, Number right) {
    27  		return new Integer(left.intValue() - right.intValue());
    28  	}
    29
    30  	protected Number multiplyImpl(Number left, Number right) {
    31  		return new Integer(left.intValue() * right.intValue());
    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  		int leftVal = left.intValue();
    40  		int rightVal = right.intValue();
    41  		return (leftVal<rightVal ? -1 : (leftVal==rightVal ? 0 : 1));
    42  	}
    43
    44      protected Number orImpl(Number left, Number right) {
    45          return new Integer(left.intValue() | right.intValue());
    46      }
    47
    48      protected Number andImpl(Number left, Number right) {
    49          return new Integer(left.intValue() & right.intValue());
    50      }
    51
    52  	protected Number intdivImpl(Number left, Number right) {
    53          return new Integer(left.intValue() / right.intValue());
    54  	}
    55
    56      protected Number modImpl(Number left, Number right) {
    57          return new Integer(left.intValue() % right.intValue());
    58      }
    59
    60      protected Number negateImpl(Number left) {
    61          return new Integer(-left.intValue());
    62      }
    63
    64  	protected Number leftShiftImpl(Number left, Number right) {
    65  		return new Integer(left.intValue() << right.intValue());
    66  	}
    67
    68  	protected Number rightShiftImpl(Number left, Number right) {
    69  		return new Integer(left.intValue() >> right.intValue());
    70  	}
    71
    72  	protected Number rightShiftUnsignedImpl(Number left, Number right) {
    73  		return new Integer(left.intValue() >>> right.intValue());
    74  	}
    75  }