public NumberValue times( NumberValue numberValue ) { if ( numberValue instanceof IntegralValue ) { return times( numberValue.longValue() ); } else if ( numberValue instanceof FloatingPointValue ) { return times( numberValue.doubleValue() ); } else { throw new IllegalArgumentException( "Cannot multiply with " + numberValue ); } }
public static AnyValue multiply( AnyValue lhs, AnyValue rhs ) { if ( lhs instanceof NumberValue && rhs instanceof NumberValue ) { return ((NumberValue) lhs).times( (NumberValue) rhs ); } // Temporal values if ( lhs instanceof DurationValue ) { if ( rhs instanceof NumberValue ) { return ((DurationValue) lhs).mul( (NumberValue) rhs ); } } if ( rhs instanceof DurationValue ) { if ( lhs instanceof NumberValue ) { return ((DurationValue) rhs).mul( (NumberValue) lhs ); } } throw new CypherTypeException( String.format( "Cannot multiply `%s` and `%s`", lhs.getTypeName(), rhs.getTypeName() ), null ); }
@Test void shouldMultiplySimpleIntegers() { NumberValue[] integers = new NumberValue[]{byteValue( (byte) 42 ), shortValue( (short) 42 ), intValue( 42 ), longValue( 42 )}; for ( NumberValue a : integers ) { for ( NumberValue b : integers ) { assertThat( a.times( b ), equalTo( longValue( 42 * 42 ) ) ); assertThat( b.times( a ), equalTo( longValue( 42 * 42 ) ) ); } } }
@Test void shouldMultiplySimpleFloats() { NumberValue[] integers = new NumberValue[]{byteValue( (byte) 42 ), shortValue( (short) 42 ), intValue( 42 ), longValue( 42 )}; NumberValue[] floats = new NumberValue[]{floatValue( 42 ), doubleValue( 42 )}; for ( NumberValue a : integers ) { for ( NumberValue b : floats ) { assertThat( a.times( b ), equalTo( doubleValue( 42 * 42 ) ) ); assertThat( b.times( a ), equalTo( doubleValue( 42 * 42 ) ) ); } } }
return ((NumberValue) lhs).times( (NumberValue) rhs );
public NumberValue times( NumberValue numberValue ) { if ( numberValue instanceof IntegralValue ) { return times( numberValue.longValue() ); } else if ( numberValue instanceof FloatingPointValue ) { return times( numberValue.doubleValue() ); } else { throw new IllegalArgumentException( "Cannot multiply with " + numberValue ); } }
public static AnyValue multiply( AnyValue lhs, AnyValue rhs ) { if ( lhs instanceof NumberValue && rhs instanceof NumberValue ) { return ((NumberValue) lhs).times( (NumberValue) rhs ); } // Temporal values if ( lhs instanceof DurationValue ) { if ( rhs instanceof NumberValue ) { return ((DurationValue) lhs).mul( (NumberValue) rhs ); } } if ( rhs instanceof DurationValue ) { if ( lhs instanceof NumberValue ) { return ((DurationValue) rhs).mul( (NumberValue) lhs ); } } throw new CypherTypeException( String.format( "Cannot multiply `%s` and `%s`", lhs.getTypeName(), rhs.getTypeName() ), null ); }
return ((NumberValue) lhs).times( (NumberValue) rhs );