Don’t use number parsing from the utils package anymore.
[Sone.git] / src / main / java / net / pterodactylus / sone / utils / NumberParsers.java
diff --git a/src/main/java/net/pterodactylus/sone/utils/NumberParsers.java b/src/main/java/net/pterodactylus/sone/utils/NumberParsers.java
new file mode 100644 (file)
index 0000000..9e8afb4
--- /dev/null
@@ -0,0 +1,36 @@
+package net.pterodactylus.sone.utils;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Longs;
+
+/**
+ * Parses numbers from strings.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class NumberParsers {
+
+       @Nonnull
+       public static Integer parseInt(@Nullable String text,
+                       @Nullable Integer defaultValue) {
+               if (text == null) {
+                       return defaultValue;
+               }
+               Integer value = Ints.tryParse(text);
+               return (value == null) ? defaultValue : value;
+       }
+
+       @Nonnull
+       public static Long parseLong(@Nullable String text,
+                       @Nullable Long defaultValue) {
+               if (text == null) {
+                       return defaultValue;
+               }
+               Long value = Longs.tryParse(text);
+               return (value == null) ? defaultValue : value;
+       }
+
+}