Merge pull request #13761 from ciphx/string_to_int_encapsulation

https://jira.baeldung.com/browse/BAEL-5859
This commit is contained in:
Vini 2023-05-22 13:42:20 +02:00 committed by GitHub
commit 2285893a3d
5 changed files with 99 additions and 105 deletions

View File

@ -12,6 +12,14 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-numbers-conversions</finalName>
<resources>

View File

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

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 {
private StringToIntConverter stringToIntConverter = new StringToIntConverter();
@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).orElse(Integer.MIN_VALUE));
Assertions.assertEquals(data.expectedOutput, stringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input).orElse(Integer.MIN_VALUE));
Assertions.assertEquals(data.expectedOutput, stringToIntConverter.convertStringToIntUsingIntegerDecode(data.input).orElse(Integer.MIN_VALUE));
Assertions.assertEquals(data.expectedOutput, stringToIntConverter.convertStringToIntUsingNumberUtils(data.input,Integer.MIN_VALUE ));
});
}
@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).orElse(Integer.MIN_VALUE));
Assertions.assertEquals(data.expectedOutput, stringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input).orElse(Integer.MIN_VALUE));
Assertions.assertEquals(data.expectedOutput, stringToIntConverter.convertStringToIntUsingNumberUtils(data.input, Integer.MIN_VALUE));
Assertions.assertEquals(data.expectedOutput, stringToIntConverter.convertStringToIntUsingIntegerDecode(data.input).orElse(Integer.MIN_VALUE));
});
}
public static class TestData{
String input;
Integer expectedOutput;
TestData(String input, Integer expectedOutput){
this.input = input;
this.expectedOutput = expectedOutput;
}
}
}

View File

@ -1,52 +0,0 @@
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

@ -1,53 +0,0 @@
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;
}
}
}