@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 1477363453: // decimalPlaces return ((HalfUpRounding) bean).getDecimalPlaces(); case -1653751294: // fraction return ((HalfUpRounding) bean).getFraction(); } return super.propertyGet(bean, propertyName, quiet); }
/** * Obtains an instance that rounds to the specified number of decimal places. * <p> * This returns a convention that rounds to the specified number of decimal places. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * * @param decimalPlaces the number of decimal places to round to, from 0 to 255 inclusive * @return the rounding convention * @throws IllegalArgumentException if the decimal places is invalid */ public static Rounding ofDecimalPlaces(int decimalPlaces) { return HalfUpRounding.ofDecimalPlaces(decimalPlaces); }
/** * Obtains an instance from the number of decimal places and fraction. * <p> * This returns a convention that rounds to a fraction of the specified number of decimal places. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * <p> * For example, to round to the nearest 1/32nd of the 4th decimal place, call * this method with the arguments 4 and 32. * * @param decimalPlaces the number of decimal places to round to, from 0 to 255 inclusive * @param fraction the fraction of the last decimal place, such as 32 for 1/32, from 0 to 255 inclusive * @return the rounding convention * @throws IllegalArgumentException if the decimal places or fraction is invalid */ public static Rounding ofFractionalDecimalPlaces(int decimalPlaces, int fraction) { return HalfUpRounding.ofFractionalDecimalPlaces(decimalPlaces, fraction); }
public void test_ofDecimalPlaces() { HalfUpRounding test = HalfUpRounding.ofDecimalPlaces(4); assertEquals(test.getDecimalPlaces(), 4); assertEquals(test.getFraction(), 0); assertEquals(test.toString(), "Round to 4dp"); assertEquals(Rounding.ofDecimalPlaces(4), test); }
public void test_ofFractionalDecimalPlaces() { HalfUpRounding test = HalfUpRounding.ofFractionalDecimalPlaces(4, 32); assertEquals(test.getDecimalPlaces(), 4); assertEquals(test.getFraction(), 32); assertEquals(test.toString(), "Round to 1/32 of 4dp"); assertEquals(Rounding.ofFractionalDecimalPlaces(4, 32), test); }
public void test_builder() { HalfUpRounding test = HalfUpRounding.meta().builder() .set(HalfUpRounding.meta().decimalPlaces(), 4) .set(HalfUpRounding.meta().fraction(), 1) .build(); assertEquals(test.getDecimalPlaces(), 4); assertEquals(test.getFraction(), 0); assertEquals(test.toString(), "Round to 4dp"); }
@DataProvider(name = "round") public static Object[][] data_round() { return new Object[][] { {HalfUpRounding.ofDecimalPlaces(2), 12.3449, 12.34}, {HalfUpRounding.ofDecimalPlaces(2), 12.3450, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3451, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3500, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3549, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3550, 12.36}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3424, 12.340}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3425, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3426, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3449, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3450, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3451, 12.345}, }; }
@Override public HalfUpRounding build() { return new HalfUpRounding( decimalPlaces, fraction); }
@Test(dataProvider = "round") public void round_double_NONE(HalfUpRounding rounding, double input, double expected) { assertEquals(rounding.round(input), expected); }
public void test_builder_invalid() { assertThrowsIllegalArg(() -> HalfUpRounding.meta().builder() .set(HalfUpRounding.meta().decimalPlaces(), -1) .build()); assertThrowsIllegalArg(() -> HalfUpRounding.meta().builder() .set(HalfUpRounding.meta().decimalPlaces(), 257) .build()); assertThrowsIllegalArg(() -> HalfUpRounding.meta().builder() .set(HalfUpRounding.meta().decimalPlaces(), 4) .set(HalfUpRounding.meta().fraction(), -1) .build()); assertThrowsIllegalArg(() -> HalfUpRounding.meta().builder() .set(HalfUpRounding.meta().decimalPlaces(), 4) .set(HalfUpRounding.meta().fraction(), 257) .build()); }
public void test_ofDecimalPlaces_big() { HalfUpRounding test = HalfUpRounding.ofDecimalPlaces(40); assertEquals(test.getDecimalPlaces(), 40); assertEquals(test.getFraction(), 0); assertEquals(test.toString(), "Round to 40dp"); assertEquals(Rounding.ofDecimalPlaces(40), test); }
public void coverage() { HalfUpRounding test = HalfUpRounding.ofDecimalPlaces(4); coverImmutableBean(test); HalfUpRounding test2 = HalfUpRounding.ofFractionalDecimalPlaces(4, 32); coverBeanEquals(test, test2); }
private Object readResolve() throws ObjectStreamException { return new HalfUpRounding(decimalPlaces, fraction); }
@Test(dataProvider = "round") public void round_BigDecimal_NONE(HalfUpRounding rounding, double input, double expected) { assertEquals(rounding.round(BigDecimal.valueOf(input)), BigDecimal.valueOf(expected)); }
/** * Obtains an instance that rounds to the number of minor units in the currency. * <p> * This returns a convention that rounds for the specified currency. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * * @param currency the currency * @return the rounding convention */ public static Rounding of(Currency currency) { return HalfUpRounding.ofDecimalPlaces(currency.getMinorUnitDigits()); }
public void test_ofFractionalDecimalPlaces_invalid() { assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(-1, 0)); assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(257, 0)); assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(0, -1)); assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(0, 257)); }
/** * Obtains an instance from the number of decimal places and fraction. * <p> * This returns a convention that rounds to a fraction of the specified number of decimal places. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * <p> * For example, to round to the nearest 1/32nd of the 4th decimal place, call * this method with the arguments 4 and 32. * * @param decimalPlaces the number of decimal places to round to, from 0 to 255 inclusive * @param fraction the fraction of the last decimal place, such as 32 for 1/32, from 0 to 256 inclusive * @return the rounding convention * @throws IllegalArgumentException if the decimal places or fraction is invalid */ public static HalfUpRounding ofFractionalDecimalPlaces(int decimalPlaces, int fraction) { return new HalfUpRounding(decimalPlaces, fraction); }
public void test_ofDecimalPlaces_invalid() { assertThrowsIllegalArg(() -> HalfUpRounding.ofDecimalPlaces(-1)); assertThrowsIllegalArg(() -> HalfUpRounding.ofDecimalPlaces(257)); }
/** * Obtains an instance that rounds to the specified number of decimal places. * <p> * This returns a convention that rounds to the specified number of decimal places. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * * @param decimalPlaces the number of decimal places to round to, from 0 to 255 inclusive * @return the rounding convention * @throws IllegalArgumentException if the decimal places is invalid */ public static HalfUpRounding ofDecimalPlaces(int decimalPlaces) { if (decimalPlaces >= 0 && decimalPlaces < 16) { return CACHE[decimalPlaces]; } return new HalfUpRounding(decimalPlaces, 1); }
public void test_serialization() { HalfUpRounding test = HalfUpRounding.ofDecimalPlaces(4); assertSerialization(test); }