@Override public @NonNull IntegerValue divInteger(@NonNull IntegerValue right) { if (right.bigIntegerValue().signum() == 0) { throw new InvalidValueException("div zero"); } @SuppressWarnings("null") @NonNull BigInteger result = bigIntegerValue().divide(right.bigIntegerValue()); return ValueUtil.integerValueOf(result); }
@Override public @NonNull IntegerValue addInteger(@NonNull IntegerValue right) { if (right instanceof IntIntegerValueImpl) { int thatValue = ((IntIntegerValueImpl) right).intValue(); int sum = value + thatValue; if (value >= 0) { if ((thatValue >= 0) && (sum >= 0)) { return ValueUtil.integerValueOf(sum); } } else { if ((thatValue <= 0) && (sum <= 0)) { return ValueUtil.integerValueOf(sum); } } return ValueUtil.integerValueOf((long) value + (long) thatValue); } else if (right instanceof LongIntegerValueImpl) { long thatValue = ((LongIntegerValueImpl) right).longValue(); return ValueUtil.integerValueOf(value + thatValue); } else { @SuppressWarnings("null") @NonNull BigInteger result = bigIntegerValue().add(right.bigIntegerValue()); return ValueUtil.integerValueOf(result); } }
@Override public @NonNull IntegerValue subtractInteger(@NonNull IntegerValue right) { if (right instanceof IntIntegerValueImpl) { int thatValue = ((IntIntegerValueImpl) right).intValue(); int diff = value - thatValue; if (value >= 0) { if ((thatValue <= 0) && (diff >= 0)) { return ValueUtil.integerValueOf(diff); } } else { if ((thatValue >= 0) && (diff <= 0)) { return ValueUtil.integerValueOf(diff); } } return ValueUtil.integerValueOf((long) value - (long) thatValue); } else if (right instanceof LongIntegerValueImpl) { long thatValue = ((LongIntegerValueImpl) right).longValue(); return ValueUtil.integerValueOf(value - thatValue); } else { @SuppressWarnings("null") @NonNull BigInteger result = bigIntegerValue().subtract(right.bigIntegerValue()); return ValueUtil.integerValueOf(result); } }
@Override public @NonNull IntegerValue modInteger(@NonNull IntegerValue right) { if (right.bigIntegerValue().signum() == 0) { throw new InvalidValueException("mod zero"); } if (right instanceof IntIntegerValueImpl) { int thatValue = ((IntIntegerValueImpl) right).intValue(); return ValueUtil.integerValueOf(value % thatValue); } else if (right instanceof LongIntegerValueImpl) { long thatValue = ((LongIntegerValueImpl) right).longValue(); return ValueUtil.integerValueOf(value % thatValue); } else { @SuppressWarnings("null") @NonNull BigInteger result = bigIntegerValue().remainder(right.bigIntegerValue()); return ValueUtil.integerValueOf(result); } }
@Override public @NonNull IntegerValue multiplyInteger(@NonNull IntegerValue right) { if (right instanceof IntIntegerValueImpl) { long thatValue = ((IntIntegerValueImpl) right).intValue(); return ValueUtil.integerValueOf(value * thatValue); } else { @SuppressWarnings("null") @NonNull BigInteger result = bigIntegerValue().multiply(right.bigIntegerValue()); return ValueUtil.integerValueOf(result); } }
@Override public @NonNull IntegerValue minInteger(@NonNull IntegerValue right) { if (right instanceof IntIntegerValueImpl) { int thatValue = ((IntIntegerValueImpl) right).intValue(); return value <= thatValue ? this : right; } else if (right instanceof LongIntegerValueImpl) { long thatValue = ((LongIntegerValueImpl) right).longValue(); return value <= thatValue ? this : right; } else { return bigIntegerValue().compareTo(right.bigIntegerValue()) <= 0 ? this : right; } }
@Override public int commutatedCompareToInteger(@NonNull IntegerValue o) { try { if (o instanceof IntIntegerValueImpl) { int thatValue = ((IntIntegerValueImpl) o).intValue(); return (value < thatValue ? -1 : (value == thatValue ? 0 : 1)); } else if (o instanceof LongIntegerValueImpl) { long thatValue = ((LongIntegerValueImpl) o).longValue(); return (value < thatValue ? -1 : (value == thatValue ? 0 : 1)); } else { return bigIntegerValue().compareTo(o.bigIntegerValue()); } } catch (InvalidValueException e) { return this.hashCode() - o.hashCode(); } }