Package com.univocity.parsers.common
Class ArgumentUtils
- java.lang.Object
-
- com.univocity.parsers.common.ArgumentUtils
-
public class ArgumentUtils extends java.lang.ObjectAn utility class for validating inputs.- Author:
- uniVocity Software Pty Ltd - parsers@univocity.com
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String[]EMPTY_STRING_ARRAYAn empty String array.
-
Constructor Summary
Constructors Constructor Description ArgumentUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Object[]findMissingElements(java.lang.Object[] array, java.lang.Object[] elements)Searches for elements in a given array and returns the elements not found.static java.lang.Object[]findMissingElements(java.lang.Object[] array, java.util.Collection<?> elements)Searches for elements in a given array and returns the elements not found.static intindexOf(java.lang.Object[] array, java.lang.Object element)Returns the index of an element in a given array.static intindexOf(java.lang.String[] array, java.lang.String element, FieldSelector fieldSelector)Returns the index of a header, when headers are selected using aFieldSelector.static java.io.ReadernewReader(java.io.File file)Creates aReaderfor a given a filestatic java.io.ReadernewReader(java.io.File file, java.lang.String encoding)Creates aReaderfor a given a filestatic java.io.ReadernewReader(java.io.File file, java.nio.charset.Charset encoding)Creates aReaderfor a given a filestatic java.io.ReadernewReader(java.io.InputStream input)Creates aReaderfrom an input streamstatic java.io.ReadernewReader(java.io.InputStream input, java.lang.String encoding)Creates aReaderfrom an input streamstatic java.io.ReadernewReader(java.io.InputStream input, java.nio.charset.Charset encoding)Creates aReaderfrom an input streamstatic java.io.WriternewWriter(java.io.File file)Creates aWriterfrom a filestatic java.io.WriternewWriter(java.io.File file, java.lang.String encoding)Creates aWriterfrom a filestatic java.io.WriternewWriter(java.io.File file, java.nio.charset.Charset encoding)Creates aWriterfrom a filestatic java.io.WriternewWriter(java.io.OutputStream output)Creates aWriterfrom an output streamstatic java.io.WriternewWriter(java.io.OutputStream output, java.lang.String encoding)Creates aWriterfrom an output streamstatic java.io.WriternewWriter(java.io.OutputStream output, java.nio.charset.Charset encoding)Creates aWriterfrom an output streamstatic <T> voidnoNulls(java.lang.String argDescription, T... args)Throws an IllegalArgumentException if the given array is null,empty, or contains null valuesstatic java.lang.Stringnormalize(java.lang.String string)Normalizes a given String by trimming whitespaces and converting it to lower case.static java.lang.String[]normalize(java.lang.String[] strings)Normalizes the Strings in a given array by trimming all elements and converting them to lower case.static voidnormalize(java.util.Collection<java.lang.String> strings)Normalizes the Strings in a given array by trimming all elements and converting them to lower case.static <T> voidnotEmpty(java.lang.String argDescription, T... args)Throws an IllegalArgumentException if the given array is null or empty.static java.lang.StringrestrictContent(int length, java.lang.CharSequence content)static java.lang.StringrestrictContent(int length, java.lang.Object content)static voidthrowUnchecked(java.lang.Throwable error)Allows rethrowing a checked exception instead of wrapping it into a runtime exception.static java.lang.String[]toArray(java.util.List<java.lang.Enum> enums)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(java.util.Collection<java.lang.Character> characters)Converts any collection ofCharacterinto a char array.static int[]toIntArray(java.util.Collection<java.lang.Integer> ints)Converts any collection ofIntegerinto anintarray.
-
-
-
Method Detail
-
notEmpty
public static <T> void notEmpty(java.lang.String argDescription, T... args)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
public static <T> void noNulls(java.lang.String argDescription, T... args)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(java.lang.String[] array, java.lang.String 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.
-
indexOf
public static int indexOf(java.lang.Object[] array, java.lang.Object element)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.
-
findMissingElements
public static java.lang.Object[] findMissingElements(java.lang.Object[] array, java.util.Collection<?> elements)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
public static java.lang.Object[] findMissingElements(java.lang.Object[] array, java.lang.Object[] elements)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.
-
normalize
public static java.lang.String[] normalize(java.lang.String[] strings)
Normalizes the Strings in a given array by trimming all elements and converting them to lower case.- Parameters:
strings- a String array with elements to be normalized.- Returns:
- the normalized version of the original string array.
-
normalize
public static java.lang.String normalize(java.lang.String string)
Normalizes a given String by trimming whitespaces and converting it to lower case.- Parameters:
string- a String to be normalized.- Returns:
- the normalized version of the original String.
-
normalize
public static void normalize(java.util.Collection<java.lang.String> strings)
Normalizes the Strings in a given array by trimming all elements and converting them to lower case.- Parameters:
strings- a String collection with elements to be normalized. The original contents of the collection will be modified.
-
newWriter
public static java.io.Writer newWriter(java.io.OutputStream output)
Creates aWriterfrom an output stream- Parameters:
output- the output stream- Returns:
Writerwrapping the given output stream
-
newWriter
public static java.io.Writer newWriter(java.io.OutputStream output, java.lang.String encoding)Creates aWriterfrom an output stream- Parameters:
output- the output streamencoding- the encoding to use when writing to the output stream- Returns:
Writerwrapping the given output stream
-
newWriter
public static java.io.Writer newWriter(java.io.OutputStream output, java.nio.charset.Charset encoding)Creates aWriterfrom an output stream- Parameters:
output- the output streamencoding- the encoding to use when writing to the output stream- Returns:
Writerwrapping the given output stream
-
newWriter
public static java.io.Writer newWriter(java.io.File file)
Creates aWriterfrom a file- Parameters:
file- the file to be written- Returns:
Writerfor the given file
-
newWriter
public static java.io.Writer newWriter(java.io.File file, java.lang.String encoding)Creates aWriterfrom a file- Parameters:
file- the file to be writtenencoding- the encoding to use when writing to the file- Returns:
Writerfor the given file
-
newWriter
public static java.io.Writer newWriter(java.io.File file, java.nio.charset.Charset encoding)Creates aWriterfrom a file- Parameters:
file- the file to be writtenencoding- the encoding to use when writing to the file- Returns:
Writerfor the given file
-
newReader
public static java.io.Reader newReader(java.io.InputStream input)
Creates aReaderfrom an input stream- Parameters:
input- the input stream- Returns:
- a
Readerwrapping the given input stream
-
newReader
public static java.io.Reader newReader(java.io.InputStream input, java.lang.String encoding)Creates aReaderfrom an input stream- Parameters:
input- the input streamencoding- the encoding to use when reading from the input stream- Returns:
- a
Readerwrapping the given input stream
-
newReader
public static java.io.Reader newReader(java.io.InputStream input, java.nio.charset.Charset encoding)Creates aReaderfrom an input stream- Parameters:
input- the input streamencoding- the encoding to use when reading from the input stream- Returns:
- a
Readerwrapping the given input stream
-
newReader
public static java.io.Reader newReader(java.io.File file)
Creates aReaderfor a given a file- Parameters:
file- the file to be read- Returns:
- a
Readerfor reading the given file
-
newReader
public static java.io.Reader newReader(java.io.File file, java.lang.String encoding)Creates aReaderfor a given a file- Parameters:
file- the file to be readencoding- the encoding to be used when reading from the file- Returns:
- a
Readerfor reading the given file
-
newReader
public static java.io.Reader newReader(java.io.File file, java.nio.charset.Charset encoding)Creates aReaderfor a given a file- Parameters:
file- the file to be readencoding- the encoding to be used when reading from the file- Returns:
- a
Readerfor reading the given file
-
toArray
public static java.lang.String[] toArray(java.util.List<java.lang.Enum> enums)
Converts a list of enumerations to an array of theirEnum.toString()representation- Parameters:
enums- a list of enumerations to convert- Returns:
- an array of
Stringwith the values produced by each element'sEnum.toString()method.
-
toIntArray
public static int[] toIntArray(java.util.Collection<java.lang.Integer> ints)
Converts any collection ofIntegerinto anintarray.- Parameters:
ints- a collection of (boxed) integers.- Returns:
- a primitive
intarray with the unboxed integer values.
-
toCharArray
public static char[] toCharArray(java.util.Collection<java.lang.Character> characters)
Converts any collection ofCharacterinto a char array.- Parameters:
characters- a collection of (boxed) characters.- Returns:
- a primitive
chararray with the unboxed character values.
-
restrictContent
public static java.lang.String restrictContent(int length, java.lang.CharSequence content)
-
restrictContent
public static java.lang.String restrictContent(int length, java.lang.Object content)
-
throwUnchecked
public static void throwUnchecked(java.lang.Throwable error)
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.
-
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.
-
-