FloatValue extends NumberValueAdapter<Double> { @Override public int asInt() { int intVal = (int) val; if ((double) intVal != val) { throw new LossyCoercion( type().name(), "Java int" ); } return intVal; } FloatValue( double val ); @Override Type type(); @Override Double asNumber(); @Override long asLong(); @Override int asInt(); @Override double asDouble(); @Override float asFloat(); @Override boolean equals( Object o ); @Override int hashCode(); @Override String toString();  }
@Test void shouldThrowIfLargerThanIntegerMax() { FloatValue value1 = new FloatValue( Integer.MAX_VALUE ); FloatValue value2 = new FloatValue( Integer.MAX_VALUE + 1L); assertThat(value1.asInt(), equalTo(Integer.MAX_VALUE)); assertThrows( LossyCoercion.class, value2::asInt ); }
@Test void shouldThrowIfSmallerThanIntegerMin() { FloatValue value1 = new FloatValue( Integer.MIN_VALUE ); FloatValue value2 = new FloatValue( Integer.MIN_VALUE - 1L ); assertThat(value1.asInt(), equalTo(Integer.MIN_VALUE)); assertThrows( LossyCoercion.class, value2::asInt ); }