Skip to content

Commit

Permalink
Add methods in KiwiPrimitives to parse CharSequence to double (#917)
Browse files Browse the repository at this point in the history
Closes #914
  • Loading branch information
sleberknight authored Mar 15, 2023
1 parent f39df9a commit cee9e31
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiPrimitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import lombok.experimental.UtilityClass;

import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

Expand Down Expand Up @@ -130,4 +131,47 @@ public static long tryParseLongOrThrow(CharSequence cs) {
throw new IllegalStateException(e);
}
}

/**
* Attempt to parse the given {@link CharSequence} to an {@link Double}.
*
* @param cs the value to parse
* @return the value as a Double or {@code null} if the value cannot be parsed
*/
public static Double tryParseDoubleOrNull(CharSequence cs) {
try {
return Double.valueOf(cs.toString());
} catch (Exception e) {
return null;
}
}

/**
* Attempt to parse the given {@link CharSequence} to an {@code double}.
*
* @param cs the value to parse
* @return an {@link OptionalDouble} that will contain the parsed value or will be empty if the input cannot be parsed
*/
public static OptionalDouble tryParseDouble(CharSequence cs) {
try {
return OptionalDouble.of(Double.parseDouble(cs.toString()));
} catch (Exception e) {
return OptionalDouble.empty();
}
}

/**
* Attempt to parse the given {@link CharSequence} to an {@code double}.
*
* @param cs the value to parse
* @return the parsed {@code double} value if successful; if it cannot be parsed this method always throws an exception
* @throws IllegalStateException if the value cannot be parsed
*/
public static double tryParseDoubleOrThrow(CharSequence cs) {
try {
return Double.parseDouble(cs.toString());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
68 changes: 68 additions & 0 deletions src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,72 @@ void shouldThrow_WhenArgumentIsNotParseableToLong(String string) {
.withCauseExactlyInstanceOf(NumberFormatException.class);
}
}

@Nested
class TryParseDoubleOrNull {

@Test
void shouldReturnDouble_WhenArgumentIsParseableToDouble() {
assertThat(KiwiPrimitives.tryParseDoubleOrNull("420000.042")).isEqualTo(420_000.042);
}

@ParameterizedTest
@NullAndEmptySource
void shouldReturnNull_WhenArgumentIsNullOrEmpty(String string) {
assertThat(KiwiPrimitives.tryParseDoubleOrNull(string)).isNull();
}

@ParameterizedTest
@ValueSource(strings = {" ", "abcd", "42a", "a42", "4,200", "420_000"})
void shouldReturnNull_WhenArgumentIsNotParseableToDouble(String string) {
assertThat(KiwiPrimitives.tryParseDoubleOrNull(string)).isNull();
}
}

@Nested
class TryParseDouble {

@Test
void shouldReturnOptionalHavingValue_WhenArgumentIsParseableToDouble() {
assertThat(KiwiPrimitives.tryParseDouble("840000.042")).hasValue(840_000.042);
}

@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyOptional_WhenArgumentIsNullOrEmpty(String string) {
assertThat(KiwiPrimitives.tryParseDouble(string)).isEmpty();
}

@ParameterizedTest
@ValueSource(strings = {" ", "abcd", "42a", "a42", "4,200", "4200,00"})
void shouldReturnEmptyOptional_WhenArgumentIsNotParseableToDouble(String string) {
assertThat(KiwiPrimitives.tryParseDouble(string)).isEmpty();
}
}

@Nested
class TryParseDoubleOrThrow {

@Test
void shouldReturDouble_WhenArgumentIsParseableToDouble() {
assertThat(KiwiPrimitives.tryParseDoubleOrThrow("1260000.042")).isEqualTo(1_260_000.042);
}

@Test
void shouldThrow_WhenArgumentIsNull() {
assertThatIllegalStateException()
.isThrownBy(() -> KiwiPrimitives.tryParseDoubleOrThrow(null))
.withMessageContaining("java.lang.NullPointerException")
.withCauseExactlyInstanceOf(NullPointerException.class);
}

@ParameterizedTest
@ValueSource(strings = {"", " ", "abcd", "42a", "a42", "4,200", "420-000"})
void shouldThrow_WhenArgumentIsNotParseableToDouble(String string) {
assertThatIllegalStateException()
.isThrownBy(() -> KiwiPrimitives.tryParseDoubleOrThrow(string))
.withMessageContaining(string)
.withCauseExactlyInstanceOf(NumberFormatException.class);
}
}
}

0 comments on commit cee9e31

Please sign in to comment.