package ch.epfl.xblast; import ch.epfl.cs108.Sq; import java.util.Collection; import java.util.Objects; /** * ArgumentChecker. * * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public final class ArgumentChecker { private ArgumentChecker() { // Static class } /** * Returns the given value if it is non-negative. * * @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 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; } /** * Returns the element from the array at the requested index, or null if it cannot be retrieved. * * @param array the array * @param index the index * @param the type of element * @return the requested element, or null */ public static T getOrNull(T[] array, int index) { if (Objects.isNull(array) || index < 0 || index >= array.length) return null; else return array[index]; } /** * Parses and returns an integer, or return null on error. * * @param str the integer to parse * @return the parsed integer, or null */ public static Integer parseIntOrNull(String str) { try { return Integer.parseInt(str); } catch (NumberFormatException | NullPointerException e) { return null; } } }