Top Description Fields Constructors Methods
com.univocity.parsers.common

public Class ArgumentUtils

extends Object
Class Inheritance
Imports
com.univocity.parsers.common.fields.*, com.univocity.parsers.common.input.*, java.io.*, java.nio.charset.*, java.util.*

An utility class for validating inputs.
Author
Univocity Software Pty Ltd - [email protected]

Field Summary

Modifier and TypeField and Description
public static final NormalizedString[]
public static final String[]
EMPTY_STRING_ARRAY

An empty String array.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the updated string with any line separators replaced by visible character sequences.
displayLineSeparators
(String
the string to have its line separators displayed
str
,
boolean
flag indicating whether the original `\r` or `\n` characters should be kept in the string. if true, `\r` will be replaced by `[cr]\r` for example.
addNewLine
)

Displays line separators in a string by replacing all instances of `\r` and `\n` with `[cr]` and `[lf]`.

public static <
the type of elements held in the given array.
T
>
T[]

Returns:

all duplicate values found in the given array, or empty array if no duplicates, or null if the input is null.
findDuplicates
(T[]
the search array
array
)

Identifies duplicate values in a given array and returns them

public static Object[]

Returns:

the elements not found in the array.
findMissingElements
(Object[]
An array with elements
array
,
Collection<?>
the elements to be found
elements
)

Searches for elements in a given array and returns the elements not found.

public static Object[]

Returns:

the elements not found in the array.
findMissingElements
(Object[]
An array with elements
array
,
Object[]
the elements to be found
elements
)

Searches for elements in a given array and returns the elements not found.

public static int[]

Returns:

the indexes of the given element in the array, or an empty array if no element could be found
indexesOf
(Object[]
the element array
array
,
Object
the element to be looked for in the array.
element
)

Returns the indexes of an element in a given array.

public static int

Returns:

the index of the given element in the array, or -1 if the element could not be found.
indexOf
(NormalizedString[]
the element array
array
,
NormalizedString
the element to be looked for in the array.
element
,
FieldSelector
a field selector that indicates which elements of the given array are selected.
fieldSelector
)

Returns the index of a header, when headers are selected using a FieldSelector.

public static int

Returns:

the index of the given element in the array, or -1 if the element could not be found.
indexOf
(Object[]
the element array
array
,
Object
the element to be looked for in the array.
element
)

Returns the index of an element in a given array.

public static int

Returns:

the index of the given character in the array, or -1 if the character could not be found.
indexOf
(char[]
the character array
array
,
char
the character to be looked for in the array.
element
,
int
the starting position of the array from where to start the search
from
)

Returns the index of a character in a given array.

private static int

Returns:

the index of the given element in the array, or -1 if the element could not be found.
indexOf
(Object[]
the element array
array
,
Object
the element to be looked for in the array.
element
,
int
the starting position of the array from where to start the search
from
)

Returns the index of an element in a given array.

public static Reader

Returns:

a java.io.Reader wrapping the given input stream
newReader
(InputStream
the input stream
input
)

Creates a java.io.Reader from an input stream

public static Reader

Returns:

a java.io.Reader wrapping the given input stream
newReader
(InputStream
the input stream
input
,
String
the encoding to use when reading from the input stream
encoding
)

Creates a java.io.Reader from an input stream

public static Reader

Returns:

a java.io.Reader wrapping the given input stream
newReader
(InputStream
the input stream
input
,
Charset
the encoding to use when reading from the input stream
encoding
)

Creates a java.io.Reader from an input stream

public static Reader

Returns:

a java.io.Reader for reading the given file
newReader
(File
the file to be read
file
)

Creates a java.io.Reader for a given a file

public static Reader

Returns:

a java.io.Reader for reading the given file
newReader
(File
the file to be read
file
,
String
the encoding to be used when reading from the file
encoding
)

Creates a java.io.Reader for a given a file

public static Reader

Returns:

a java.io.Reader for reading the given file
newReader
(File
the file to be read
file
,
Charset
the encoding to be used when reading from the file
encoding
)

Creates a java.io.Reader for a given a file

public static Writer

Returns:

java.io.Writer wrapping the given output stream
newWriter
(OutputStream
the output stream
output
)

Creates a java.io.Writer from an output stream

