/** * Creates a new value update that decrements a value. Note that the data type must be a numeric * type. * * @param decrement the number to decrement by * @return a ValueUpdate specifying the decrement * @throws UnsupportedOperationException if the data type is non-numeric */ public static ValueUpdate createDecrement(Number decrement) { return new ArithmeticValueUpdate(ArithmeticValueUpdate.Operator.SUB, decrement); }
@Override public void write(ArithmeticValueUpdate update) { putInt(null, update.getOperator().id); putDouble(null, update.getOperand().doubleValue()); }
@Override public void write(ArithmeticValueUpdate update) { final ArithmeticValueUpdate.Operator operator = update.getOperator(); final String operationKey; switch (operator) { case ADD: operationKey = "increment"; break; case DIV: operationKey = "divide"; break; case MUL: operationKey = "multiply"; break; case SUB: operationKey = "decrement"; break; default: throw new RuntimeException(String.format("Unrecognized arithmetic operator '%s'", operator.name)); } wrapIOException(() -> generator.writeFieldName(operationKey)); update.getValue().serialize(this); }
@Override public FieldValue applyTo(FieldValue oldValue) { if (oldValue instanceof NumericFieldValue) { Number number = (Number) oldValue.getWrappedValue(); oldValue.assign(calculate(number)); } else { throw new IllegalStateException("Cannot use arithmetic value update on non-numeric datatype "+oldValue.getClass().getName()); } return oldValue; }
@Override public void write(ArithmeticValueUpdate update) { putInt(null, update.getOperator().id); putDouble(null, update.getOperand().doubleValue()); }
if (((ArithmeticValueUpdate)upd).getOperator() == ArithmeticValueUpdate.Operator.DIV && ((ArithmeticValueUpdate)upd).getOperand().doubleValue() == 0) { throw new IllegalArgumentException("Division by zero.");
/** * Creates a new value update that divides a value. Note that the data type must be a numeric * type. * * @param divisor the number to divide by * @return a ValueUpdate specifying the division * @throws UnsupportedOperationException if the data type is non-numeric */ public static ValueUpdate createDivide(Number divisor) { return new ArithmeticValueUpdate(ArithmeticValueUpdate.Operator.DIV, divisor); }
/** * Creates a new value update that increments a value. Note that the data type must be a numeric * type. * * @param increment the number to increment by * @return a ValueUpdate specifying the increment * @throws UnsupportedOperationException if the data type is non-numeric */ public static ValueUpdate createIncrement(Number increment) { return new ArithmeticValueUpdate(ArithmeticValueUpdate.Operator.ADD, increment); }
/** * Creates a new value update that multiplies a value. Note that the data type must be a numeric * type. * * @param factor the number to multiply by * @return a ValueUpdate specifying the multiplication * @throws UnsupportedOperationException if the data type is non-numeric */ public static ValueUpdate createMultiply(Number factor) { return new ArithmeticValueUpdate(ArithmeticValueUpdate.Operator.MUL, factor); }
ArithmeticValueUpdate.Operator operator = ArithmeticValueUpdate.Operator.getID(opId); double operand = getDouble(null); return new ArithmeticValueUpdate(operator, operand); case ASSIGN:
ArithmeticValueUpdate.Operator operator = ArithmeticValueUpdate.Operator.getID(opId); double operand = getDouble(null); return new ArithmeticValueUpdate(operator, operand); case ASSIGN: