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 classUtilities.ModifiableBoolean
-
Field Summary
Fields Modifier and Type Field Description static Comparator<int[]>lexComparatorIntstatic Comparator<String[]>lexComparatorString
-
Constructor Summary
Constructors Constructor Description Utilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringarrayToString(Object array)Method for converting an array into a string.static StringarrayToString(Object array, char LEFT, char RIGHT, String SEP)Method for converting an array into a string.static intbinomial(int n, int k)static BigIntegerbinomialBig(int n, int k)static <T> T[]buildArray(Class<?> cl, int length)static <T> T[][]buildArray(Class<?> cl, int length1, int length2)static booleancheck(Object obj, java.util.function.Predicate<Object> p)Returns true if inside the specified object, there is an element that checks the predicate.static booleancheckSafeArithmeticOperation(Runnable r)Checks if the specifiedRunnableobject raises anArithmeticExceptionobject, 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 booleancontains(int[] tab, int v)static booleancontains(int[] tab, int v, int from, int to)static Objectcontrol(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 Elementelement(Document doc, String tag, Object textContent)static Elementelement(Document doc, String tag, String attName, String attValue)static Elementelement(Document doc, String tag, String attName, String attValue, Object textContent)static Elementelement(Document doc, String tag, String attName1, String attValue1, String attName2, String attValue2)static Elementelement(Document doc, String tag, Collection<Map.Entry<String,Object>> attributes)static Elementelement(Document doc, String tag, List<Element> sons)static Elementelement(Document doc, String tag, java.util.stream.Stream<Map.Entry<String,Object>> attributes)static Elementelement(Document doc, String tag, Element son)static Elementelement(Document doc, String tag, Element son, java.util.stream.Stream<Element> otherSons)static Elementelement(Document doc, String tag, Element son, Element... otherSons)static Objectexit(String message)static intfactorial(int n)static BigIntegerfactorialBig(int n)static ObjectfirstNonNull(Object array)static booleanhasSize(Object array, int... size)Returns true is the array is regular and matches exactly the specified size.static intindexOf(int value, int[] t)static intindexOf(Object value, Object[] t)static intindexOf(String s, String... t)static intindexOf(String s, List<Object> list)static booleanisInteger(String token)static booleanisLong(String token)static booleanisNumeric(String token)static booleanisNumericInterval(String token)static booleanisRegular(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 booleanisSafeInt(long l)static booleanisSafeInt(long l, boolean useMargin)static booleanisTag(Element elt, Types.TypeChild type)Determines whether the specified element has the specified type as tag name.static Stringjoin(int[][] m, String separator, String delimiter)static Stringjoin(Object array)Method that joins the elements of the specified array, using a white-space as delimiter.static Stringjoin(Object[][] m, String separator, String delimiter)Method that joins the elements of the specified two-dimensional array, using the specified separator and delimiter.static Stringjoin(Object array, String delimiter)Method that joins the elements of the specified array, using the specified delimiter to separate them.static Stringjoin(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 DocumentloadDocument(String fileName)Method that loads an XML document, using the specified file name.static BigIntegernArrangementsFor(int[] nValues)static intpower(long a, int b)static BigIntegerpowerBig(long a, int b)static intrindexOf(int value, int[] t)static intsafeInt(Long l)Converts the specified long to int if it is safe to do it.static intsafeInt(Long l, boolean useMargin)Converts the specified long number to int if it is safe to do it.static intsafeIntWhileHandlingInfinity(long l)Converts the specified long to int if it is safe to do it.static LongsafeLong(String s)Method that parses the specified string as a long integer.static LongsafeLong(String s, boolean checkSpecialValues)Method that parses the specified string as a long integer.static voidsave(Document document, PrintWriter out)static Stringsave(Document document, String fileName)static Object[][]specificArray2DFrom(List<Object[]> list)static Object[]specificArrayFrom(List<Object> list)static intsplitToInt(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 BooleantoBoolean(String s)static StringtoCamelCase(String s)Returns the specified string in camel case form (with the first letter of the first word in lower case).static doubletoDouble(String token)static DoubletoDouble(String token, java.util.function.Predicate<Double> p)static IntegertoInteger(String token)static IntegertoInteger(String token, java.util.function.Predicate<Integer> p)static int[]toIntegers(String[] tokens)static StringtoKebabCase(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 specifiedRunnableobject raises anArithmeticExceptionobject, when run. The valuetrueis returned iff no such exception is raised.- Parameters:
r- aRunnableobject to be run- Returns:
trueiff noArithmeticExceptionis 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)
-
-