From c4431dadf5366c7fae40c26209331337f46f132b Mon Sep 17 00:00:00 2001 From: Deboshree Date: Thu, 18 May 2023 19:27:02 +0530 Subject: [PATCH] change return type to optional --- .../stringtoint/StringToIntConverter.java | 28 ++++++++----------- .../StringToIntConverterUnitTest.java | 24 ++++++++-------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java b/core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java index 52d227c379..6e7ea05bd7 100644 --- a/core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java +++ b/core-java-modules/core-java-string-conversions-3/src/main/java/com/baeldung/stringtoint/StringToIntConverter.java @@ -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 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 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 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 convertStringToIntUsingOptional(String input){ Optional 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); } } diff --git a/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java b/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java index 1bd7450914..c0c5da7731 100644 --- a/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java +++ b/core-java-modules/core-java-string-conversions-3/src/test/java/com/baeldung/stringtoint/StringToIntConverterUnitTest.java @@ -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)); }); }