Package org.xcsp.common
Class Utilities
- java.lang.Object
-
- org.xcsp.common.Utilities
-
public class Utilities extends Object
A class with some utility (static) methods.- Author:
- Christophe Lecoutre
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Utilities.ModifiableBoolean
-
Field Summary
Fields Modifier and Type Field Description static Comparator<int[]>
lexComparatorInt
static Comparator<String[]>
lexComparatorString
-
Constructor Summary
Constructors Constructor Description Utilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
arrayToString(Object array)
Method for converting an array into a string.static String
arrayToString(Object array, char LEFT, char RIGHT, String SEP)
Method for converting an array into a string.static int
binomial(int n, int k)
static BigInteger
binomialBig(int n, int k)
static <T> T[]
buildArray(Class<?> cl, int length)
static <T> T[][]
buildArray(Class<?> cl, int length1, int length2)
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.static boolean
checkSafeArithmeticOperation(Runnable r)
Checks if the specifiedRunnable
object raises anArithmeticException
object, when run.static Element[]
childElementsOf(Element element)
Method that returns an array with the child elements of the specified element.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.static int[]
collectInt(Object... src)
Builds a 1-dimensional array of integers (int) from the specified sequence of parameters.static boolean
contains(int[] tab, int v)
static boolean
contains(int[] tab, int v, int from, int to)
static Object
control(boolean condition, String message)
Method that controls that the specified condition is verified.static <T> T[]
convert(Object[] t)
static <T> T[][]
convert(Object[][] t)
static <T> T[]
convert(Collection<T> list)
Builds a one-dimensional array of T with the objects of the specified list.static <T> T[]
convert(java.util.stream.Stream<T> stream)
Builds a one-dimensional array of T with the objects of the specified stream.static Element
element(Document doc, String tag, Object textContent)
static Element
element(Document doc, String tag, String attName, String attValue)
static Element
element(Document doc, String tag, String attName, String attValue, Object textContent)
static Element
element(Document doc, String tag, String attName1, String attValue1, String attName2, String attValue2)
static Element
element(Document doc, String tag, Collection<Map.Entry<String,Object>> attributes)
static Element
element(Document doc, String tag, List<Element> sons)
static Element
element(Document doc, String tag, java.util.stream.Stream<Map.Entry<String,Object>> attributes)
static Element
element(Document doc, String tag, Element son)
static Element
element(Document doc, String tag, Element son, java.util.stream.Stream<Element> otherSons)
static Element
element(Document doc, String tag, Element son, Element... otherSons)
static Object
exit(String message)
static int
factorial(int n)
static BigInteger
factorialBig(int n)
static Object
firstNonNull(Object array)
static boolean
hasSize(Object array, int... size)
Returns true is the array is regular and matches exactly the specified size.static int
indexOf(int value, int[] t)
static int
indexOf(Object value, Object[] t)
static int
indexOf(String s, String... t)
static int
indexOf(String s, List<Object> list)
static boolean
isInteger(String token)
static boolean
isLong(String token)
static boolean
isNumeric(String token)
static boolean
isNumericInterval(String token)
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...static boolean
isSafeInt(long l)
static boolean
isSafeInt(long l, boolean useMargin)
static boolean
isTag(Element elt, Types.TypeChild type)
Determines whether the specified element has the specified type as tag name.static String
join(int[][] m, String separator, String delimiter)
static String
join(Object array)
Method that joins the elements of the specified array, using a white-space as delimiter.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.static String
join(Object array, String delimiter)
Method that joins the elements of the specified array, using the specified delimiter to separate them.static String
join(Collection<? extends Object> c)
static <K,V>
Stringjoin(Map<K,V> m, String separator, String delimiter)
Method that joins the elements of the specified map, using the specified separator and delimiter.static Document
loadDocument(String fileName)
Method that loads an XML document, using the specified file name.static BigInteger
nArrangementsFor(int[] nValues)
static int
power(long a, int b)
static BigInteger
powerBig(long a, int b)
static int
rindexOf(int value, int[] t)
static int
safeInt(Long l)
Converts the specified long to int if it is safe to do it.static int
safeInt(Long l, boolean useMargin)
Converts the specified long number to int if it is safe to do it.static int
safeIntWhileHandlingInfinity(long l)
Converts the specified long to int if it is safe to do it.static Long
safeLong(String s)
Method that parses the specified string as a long integer.static Long
safeLong(String s, boolean checkSpecialValues)
Method that parses the specified string as a long integer.static void
save(Document document, PrintWriter out)
static String
save(Document document, String fileName)
static Object[][]
specificArray2DFrom(List<Object[]> list)
static Object[]
specificArrayFrom(List<Object> list)
static int
splitToInt(String s, String regex)
static int[]
splitToInts(String s)
static int[]
splitToInts(String s, String regex)
static <T> T[]
swap(T[] t, int i, int j)
static Boolean
toBoolean(String s)
static String
toCamelCase(String s)
Returns the specified string in camel case form (with the first letter of the first word in lower case).static double
toDouble(String token)
static Double
toDouble(String token, java.util.function.Predicate<Double> p)
static Integer
toInteger(String token)
static Integer
toInteger(String token, java.util.function.Predicate<Integer> p)
static int[]
toIntegers(String[] tokens)
static String
toKebabCase(String s)
Returns the specified string in kebab case form.static int[]
wordAsIntArray(String s)
-
-
-
Field Detail
-
lexComparatorInt
public static final Comparator<int[]> lexComparatorInt
-
lexComparatorString
public static final Comparator<String[]> lexComparatorString
-
-
Method Detail
-
buildArray
public static <T> T[] buildArray(Class<?> cl, int length)
-
buildArray
public static <T> T[][] buildArray(Class<?> cl, int length1, int length2)
-
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)
-
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 collectedsrc
- 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)
-
wordAsIntArray
public static int[] wordAsIntArray(String s)
-
isInteger
public static boolean isInteger(String token)
-
isLong
public static boolean isLong(String token)
-
toIntegers
public static int[] toIntegers(String[] tokens)
-
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(int value, int[] t)
-
rindexOf
public static int rindexOf(int value, int[] 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 specifiedRunnable
object raises anArithmeticException
object, when run. The valuetrue
is returned iff no such exception is raised.- Parameters:
r
- aRunnable
object to be run- Returns:
true
iff noArithmeticException
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 String join(Collection<? extends Object> c)
-
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
-
save
public static void save(Document document, PrintWriter out)
-
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.
-
element
public static Element element(Document doc, String tag, Element son, java.util.stream.Stream<Element> otherSons)
-
element
public static Element element(Document doc, String tag, String attName, String attValue, Object textContent)
-
element
public static Element element(Document doc, String tag, String attName1, String attValue1, String attName2, String attValue2)
-
element
public static Element element(Document doc, String tag, java.util.stream.Stream<Map.Entry<String,Object>> attributes)
-
-