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