Package com.univocity.parsers.common
Class ArgumentUtils
java.lang.Object
com.univocity.parsers.common.ArgumentUtils
An utility class for validating inputs.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final NormalizedString[]
static final String[]
An empty String array. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
displayLineSeparators
(String str, boolean addNewLine) Displays line separators in a string by replacing all instances of `\r` and `\n` with `[cr]` and `[lf]`.static <T> T[]
findDuplicates
(T[] array) Identifies duplicate values in a given array and returns themstatic Object[]
findMissingElements
(Object[] array, Object[] elements) Searches for elements in a given array and returns the elements not found.static Object[]
findMissingElements
(Object[] array, Collection<?> elements) Searches for elements in a given array and returns the elements not found.static int[]
Returns the indexes of an element in a given array.static int
indexOf
(char[] array, char element, int from) Returns the index of a character in a given array.static int
indexOf
(NormalizedString[] array, NormalizedString element, FieldSelector fieldSelector) Returns the index of a header, when headers are selected using aFieldSelector
.static int
Returns the index of an element in a given array.private static int
Returns the index of an element in a given array.static Reader
Creates aReader
for a given a filestatic Reader
Creates aReader
for a given a filestatic Reader
Creates aReader
for a given a filestatic Reader
newReader
(InputStream input) Creates aReader
from an input streamstatic Reader
newReader
(InputStream input, String encoding) Creates aReader
from an input streamstatic Reader
newReader
(InputStream input, Charset encoding) Creates aReader
from an input streamstatic Writer
Creates aWriter
from a filestatic Writer
Creates aWriter
from a filestatic Writer
Creates aWriter
from a filestatic Writer
newWriter
(OutputStream output) Creates aWriter
from an output streamstatic Writer
newWriter
(OutputStream output, String encoding) Creates aWriter
from an output streamstatic Writer
newWriter
(OutputStream output, Charset encoding) Creates aWriter
from an output streamstatic <T> void
Throws an IllegalArgumentException if the given array is null,empty, or contains null valuesstatic <T> void
Throws an IllegalArgumentException if the given array is null or empty.static int[]
removeAll
(int[] array, int e) Removes all instances of a given element from an int array.static String
restrictContent
(int length, CharSequence content) Restricts the length of a given content.static String
restrictContent
(int length, Object content) Restricts the length of a given content.private static <T extends Exception>
voidthrowsUnchecked
(Throwable toThrow) static void
throwUnchecked
(Throwable error) Allows rethrowing a checked exception instead of wrapping it into a runtime exception.static String[]
Converts a list of enumerations to an array of theirEnum.toString()
representationstatic byte[]
toByteArray
(int... ints) Converts a sequence of int numbers into a byte array.static char[]
toCharArray
(Collection<Character> characters) Converts any collection ofCharacter
into a char array.static int[]
toIntArray
(Collection<Integer> ints) Converts any collection ofInteger
into anint
array.static String
Removes surrounding spaces from a givenString
, from its right or left side, or both.
-
Field Details
-
EMPTY_STRING_ARRAY
An empty String array. -
EMPTY_NORMALIZED_STRING_ARRAY
-
-
Constructor Details
-
ArgumentUtils
public ArgumentUtils()
-
-
Method Details
-
notEmpty
Throws an IllegalArgumentException if the given array is null or empty.- Type Parameters:
T
- Type of arguments to be validated- Parameters:
argDescription
- the description of the elementsargs
- the elements to be validated.
-
noNulls
Throws an IllegalArgumentException if the given array is null,empty, or contains null values- Type Parameters:
T
- Type of arguments to be validated- Parameters:
argDescription
- the description of the elementsargs
- the elements to be validated.
-
indexOf
public static int indexOf(NormalizedString[] array, NormalizedString element, FieldSelector fieldSelector) Returns the index of a header, when headers are selected using aFieldSelector
.- Parameters:
array
- the element arrayelement
- the element to be looked for in the array.fieldSelector
- a field selector that indicates which elements of the given array are selected.- Returns:
- the index of the given element in the array, or -1 if the element could not be found.
-
indexesOf
Returns the indexes of an element in a given array.- Parameters:
array
- the element arrayelement
- the element to be looked for in the array.- Returns:
- the indexes of the given element in the array, or an empty array if no element could be found
-
indexOf
Returns the index of an element in a given array.- Parameters:
array
- the element arrayelement
- the element to be looked for in the array.- Returns:
- the index of the given element in the array, or -1 if the element could not be found.
-
indexOf
public static int indexOf(char[] array, char element, int from) Returns the index of a character in a given array.- Parameters:
array
- the character arrayelement
- the character to be looked for in the array.from
- the starting position of the array from where to start the search- Returns:
- the index of the given character in the array, or -1 if the character could not be found.
-
indexOf
Returns the index of an element in a given array.- Parameters:
array
- the element arrayelement
- the element to be looked for in the array.from
- the starting position of the array from where to start the search- Returns:
- the index of the given element in the array, or -1 if the element could not be found.
-
findMissingElements
Searches for elements in a given array and returns the elements not found.- Parameters:
array
- An array with elementselements
- the elements to be found- Returns:
- the elements not found in the array.
-
findMissingElements
Searches for elements in a given array and returns the elements not found.- Parameters:
array
- An array with elementselements
- the elements to be found- Returns:
- the elements not found in the array.
-
newWriter
Creates aWriter
from an output stream- Parameters:
output
- the output stream- Returns:
Writer
wrapping the given output stream
-
newWriter
Creates aWriter
from an output stream- Parameters:
output
- the output streamencoding
- the encoding to use when writing to the output stream- Returns:
Writer
wrapping the given output stream
-
newWriter
Creates aWriter
from an output stream- Parameters:
output
- the output streamencoding
- the encoding to use when writing to the output stream- Returns:
Writer
wrapping the given output stream
-
newWriter
Creates aWriter
from a file- Parameters:
file
- the file to be written- Returns:
Writer
for the given file
-
newWriter
Creates aWriter
from a file- Parameters:
file
- the file to be writtenencoding
- the encoding to use when writing to the file- Returns:
Writer
for the given file
-
newWriter
Creates aWriter
from a file- Parameters:
file
- the file to be writtenencoding
- the encoding to use when writing to the file- Returns:
Writer
for the given file
-
newReader
Creates aReader
from an input stream- Parameters:
input
- the input stream- Returns:
- a
Reader
wrapping the given input stream
-
newReader
Creates aReader
from an input stream- Parameters:
input
- the input streamencoding
- the encoding to use when reading from the input stream- Returns:
- a
Reader
wrapping the given input stream
-
newReader
Creates aReader
from an input stream- Parameters:
input
- the input streamencoding
- the encoding to use when reading from the input stream- Returns:
- a
Reader
wrapping the given input stream
-
newReader
Creates aReader
for a given a file- Parameters:
file
- the file to be read- Returns:
- a
Reader
for reading the given file
-
newReader
Creates aReader
for a given a file- Parameters:
file
- the file to be readencoding
- the encoding to be used when reading from the file- Returns:
- a
Reader
for reading the given file
-
newReader
Creates aReader
for a given a file- Parameters:
file
- the file to be readencoding
- the encoding to be used when reading from the file- Returns:
- a
Reader
for reading the given file
-
toArray
Converts a list of enumerations to an array of theirEnum.toString()
representation- Parameters:
enums
- a list of enumerations to convert- Returns:
- an array of
String
with the values produced by each element'sEnum.toString()
method.
-
toIntArray
Converts any collection ofInteger
into anint
array.- Parameters:
ints
- a collection of (boxed) integers.- Returns:
- a primitive
int
array with the unboxed integer values.
-
toCharArray
Converts any collection ofCharacter
into a char array.- Parameters:
characters
- a collection of (boxed) characters.- Returns:
- a primitive
char
array with the unboxed character values.
-
restrictContent
Restricts the length of a given content.- Parameters:
length
- the maximum length to be displayed. If0
, the"<omitted>"
string will be returned.content
- the content whose length should be restricted.- Returns:
- the restricted content.
-
restrictContent
Restricts the length of a given content.- Parameters:
length
- the maximum length to be displayed. If0
, the"<omitted>"
string will be returned.content
- the content whose length should be restricted.- Returns:
- the restricted content.
-
throwUnchecked
Allows rethrowing a checked exception instead of wrapping it into a runtime exception. For internal use only as this generally causes more trouble than it solves (your exception-specific catch statement may not catch this error - make sure you are catching a Throwable)- Parameters:
error
- the (potentially checked) exception to the thrown.
-
throwsUnchecked
- Throws:
T extends Exception
-
toByteArray
public static byte[] toByteArray(int... ints) Converts a sequence of int numbers into a byte array.- Parameters:
ints
- the integers to be cast to by- Returns:
- the resulting byte array.
-
findDuplicates
public static <T> T[] findDuplicates(T[] array) Identifies duplicate values in a given array and returns them- Type Parameters:
T
- the type of elements held in the given array.- Parameters:
array
- the search array- Returns:
- all duplicate values found in the given array, or empty array if no duplicates, or
null
if the input isnull
.
-
trim
Removes surrounding spaces from a givenString
, from its right or left side, or both.- Parameters:
input
- the content to trimleft
- flag to indicate whether spaces on the left side of the string should be removed.right
- flag to indicate whether spaces on the right side of the string should be removed.- Returns:
- the trimmed string.
-
displayLineSeparators
Displays line separators in a string by replacing all instances of `\r` and `\n` with `[cr]` and `[lf]`. If `\r` is followed by `\n` or vice versa, then `[crlf]` or `[lfcr]` will be printed.- Parameters:
str
- the string to have its line separators displayedaddNewLine
- flag indicating whether the original `\r` or `\n` characters should be kept in the string. iftrue
, `\r` will be replaced by `[cr]\r` for example.- Returns:
- the updated string with any line separators replaced by visible character sequences.
-
removeAll
public static int[] removeAll(int[] array, int e) Removes all instances of a given element from an int array.- Parameters:
array
- the array to be checkede
- the element to be removed- Returns:
- an updated array that does not contain the given element anywhere, or the original array if the element has not been found.
-