Class Utilities


  • public class Utilities
    extends Object
    A class with some utility (static) methods.
    Author:
    Christophe Lecoutre
    • Field Detail

      • lexComparatorInt

        public static final Comparator<int[]> lexComparatorInt
      • lexComparatorString

        public static final Comparator<String[]> lexComparatorString
    • Constructor Detail

      • Utilities

        public Utilities()
    • Method Detail

      • buildArray

        public static <T> T[] buildArray​(Class<?> cl,
                                         int length)
      • buildArray

        public static <T> T[][] buildArray​(Class<?> cl,
                                           int length1,
                                           int length2)
      • firstNonNull

        public static Object firstNonNull​(Object array)
      • convert

        public static <T> T[] convert​(Collection<T> list)
        Builds a one-dimensional array of T with the objects of the specified list. If the list does not contain any object other than null, null is returned.
      • convert

        public static <T> T[] convert​(java.util.stream.Stream<T> stream)
        Builds a one-dimensional array of T with the objects of the specified stream. If the stream does not contain any object other than null, null is returned.
      • convert

        public static <T> T[] convert​(Object[] t)
      • convert

        public static <T> T[][] convert​(Object[][] t)
      • specificArrayFrom

        public static Object[] specificArrayFrom​(List<Object> list)
      • specificArray2DFrom

        public static Object[][] specificArray2DFrom​(List<Object[]> list)
      • collect

        public static <T> T[] collect​(Class<T> clazz,
                                      Object... src)
        Returns a 1-dimensional array of objects of the specified type after collecting any object of this type being present in the specified objects. The specified objects can be stream (and IntStream), collections and arrays. The collecting process is made recursively.
        Parameters:
        clazz - the class of the objects to be collected
        src - the objects where to collect the objects
        Returns:
        a 1-dimensional array of objects of the specified type after collecting any object of this type being present in the specified objects
      • collectInt

        public static int[] collectInt​(Object... src)
        Builds a 1-dimensional array of integers (int) from the specified sequence of parameters. Each parameter can be an integer, a Range, an array, a stream, a collection, etc. All integers are collected and concatenated to form a 1-dimensional array.
        Parameters:
        src - the objects where to collect the integers
        Returns:
        a 1-dimensional array of integers after collecting any encountered integer in the specified objects
      • isNumeric

        public static boolean isNumeric​(String token)
      • isNumericInterval

        public static boolean isNumericInterval​(String token)
      • splitToInts

        public static int[] splitToInts​(String s,
                                        String regex)
      • splitToInts

        public static int[] splitToInts​(String s)
      • splitToInt

        public static int splitToInt​(String s,
                                     String regex)
      • wordAsIntArray

        public static int[] wordAsIntArray​(String s)
      • isInteger

        public static boolean isInteger​(String token)
      • toInteger

        public static Integer toInteger​(String token,
                                        java.util.function.Predicate<Integer> p)
      • toIntegers

        public static int[] toIntegers​(String[] tokens)
      • toDouble

        public static Double toDouble​(String token,
                                      java.util.function.Predicate<Double> p)
      • toDouble

        public static double toDouble​(String token)
      • powerBig

        public static BigInteger powerBig​(long a,
                                          int b)
      • power

        public static int power​(long a,
                                int b)
      • factorialBig

        public static BigInteger factorialBig​(int n)
      • factorial

        public static int factorial​(int n)
      • binomialBig

        public static BigInteger binomialBig​(int n,
                                             int k)
      • binomial

        public static int binomial​(int n,
                                   int k)
      • nArrangementsFor

        public static BigInteger nArrangementsFor​(int[] nValues)
      • contains

        public static boolean contains​(int[] tab,
                                       int v,
                                       int from,
                                       int to)
      • contains

        public static boolean contains​(int[] tab,
                                       int v)
      • indexOf

        public static int indexOf​(String s,
                                  String... t)
      • indexOf

        public static int indexOf​(int value,
                                  int[] t)
      • indexOf

        public static int indexOf​(Object value,
                                  Object[] t)
      • hasSize

        public static boolean hasSize​(Object array,
                                      int... size)
        Returns true is the array is regular and matches exactly the specified size. For example, if size is [5,4] then the specified array must be a 2-dimensional array of 5 x 4 squares.
      • isRegular

        public static boolean isRegular​(Object array)
        Returns true is the array is regular, that is to say has the form of a rectangle for a 2-dimensional array, a cube for a 3-dimensional array... For example, if the specified array is a 2-dimensional array of 5 x 4 squares, then it is regular. But it has 3 squares for the first row, and 4 squares for the second row, then it is not regular.
      • control

        public static Object control​(boolean condition,
                                     String message)
        Method that controls that the specified condition is verified. If it is not the case, a message is displayed and the program is stopped.
      • checkSafeArithmeticOperation

        public static boolean checkSafeArithmeticOperation​(Runnable r)
        Checks if the specified Runnable object raises an ArithmeticException object, when run. The value true is returned iff no such exception is raised.
        Parameters:
        r - a Runnable object to be run
        Returns:
        true iff no ArithmeticException is raised when running the specified code
      • safeLong

        public static Long safeLong​(String s,
                                    boolean checkSpecialValues)
        Method that parses the specified string as a long integer. If the value is too small or too big, an exception is raised. The specified boolean allows us to indicate if some special values (such as +infinity) must be checked.
      • safeLong

        public static Long safeLong​(String s)
        Method that parses the specified string as a long integer. If the value is too small or too big, an exception is raised.
      • isSafeInt

        public static boolean isSafeInt​(long l,
                                        boolean useMargin)
      • isSafeInt

        public static boolean isSafeInt​(long l)
      • safeInt

        public static int safeInt​(Long l,
                                  boolean useMargin)
        Converts the specified long number to int if it is safe to do it. When the specified boolean is set to true, we control that it is safe according to the constants MIN_SAFE_INT and MAX_SAFE_INT.
      • safeInt

        public static int safeInt​(Long l)
        Converts the specified long to int if it is safe to do it. We control that it is safe according to the constants MIN_SAFE_INT and MAX_SAFE_INT.
      • safeIntWhileHandlingInfinity

        public static int safeIntWhileHandlingInfinity​(long l)
        Converts the specified long to int if it is safe to do it. Note that VAL_MINUS_INFINITY will be translated to VAL_MINUS_INFINITY_INT and that VAL_PLUS_INFINITY will be translated to VAL_PLUS_INFINITY_INT . We control that it is safe according to the constants MIN_SAFE_INT and MAX_SAFE_INT.
      • swap

        public static <T> T[] swap​(T[] t,
                                   int i,
                                   int j)
      • join

        public static String join​(Object array,
                                  String delimiter)
        Method that joins the elements of the specified array, using the specified delimiter to separate them.
      • join

        public static String join​(Object array)
        Method that joins the elements of the specified array, using a white-space as delimiter.
      • join

        public static <K,​V> String join​(Map<K,​V> m,
                                              String separator,
                                              String delimiter)
        Method that joins the elements of the specified map, using the specified separator and delimiter.
      • join

        public static String join​(Object[][] m,
                                  String separator,
                                  String delimiter)
        Method that joins the elements of the specified two-dimensional array, using the specified separator and delimiter.
      • toCamelCase

        public static String toCamelCase​(String s)
        Returns the specified string in camel case form (with the first letter of the first word in lower case).
      • toKebabCase

        public static String toKebabCase​(String s)
        Returns the specified string in kebab case form.
      • arrayToString

        public static String arrayToString​(Object array,
                                           char LEFT,
                                           char RIGHT,
                                           String SEP)
        Method for converting an array into a string.
      • arrayToString

        public static String arrayToString​(Object array)
        Method for converting an array into a string.
      • check

        public static boolean check​(Object obj,
                                    java.util.function.Predicate<Object> p)
        Returns true if inside the specified object, there is an element that checks the predicate. If syntactic trees are encountered, we check the leaves only.
      • loadDocument

        public static Document loadDocument​(String fileName)
                                     throws Exception
        Method that loads an XML document, using the specified file name.
        Throws:
        Exception
      • childElementsOf

        public static Element[] childElementsOf​(Element element)
        Method that returns an array with the child elements of the specified element.
      • isTag

        public static boolean isTag​(Element elt,
                                    Types.TypeChild type)
        Determines whether the specified element has the specified type as tag name.