aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/ArgumentChecker.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/xblast/ArgumentChecker.java')
-rw-r--r--src/ch/epfl/xblast/ArgumentChecker.java29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java
index 7e77b61..56f8acd 100644
--- a/src/ch/epfl/xblast/ArgumentChecker.java
+++ b/src/ch/epfl/xblast/ArgumentChecker.java
@@ -59,4 +59,33 @@ public final class ArgumentChecker {
59 return s; 59 return s;
60 } 60 }
61 61
62 /**
63 * Returns the element from the array at the requested index, or null if it cannot be retrieved.
64 *
65 * @param array the array
66 * @param index the index
67 * @param <T> the type of element
68 * @return the requested element, or null
69 */
70 public static <T> T getOrNull(T[] array, int index) {
71 if (Objects.isNull(array) || index < 0 || index >= array.length)
72 return null;
73 else
74 return array[index];
75 }
76
77 /**
78 * Parses and returns an integer, or return null on error.
79 *
80 * @param str the integer to parse
81 * @return the parsed integer, or null
82 */
83 public static Integer parseIntOrNull(String str) {
84 try {
85 return Integer.parseInt(str);
86 } catch (NumberFormatException | NullPointerException e) {
87 return null;
88 }
89 }
90
62} 91}