Skip to content

Commit 58c21c5

Browse files
authored
refactor: simplify ParseInteger (#4376)
1 parent 5bb5497 commit 58c21c5

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/main/java/com/thealgorithms/maths/ParseInteger.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
package com.thealgorithms.maths;
22

3-
public class ParseInteger {
3+
public final class ParseInteger {
4+
private ParseInteger() {
5+
}
6+
7+
private static void checkInput(final String s) {
8+
if (s == null) {
9+
throw new NumberFormatException("Input parameter must not be null!");
10+
}
11+
if (s.isEmpty()) {
12+
throw new NumberFormatException("Input parameter must not be empty!");
13+
}
14+
}
15+
16+
private static void checkDigitAt(final String s, final int pos) {
17+
if (!Character.isDigit(s.charAt(pos))) {
18+
throw new NumberFormatException("Input parameter of incorrect format: " + s);
19+
}
20+
}
21+
22+
private static int digitToInt(final char digit) {
23+
return digit - '0';
24+
}
25+
426
/**
527
* Parse a string to integer
628
*
@@ -9,18 +31,15 @@ public class ParseInteger {
931
* @throws NumberFormatException if the {@code string} does not contain a
1032
* parsable integer.
1133
*/
12-
public static int parseInt(String s) {
13-
if (s == null || s.length() == 0) {
14-
throw new NumberFormatException("Input parameter must not be null!");
15-
}
16-
boolean isNegative = s.charAt(0) == '-';
17-
boolean isPositive = s.charAt(0) == '+';
34+
public static int parseInt(final String s) {
35+
checkInput(s);
36+
37+
final boolean isNegative = s.charAt(0) == '-';
38+
final boolean isPositive = s.charAt(0) == '+';
1839
int number = 0;
19-
for (int i = isNegative ? 1 : isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
20-
if (!Character.isDigit(s.charAt(i))) {
21-
throw new NumberFormatException("Input parameter of incorrect format: " + s);
22-
}
23-
number = number * 10 + s.charAt(i) - '0';
40+
for (int i = isNegative || isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
41+
checkDigitAt(s, i);
42+
number = number * 10 + digitToInt(s.charAt(i));
2443
}
2544
return isNegative ? -number : number;
2645
}

src/test/java/com/thealgorithms/maths/ParseIntegerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
public class ParseIntegerTest {
1010
private static final String NULL_PARAMETER_MESSAGE = "Input parameter must not be null!";
11+
private static final String EMPTY_PARAMETER_MESSAGE = "Input parameter must not be empty!";
1112
private static final String INCORRECT_FORMAT_MESSAGE = "Input parameter of incorrect format";
1213

1314
@Test
@@ -16,6 +17,12 @@ public void testNullInput() {
1617
Assertions.assertEquals(exception.getMessage(), NULL_PARAMETER_MESSAGE);
1718
}
1819

20+
@Test
21+
public void testEmptyInput() {
22+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> ParseInteger.parseInt(""));
23+
Assertions.assertEquals(exception.getMessage(), EMPTY_PARAMETER_MESSAGE);
24+
}
25+
1926
@Test
2027
public void testInputOfIncorrectFormat() {
2128
IllegalArgumentException exception = Assertions.assertThrows(NumberFormatException.class, () -> ParseInteger.parseInt("+0a123"));

0 commit comments

Comments
 (0)