ce4da2f9b873ddf5b7e71208c694fb1af3002e11
[Sone.git] / src / main / java / net / pterodactylus / sone / utils / NumberParsers.java
1 package net.pterodactylus.sone.utils;
2
3 import javax.annotation.Nonnull;
4 import javax.annotation.Nullable;
5
6 import com.google.common.primitives.Ints;
7 import com.google.common.primitives.Longs;
8
9 /**
10  * Parses numbers from strings.
11  *
12  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
13  */
14 public class NumberParsers {
15
16         @Nullable
17         public static Integer parseInt(@Nullable String text,
18                         @Nullable Integer defaultValue) {
19                 if (text == null) {
20                         return defaultValue;
21                 }
22                 Integer value = Ints.tryParse(text);
23                 return (value == null) ? defaultValue : value;
24         }
25
26         @Nullable
27         public static Long parseLong(@Nullable String text,
28                         @Nullable Long defaultValue) {
29                 if (text == null) {
30                         return defaultValue;
31                 }
32                 Long value = Longs.tryParse(text);
33                 return (value == null) ? defaultValue : value;
34         }
35
36 }