Skip to content

Commit

Permalink
Add methods in KiwiPrimitives to parse CharSequence to long
Browse files Browse the repository at this point in the history
Part of #914
  • Loading branch information
sleberknight committed Mar 15, 2023
1 parent 5882aae commit ae8e119
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
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 @@ -5,6 +5,7 @@
import lombok.experimental.UtilityClass;

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

/**
* Static utilities that operate on primitive values, and are not already provided by classes in Guava's
Expand Down Expand Up @@ -86,4 +87,47 @@ public static int tryParseIntOrThrow(CharSequence cs) {
throw new IllegalStateException(e);
}
}

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

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

/**
* Attempt to parse the given {@link CharSequence} to an {@code long}.
*
* @param cs the value to parse
* @return the parsed {@code long} value if successful; if it cannot be parsed this method always throws an exception
* @throws IllegalStateException if the value cannot be parsed
*/
public static long tryParseLongOrThrow(CharSequence cs) {
try {
return Long.parseLong(cs.toString());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
70 changes: 69 additions & 1 deletion src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void shouldReturnOptionalHavingValue_WhenArgumentIsParseableToInt() {

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

Expand Down Expand Up @@ -102,4 +102,72 @@ void shouldThrow_WhenArgumentIsNotParseableToInt(String string) {
.withCauseExactlyInstanceOf(NumberFormatException.class);
}
}

@Nested
class TryParseLongOrNull {

@Test
void shouldReturnLong_WhenArgumentIsParseableToLong() {
assertThat(KiwiPrimitives.tryParseLongOrNull("420000")).isEqualTo(420_000L);
}

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

@ParameterizedTest
@ValueSource(strings = {" ", "abcd", "42a", "a42", "4,200", "420.0"})
void shouldReturnNull_WhenArgumentIsNotParseableToLong(String string) {
assertThat(KiwiPrimitives.tryParseLongOrNull(string)).isNull();
}
}

@Nested
class TryParseLong {

@Test
void shouldReturnOptionalHavingValue_WhenArgumentIsParseableToLong() {
assertThat(KiwiPrimitives.tryParseLong("840000")).hasValue(840_000L);
}

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

@ParameterizedTest
@ValueSource(strings = {" ", "abcd", "42a", "a42", "4,200", "420.0"})
void shouldReturnEmptyOptional_WhenArgumentIsNotParseableToLong(String string) {
assertThat(KiwiPrimitives.tryParseLong(string)).isEmpty();
}
}

@Nested
class TryParseLongOrThrow {

@Test
void shouldReturLong_WhenArgumentIsParseableToLong() {
assertThat(KiwiPrimitives.tryParseLongOrThrow("1260000")).isEqualTo(1_260_000L);
}

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

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

0 comments on commit ae8e119

Please sign in to comment.