Skip to content

Commit 964b8b6

Browse files
committed
Improvements for getter methods of primitive types
1 parent 51f044b commit 964b8b6

File tree

1 file changed

+6
-14
lines changed
  • src/main/java/me/despical/commandframework/utils

1 file changed

+6
-14
lines changed

src/main/java/me/despical/commandframework/utils/Utils.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import java.util.stream.Collectors;
2929

3030
/**
31-
* @author Despical
31+
* This class is a part of Despical's Commons library.
3232
* <p>
3333
* Created at 30.05.2020
3434
*
35-
* This class is a part of Despical's Commons library.
35+
* @author Despical
3636
*/
3737
@ApiStatus.Internal
3838
public class Utils {
@@ -48,11 +48,9 @@ private Utils() {
4848
* @return the int represented by the string, or zero if conversion fails
4949
*/
5050
public static int getInt(String string) {
51-
if (string == null) return 0;
52-
5351
try {
5452
return Integer.parseInt(string);
55-
} catch (NumberFormatException ignored) {
53+
} catch (NumberFormatException | NullPointerException ignored) {
5654
return 0;
5755
}
5856
}
@@ -65,11 +63,9 @@ public static int getInt(String string) {
6563
* @return the double represented by the string, or zero if conversion fails
6664
*/
6765
public static double getDouble(String string) {
68-
if (string == null) return 0d;
69-
7066
try {
7167
return Double.parseDouble(string);
72-
} catch (NumberFormatException ignored) {
68+
} catch (NumberFormatException | NullPointerException ignored) {
7369
return 0d;
7470
}
7571
}
@@ -82,11 +78,9 @@ public static double getDouble(String string) {
8278
* @return the long represented by the string, or zero if conversion fails
8379
*/
8480
public static long getLong(String string) {
85-
if (string == null) return 0L;
86-
8781
try {
8882
return Long.parseLong(string);
89-
} catch (NumberFormatException ignored) {
83+
} catch (NumberFormatException | NullPointerException ignored) {
9084
return 0L;
9185
}
9286
}
@@ -99,11 +93,9 @@ public static long getLong(String string) {
9993
* @return the float represented by the string, or zero if conversion fails
10094
*/
10195
public static float getFloat(String string) {
102-
if (string == null) return 0F;
103-
10496
try {
10597
return Float.parseFloat(string);
106-
} catch (NumberFormatException ignored) {
98+
} catch (NumberFormatException | NullPointerException ignored) {
10799
return 0F;
108100
}
109101
}

0 commit comments

Comments
 (0)