Merge pull request #13706 from ciphx/string_to_int_encapsulation

String to int encapsulation
This commit is contained in:
Vini 2023-03-25 16:54:57 +01:00 committed by GitHub
commit 50d78ab618
2 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package com.baeldung.stringtoint;
import java.util.Optional;
import org.apache.commons.lang3.math.NumberUtils;
public class StringToIntConverter {
private StringToIntConverter() {
}
public static Integer convertStringToIntUsingIntegerParseInt(String input){
try {
return Integer.parseInt(input);
} catch (NumberFormatException e) {
// log or handle the error
return Integer.MIN_VALUE;
}
}
public static Integer convertStringToIntUsingIntegerValueOf(String input){
try {
return Integer.valueOf(input);
} catch (NumberFormatException e) {
// log or handle the error
return Integer.MIN_VALUE;
}
}
public static Integer convertStringToIntUsingIntegerDecode(String input){
try {
return Integer.decode(input);
} catch (Exception e) {
// log or handle the error
return Integer.MIN_VALUE;
}
}
public static Integer convertStringToIntUsingOptional(String input){
Optional<Integer> parsedInt;
try {
parsedInt = Optional.of(Integer.parseInt(input));
} catch (Exception e) {
// log or handle the error
parsedInt = Optional.empty();
}
return parsedInt.orElse(Integer.MIN_VALUE);
}
public static int convertStringToIntUsingNumberUtils(String input){
return NumberUtils.toInt(input, Integer.MIN_VALUE);
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.stringtoint;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class StringToIntConverterUnitTest {
@Test
void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() {
List<TestData> testData = Arrays.asList(
new TestData("", Integer.MIN_VALUE),
new TestData(null, Integer.MIN_VALUE),
new TestData("23,56", Integer.MIN_VALUE),
new TestData("2147483648", Integer.MIN_VALUE),
new TestData("-2147483649", Integer.MIN_VALUE),
new TestData("hello", Integer.MIN_VALUE)
);
testData.forEach(data -> {
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input));
});
}
@Test
void whenConvertingIntToString_thenValidCasesReturnUnboxedInt() {
List<TestData> testData = Arrays.asList(
new TestData("23", 23),
new TestData("-23", -23)
);
testData.forEach(data -> {
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input));
});
}
public static class TestData{
String input;
Integer expectedOutput;
TestData(String input, Integer expectedOutput){
this.input = input;
this.expectedOutput = expectedOutput;
}
}
}