From ec46e380aca1c949ba18b90144597b521dc6a64b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 9 Apr 2016 21:50:02 +0200 Subject: Add new argument checking functions --- src/ch/epfl/xblast/ArgumentChecker.java | 43 +++++++++++++++++++++++++++++---- 1 file 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 @@ package ch.epfl.xblast; +import ch.epfl.cs108.Sq; + +import java.util.Collection; +import java.util.Objects; + /** * ArgumentChecker. * @@ -11,15 +16,43 @@ public final class ArgumentChecker { /** * Returns the given value if it is non-negative. * - * @param value the tested value + * @param v the tested value * @return the given value if non-negative * @throws IllegalArgumentException if the value is inferior to 0 */ - public static int requireNonNegative(int value) { - if (value >= 0) - return value; - else + public static int requireNonNegative(int v) { + if (v < 0) + throw new IllegalArgumentException(); + + return v; + } + + /** + * Requires the given Collection to be non-empty and returns it or throw an IllegalArgumentException otherwise. + * + * @param c the Collection to check + * @param the Collection type + * @return the checked Collection + */ + public static T requireNonEmpty(T c) { + if (Objects.requireNonNull(c).isEmpty()) throw new IllegalArgumentException(); + + return c; + } + + /** + * Requires the given sequence to be non-empty and returns it or throw an IllegalArgumentException otherwise. + * + * @param s the sequence to check + * @param the sequence type + * @return the checked sequence + */ + public static T requireNonEmpty(T s) { + if (Objects.requireNonNull(s).isEmpty()) + throw new IllegalArgumentException(); + + return s; } } -- cgit v1.2.3