Skip to content

Commit

Permalink
fixes #5: Include convenience methods: longValueExact, intValueExact,…
Browse files Browse the repository at this point in the history
… shortValueExact, byteValueExact
  • Loading branch information
Christoph Linder committed Aug 10, 2021
1 parent 159c407 commit a42672b
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,34 @@ public boolean isZero() {
return comparesTo(BigDecimal.ZERO);
}

/**
* See {@link BigDecimal#longValueExact()}.
*/
public long longValueExact() {
return getValue().longValueExact();
}

/**
* See {@link BigDecimal#intValueExact()}.
*/
public int intValueExact() {
return getValue().intValueExact();
}

/**
* See {@link BigDecimal#shortValueExact()}.
*/
public short shortValueExact() {
return getValue().shortValueExact();
}

/**
* See {@link BigDecimal#byteValueExact()}.
*/
public byte byteValueExact() {
return getValue().byteValueExact();
}

private static @Nullable BigDecimal mapValue(@Nullable AbstractFluentBigDecimal<?> input) {
if (input == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.NonNull;
import lombok.var;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

Expand Down Expand Up @@ -896,6 +897,95 @@ void TEN_returns_same_instance_every_time() {
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="ComparesTo">
@Nested
class ExactGetters {

@Nested
class longValueExact {
@Test
void gets_desired_value() {
long value = FIXTURE_CONFIG.of(123)
.longValueExact();

assertThat(value)
.isEqualTo(123);
}

@Test
void throws_if_rounding_necessary() {
assertThrowsArithmeticException(() -> FIXTURE_CONFIG.of(123.45).longValueExact());
}

}

@Nested
class intValueExact {
@Test
void gets_desired_value() {
int value = FIXTURE_CONFIG.of(123)
.intValueExact();

assertThat(value)
.isEqualTo(123);
}

@Test
void throws_if_rounding_necessary() {
assertThrowsArithmeticException(() -> FIXTURE_CONFIG.of(123.45).intValueExact());
}

}

@Nested
class shortValueExact {
@Test
void gets_desired_value() {
short value = FIXTURE_CONFIG.of(123)
.shortValueExact();

assertThat(value)
.isEqualTo(Short.valueOf((short) 123).shortValue());
}

@Test
void throws_if_rounding_necessary() {
assertThrowsArithmeticException(() -> FIXTURE_CONFIG.of(123.45).shortValueExact());
}

}

@Nested
class byteValueExact {
@Test
void gets_desired_value() {
byte value = FIXTURE_CONFIG.of(123)
.byteValueExact();

assertThat(value)
.isEqualTo((byte) 123);
}

@Test
void throws_if_rounding_necessary() {
assertThrowsArithmeticException(() -> FIXTURE_CONFIG.of(123.45).byteValueExact());
}

}

private void assertThrowsArithmeticException(Executable e) {
ArithmeticException ex = assertThrows(
ArithmeticException.class,
e
);

assertThat(ex)
.hasMessage("Rounding necessary");
}
}

//</editor-fold>

void keeps_same_scaler_impl(BinaryOperator<FluentBigDecimal> fnc) {
FluentBigDecimal actual = fnc.apply(FIXTURE, FIXTURE);

Expand Down

0 comments on commit a42672b

Please sign in to comment.