aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ch/epfl/xblast/ArgumentChecker.java43
1 files changed, 38 insertions, 5 deletions
diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java
index 589c197..a8ca31a 100644
--- a/src/ch/epfl/xblast/ArgumentChecker.java
+++ b/src/ch/epfl/xblast/ArgumentChecker.java
@@ -1,5 +1,10 @@
1package ch.epfl.xblast; 1package ch.epfl.xblast;
2 2
3import ch.epfl.cs108.Sq;
4
5import java.util.Collection;
6import java.util.Objects;
7
3/** 8/**
4 * ArgumentChecker. 9 * ArgumentChecker.
5 * 10 *
@@ -11,15 +16,43 @@ public final class ArgumentChecker {
11 /** 16 /**
12 * Returns the given value if it is non-negative. 17 * Returns the given value if it is non-negative.
13 * 18 *
14 * @param value the tested value 19 * @param v the tested value
15 * @return the given value if non-negative 20 * @return the given value if non-negative
16 * @throws IllegalArgumentException if the value is inferior to 0 21 * @throws IllegalArgumentException if the value is inferior to 0
17 */ 22 */
18 public static int requireNonNegative(int value) { 23 public static int requireNonNegative(int v) {
19 if (value >= 0) 24 if (v < 0)
20 return value; 25 throw new IllegalArgumentException();
21 else 26
27 return v;
28 }
29
30 /**
31 * Requires the given Collection to be non-empty and returns it or throw an IllegalArgumentException otherwise.
32 *
33 * @param c the Collection to check
34 * @param <T> the Collection type
35 * @return the checked Collection
36 */
37 public static <T extends Collection> T requireNonEmpty(T c) {
38 if (Objects.requireNonNull(c).isEmpty())
22 throw new IllegalArgumentException(); 39 throw new IllegalArgumentException();
40
41 return c;
42 }
43
44 /**
45 * Requires the given sequence to be non-empty and returns it or throw an IllegalArgumentException otherwise.
46 *
47 * @param s the sequence to check
48 * @param <T> the sequence type
49 * @return the checked sequence
50 */
51 public static <T extends Sq> T requireNonEmpty(T s) {
52 if (Objects.requireNonNull(s).isEmpty())
53 throw new IllegalArgumentException();
54
55 return s;
23 } 56 }
24 57
25} 58}