public static Writer

Returns:

java.io.Writer wrapping the given output stream
newWriter
(OutputStream
the output stream
output
,
String
the encoding to use when writing to the output stream
encoding
)

Creates a java.io.Writer from an output stream

public static Writer

Returns:

java.io.Writer wrapping the given output stream
newWriter
(OutputStream
the output stream
output
,
Charset
the encoding to use when writing to the output stream
encoding
)

Creates a java.io.Writer from an output stream

public static Writer

Returns:

java.io.Writer for the given file
newWriter
(File
the file to be written
file
)

Creates a java.io.Writer from a file

public static Writer

Returns:

java.io.Writer for the given file
newWriter
(File
the file to be written
file
,
String
the encoding to use when writing to the file
encoding
)

Creates a java.io.Writer from a file

public static Writer

Returns:

java.io.Writer for the given file
newWriter
(File
the file to be written
file
,
Charset
the encoding to use when writing to the file
encoding
)

Creates a java.io.Writer from a file

public static <
Type of arguments to be validated
T
>
void
noNulls(String
the description of the elements
argDescription
,
T...
the elements to be validated.
args
)

Throws an IllegalArgumentException if the given array is null,empty, or contains null values

public static <
Type of arguments to be validated
T
>
void
notEmpty(String
the description of the elements
argDescription
,
T...
the elements to be validated.
args
)

Throws an IllegalArgumentException if the given array is null or empty.

public static int[]

Returns:

an updated array that does not contain the given element anywhere, or the original array if the element has not been found.
removeAll
(int[]
the array to be checked
array
,
int
the element to be removed
e
)

Removes all instances of a given element from an int array.

public static String

Returns:

the restricted content.
restrictContent
(int
the maximum length to be displayed. If 0, the "<omitted>" string will be returned.
length
,
CharSequence
the content whose length should be restricted.
content
)

Restricts the length of a given content.

public static String

Returns:

the restricted content.
restrictContent
(int
the maximum length to be displayed. If 0, the "<omitted>" string will be returned.
length
,
Object
the content whose length should be restricted.
content
)

Restricts the length of a given content.

private static <T extends Exception> void
public static void
throwUnchecked(Throwable
the (potentially checked) exception to the thrown.
error
)

Allows rethrowing a checked exception instead of wrapping it into a runtime exception.

public static String[]

Returns:

an array of String with the values produced by each element's Enum#toString() method.
toArray
(List<Enum<E extends Enum<E>>>
a list of enumerations to convert
enums
)

Converts a list of enumerations to an array of their Enum#toString() representation

public static byte[]

Returns:

the resulting byte array.
toByteArray
(int...
the integers to be cast to by
ints
)

Converts a sequence of int numbers into a byte array.

public static char[]

Returns:

a primitive char array with the unboxed character values.
toCharArray
(Collection<Character>
a collection of (boxed) characters.
characters
)

Converts any collection of Character into a char array.

public static int[]

Returns:

a primitive int array with the unboxed integer values.
toIntArray
(Collection<Integer>
a collection of (boxed) integers.
ints
)

Converts any collection of Integer into an int array.

public static String

Returns:

the trimmed string.
trim
(String
the content to trim
input
,
boolean
flag to indicate whether spaces on the left side of the string should be removed.
left
,
boolean
flag to indicate whether spaces on the right side of the string should be removed.
right
)

Removes surrounding spaces from a given String, from its right or left side, or both.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_NORMALIZED_STRING_ARRAYback to summary
public static final NormalizedString[] EMPTY_NORMALIZED_STRING_ARRAY
EMPTY_STRING_ARRAYback to summary
public static final String[] EMPTY_STRING_ARRAY

An empty String array.

Constructor Detail

ArgumentUtilsback to summary
public ArgumentUtils()

Method Detail

displayLineSeparatorsback to summary
public static String displayLineSeparators(String str, boolean addNewLine)

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:String

the string to have its line separators displayed

addNewLine:boolean

flag indicating whether the original `\r` or `\n` characters should be kept in the string. if true, `\r` will be replaced by `[cr]\r` for example.

Returns:String

the updated string with any line separators replaced by visible character sequences.

findDuplicatesback to summary
public static <T> T[] findDuplicates(T[] array)

