change return type to optional
This commit is contained in:
parent
d694564141
commit
c4431dadf5
@ -4,38 +4,34 @@ import java.util.Optional;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
public class StringToIntConverter {
|
||||
|
||||
private StringToIntConverter() {
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingIntegerParseInt(String input){
|
||||
Optional<Integer> convertStringToIntUsingIntegerParseInt(String input){
|
||||
try {
|
||||
return Integer.parseInt(input);
|
||||
return Optional.of(Integer.parseInt(input));
|
||||
} catch (NumberFormatException e) {
|
||||
// log or handle the error
|
||||
return Integer.MIN_VALUE;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingIntegerValueOf(String input){
|
||||
Optional<Integer> convertStringToIntUsingIntegerValueOf(String input){
|
||||
try {
|
||||
return Integer.valueOf(input);
|
||||
return Optional.of(Integer.valueOf(input));
|
||||
} catch (NumberFormatException e) {
|
||||
// log or handle the error
|
||||
return Integer.MIN_VALUE;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingIntegerDecode(String input){
|
||||
Optional<Integer> convertStringToIntUsingIntegerDecode(String input){
|
||||
try {
|
||||
return Integer.decode(input);
|
||||
return Optional.of(Integer.decode(input));
|
||||
} catch (Exception e) {
|
||||
// log or handle the error
|
||||
return Integer.MIN_VALUE;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingOptional(String input){
|
||||
Optional<Integer> convertStringToIntUsingOptional(String input){
|
||||
Optional<Integer> parsedInt;
|
||||
try {
|
||||
parsedInt = Optional.of(Integer.parseInt(input));
|
||||
@ -43,10 +39,10 @@ public class StringToIntConverter {
|
||||
// log or handle the error
|
||||
parsedInt = Optional.empty();
|
||||
}
|
||||
return parsedInt.orElse(Integer.MIN_VALUE);
|
||||
return parsedInt;
|
||||
}
|
||||
|
||||
public static int convertStringToIntUsingNumberUtils(String input, Integer defaultValue){
|
||||
int convertStringToIntUsingNumberUtils(String input, Integer defaultValue){
|
||||
return NumberUtils.toInt(input, defaultValue);
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,9 @@ import java.util.List;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StringToIntConverterUnitTest {
|
||||
class stringToIntConverter {
|
||||
|
||||
private StringToIntConverter stringToIntConverter = new StringToIntConverter();
|
||||
|
||||
@Test
|
||||
void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() {
|
||||
@ -18,11 +20,11 @@ class StringToIntConverterUnitTest {
|
||||
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,Integer.MIN_VALUE ));
|
||||
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.convertStringToIntUsingOptional(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 ));
|
||||
});
|
||||
}
|
||||
|
||||
@ -33,11 +35,11 @@ class StringToIntConverterUnitTest {
|
||||
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, Integer.MIN_VALUE));
|
||||
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input));
|
||||
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.convertStringToIntUsingOptional(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));
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user