View Javadoc

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