Identifies duplicate values in a given array and returns them

Parameters
<T>
the type of elements held in the given array.
array:T[]

the search array

Returns:T[]

all duplicate values found in the given array, or empty array if no duplicates, or null if the input is null.

findMissingElementsback to summary
public static Object[] findMissingElements(Object[] array, Collection<?> elements)

Searches for elements in a given array and returns the elements not found.

Parameters
array:Object[]

An array with elements

elements:Collection<?>

the elements to be found

Returns:Object[]

the elements not found in the array.

findMissingElementsback to summary
public static Object[] findMissingElements(Object[] array, Object[] elements)

Searches for elements in a given array and returns the elements not found.

Parameters
array:Object[]

An array with elements

elements:Object[]

the elements to be found

Returns:Object[]

the elements not found in the array.

indexesOfback to summary
public static int[] indexesOf(Object[] array, Object element)

Returns the indexes of an element in a given array.

Parameters
array:Object[]

the element array

element:Object

the element to be looked for in the array.

Returns:int[]

the indexes of the given element in the array, or an empty array if no element could be found

indexOfback to summary
public static int indexOf(NormalizedString[] array, NormalizedString element, FieldSelector fieldSelector)

Returns the index of a header, when headers are selected using a FieldSelector.

Parameters
array:NormalizedString[]

the element array

element:NormalizedString

the element to be looked for in the array.

fieldSelector:FieldSelector

a field selector that indicates which elements of the given array are selected.

Returns:int

the index of the given element in the array, or -1 if the element could not be found.

indexOfback to summary
public static int indexOf(Object[] array, Object element)

Returns the index of an element in a given array.

Parameters
array:Object[]

the element array

element:Object

the element to be looked for in the array.

Returns:int

the index of the given element in the array, or -1 if the element could not be found.

indexOfback to summary
public static int indexOf(char[] array, char element, int from)

Returns the index of a character in a given array.

Parameters
array:char[]

the character array

element:char

the character to be looked for in the array.

from:int

the starting position of the array from where to start the search

Returns:int

the index of the given character in the array, or -1 if the character could not be found.

indexOfback to summary
private static int indexOf(Object[] array, Object element, int from)

Returns the index of an element in a given array.

Parameters
array:Object[]

the element array

element:Object

the element to be looked for in the array.

from:int

the starting position of the array from where to start the search

Returns:int

the index of the given element in the array, or -1 if the element could not be found.

newReaderback to summary
public static Reader newReader(InputStream input)

Creates a java.io.Reader from an input stream

Parameters
input:InputStream

the input stream

Returns:Reader

a java.io.Reader wrapping the given input stream

newReaderback to summary
public static Reader newReader(InputStream input, String encoding)

Creates a java.io.Reader from an input stream

Parameters
input:InputStream

the input stream

encoding:String

the encoding to use when reading from the input stream

Returns:Reader

a java.io.Reader wrapping the given input stream

newReaderback to summary
public static Reader newReader(InputStream input, Charset encoding)

Creates a java.io.Reader from an input stream

Parameters
input:InputStream

the input stream

encoding:Charset

the encoding to use when reading from the input stream

Returns:Reader

a java.io.Reader wrapping the given input stream

newReaderback to summary
public static Reader newReader(File file)

Creates a java.io.Reader for a given a file

Parameters
file:File

the file to be read

Returns:Reader

a java.io.Reader for reading the given file

newReaderback to summary
public static Reader newReader(File file, String encoding)

Creates a java.io.Reader for a given a file

Parameters
file:File

the file to be read

encoding:String

the encoding to be used when reading from the file

Returns:Reader

a java.io.Reader for reading the given file

newReaderback to summary
public static Reader newReader(File file, Charset encoding)

Creates a java.io.Reader for a given a file

Parameters
file:File

the file to be read

encoding:Charset

the encoding to be used when reading from the file

Returns:Reader

a java.io.Reader for reading the given file

newWriterback to summary
public static Writer newWriter(OutputStream output)

Creates a java.io.Writer from an output stream

Parameters
output:OutputStream

the output stream

Returns:Writer

java.io.Writer wrapping the given output stream

newWriterback to summary
public static Writer newWriter(OutputStream output, String encoding)

Creates a java.io.Writer from an output stream

Parameters
output:OutputStream

the output stream

encoding:String

the encoding to use when writing to the output stream

Returns:Writer

java.io.Writer wrapping the given output stream

newWriterback to summary
public static Writer newWriter(OutputStream output, Charset encoding)

Creates a java.io.Writer from an output stream

Parameters
output:OutputStream

the output stream

encoding:Charset

the encoding to use when writing to the output stream

Returns:Writer

java.io.Writer wrapping the given output stream

newWriterback to summary
public static Writer newWriter(File file)

Creates a java.io.Writer from a file

Parameters
file:File

the file to be written

Returns:Writer

java.io.Writer for the given file

newWriterback to summary
public static Writer newWriter(File file, String encoding)

Creates a java.io.Writer from a file

Parameters
file:File

the file to be written

encoding:String

the encoding to use when writing to the file

Returns:Writer

java.io.Writer for the given file

newWriterback to summary
public static Writer newWriter(File file, Charset encoding)

Creates a java.io.Writer from a file

Parameters
file:File

the file to be written

encoding:Charset

the encoding to use when writing to the file

Returns:Writer

java.io.Writer for the given file

noNullsback to summary
public static <T> void noNulls(String argDescription, T... args)

Throws an IllegalArgumentException if the given array is null,empty, or contains null values

Parameters
<T>
Type of arguments to be validated
argDescription:String

the description of the elements

args:T[]

the elements to be validated.

notEmptyback to summary
public static <T> void notEmpty(String argDescription, T... args)

Throws an IllegalArgumentException if the given array is null or empty.

Parameters
<T>
Type of arguments to be validated
argDescription:String

the description of the elements

args:T[]

the elements to be validated.

removeAllback to summary
public static int[] removeAll(int[] array, int e)

Removes all instances of a given element from an int array.

Parameters
array:int[]

the array to be checked

e:int

the element to be removed

Returns:int[]

an updated array that does not contain the given element anywhere, or the original array if the element has not been found.

restrictContentback to summary
public static String restrictContent(int length, CharSequence content)

Restricts the length of a given content.

Parameters
length:int

the maximum length to be displayed. If 0, the "<omitted>" string will be returned.

content:CharSequence

the content whose length should be restricted.

Returns:String

the restricted content.

restrictContentback to summary
public static String restrictContent(int length, Object content)

Restricts the length of a given content.

Parameters
length:int

the maximum length to be displayed. If 0, the "<omitted>" string will be returned.

content:Object

the content whose length should be restricted.

Returns:String

the restricted content.

throwsUncheckedback to summary
private static <T extends Exception> void throwsUnchecked(Throwable toThrow) throws Throwable-:T
throwUncheckedback to summary
public static void throwUnchecked(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:Throwable

the (potentially checked) exception to the thrown.

toArrayback to summary
public static String[] toArray(List<Enum<E extends Enum<E>>> enums)

Converts a list of enumerations to an array of their Enum#toString() representation

Parameters
enums:List<Enum<E extends Enum<E>>>

a list of enumerations to convert

Returns:String[]

an array of String with the values produced by each element's Enum#toString() method.

Annotations
@SuppressWarnings:rawtypes
toByteArrayback to summary
public static byte[] toByteArray(int... ints)

Converts a sequence of int numbers into a byte array.

Parameters
ints:int[]

the integers to be cast to by

Returns:byte[]

the resulting byte array.

toCharArrayback to summary
public static char[] toCharArray(Collection<Character> characters)

Converts any collection of Character into a char array.

Parameters
characters:Collection<Character>

a collection of (boxed) characters.

Returns:char[]

a primitive char array with the unboxed character values.

toIntArrayback to summary
public static int[] toIntArray(Collection<Integer> ints)

Converts any collection of Integer into an int array.

Parameters
ints:Collection<Integer>

a collection of (boxed) integers.

Returns:int[]

a primitive int array with the unboxed integer values.

trimback to summary
public static String trim(String input, boolean left, boolean right)

Removes surrounding spaces from a given String, from its right or left side, or both.

Parameters
input:String

the content to trim

left:boolean

flag to indicate whether spaces on the left side of the string should be removed.

right:boolean

flag to indicate whether spaces on the right side of the string should be removed.

Returns:String

the trimmed string.