Top Description Constructors Methods
com.opencsv.bean.util

public final Class OpencsvUtils

extends Object
Class Inheritance
Imports
com.opencsv.ICSVParser, com.opencsv.bean.*, com.opencsv.bean.exceptionhandler.CsvExceptionHandler, com.opencsv.exceptions.CsvBadConverterException, .CsvChainedException, .CsvException, org.apache.commons.lang3.StringUtils, org.apache.commons.lang3.reflect.FieldUtils, java.util.*, java.util.concurrent.BlockingQueue, java.util.regex.Matcher, .Pattern, .PatternSyntaxException, java.util.stream.Stream

This class is meant to be a collection of general purpose static methods useful in internal processing for opencsv.
Author
Andrew Rucker Jones
Since
3.9

Constructor Summary

AccessConstructor and Description
private
OpencsvUtils()

This class can't be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static Pattern

Returns:

A compiled pattern, or null if the input was null or empty
compilePattern
(String
The regular expression to be compiled. May be null or an empty string, in which case null is returned.
regex
,
int
Flags for compiling the regular expression, as in java.util.regex.Pattern#compile(String, int).
regexFlags
,
Class<?>
The class from which this method is being called. Used for generating helpful exceptions.
callingClass
,
Locale
The locale to be used for error messages. If null, the default locale is used.
errorLocale
)

Compiles a regular expression into a java.util.regex.Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid.

public static Pattern

Returns:

A compiled pattern, or null if the input was null or empty
compilePatternAtLeastOneGroup
(String
The regular expression to be compiled. May be null or an empty string, in which case null is returned. Must have at least one capturing group if not null or empty.
regex
,
int
Flags for compiling the regular expression, as in java.util.regex.Pattern#compile(String, int).
regexFlags
,
Class<?>
The class from which this method is being called. Used for generating helpful exceptions.
callingClass
,
Locale
The locale to be used for error messages. If null, the default locale is used.
errorLocale
)

Compiles a regular expression into a java.util.regex.Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid, or if it does not have at least one capturing group.

public static <
The type of the bean for which the mapping strategy is sought
T
>
MappingStrategy<T>

Returns:

A functional mapping strategy for the bean in question
determineMappingStrategy
(Class<? extends T>
The class of the bean for which the mapping strategy is sought
type
,
Locale
The locale to use for all error messages. If null, the default locale is used.
errorLocale
,
String
The profile to use when configuring bean fields
profile
)

Determines which mapping strategy is appropriate for this bean.

public static synchronized void
handleException(CsvException
The exception originally thrown
e
,
long
The line or record number that caused the exception
lineNumber
,
CsvExceptionHandler
The exception handler
exceptionHandler
,
BlockingQueue<OrderedObject<CsvException>>
The queue for captured exceptions
queue
)

A function to consolidate code common to handling exceptions thrown during reading or writing of CSV files.

public static <
The type of the object to be queued
E
>
void
queueRefuseToAcceptDefeat(BlockingQueue<E>
The queue the object should be added to
queue
,
E
The object to be queued
object
)

I find it annoying that when I want to queue something in a blocking queue, the thread might be interrupted and I have to try again; this method fixes that.

public static void
verifyFormatString(String format, Class<?>
The class from which this method is being called. Used for generating helpful exceptions.
callingClass
,
Locale
The locale to be used for error messages. If null, the default locale is used.
errorLocale
)

Verifies that the given format string works with one string parameter.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

OpencsvUtilsback to summary
private OpencsvUtils()

This class can't be instantiated.

Method Detail

compilePatternback to summary
public static Pattern compilePattern(String regex, int regexFlags, Class<?> callingClass, Locale errorLocale) throws CsvBadConverterException

Compiles a regular expression into a java.util.regex.Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid. This method may be used by custom converters if they are required to compile regular expressions that are unknown at compile time.

Parameters
regex:String

The regular expression to be compiled. May be null or an empty string, in which case null is returned.

regexFlags:int

Flags for compiling the regular expression, as in java.util.regex.Pattern#compile(String, int).

callingClass:Class<?>

The class from which this method is being called. Used for generating helpful exceptions.

errorLocale:Locale

The locale to be used for error messages. If null, the default locale is used.

Returns:Pattern

A compiled pattern, or null if the input was null or empty

Exceptions
CsvBadConverterException:
If the regular expression is not empty but invalid
Since
4.3
compilePatternAtLeastOneGroupback to summary
public static Pattern compilePatternAtLeastOneGroup(String regex, int regexFlags, Class<?> callingClass, Locale errorLocale) throws CsvBadConverterException

Compiles a regular expression into a java.util.regex.Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid, or if it does not have at least one capturing group.

Parameters
regex:String

The regular expression to be compiled. May be null or an empty string, in which case null is returned. Must have at least one capturing group if not null or empty.

regexFlags:int

Flags for compiling the regular expression, as in java.util.regex.Pattern#compile(String, int).

callingClass:Class<?>

The class from which this method is being called. Used for generating helpful exceptions.

errorLocale:Locale

The locale to be used for error messages. If null, the default locale is used.

Returns:Pattern

A compiled pattern, or null if the input was null or empty

Exceptions
CsvBadConverterException:
If the regular expression is not empty but invalid or valid but does not have at least one capturing group
Since
4.3
determineMappingStrategyback to summary
public static <T> MappingStrategy<T> determineMappingStrategy(Class<? extends T> type, Locale errorLocale, String profile)

Determines which mapping strategy is appropriate for this bean. The algorithm is:

  1. If annotations CsvBindByPosition, CsvCustomBindByPosition, CsvBindAndSplitByPosition or CsvBindAndJoinByPosition are present, ColumnPositionMappingStrategy is chosen.
  2. Otherwise, HeaderColumnNameMappingStrategy is chosen. If annotations are present, they will be used, otherwise the field names will be used as the column names.
Parameters
<T>
The type of the bean for which the mapping strategy is sought
type:Class<? extends T>

The class of the bean for which the mapping strategy is sought

errorLocale:Locale

The locale to use for all error messages. If null, the default locale is used.

profile:String

The profile to use when configuring bean fields

Returns:MappingStrategy<T>

A functional mapping strategy for the bean in question

handleExceptionback to summary
public static synchronized void handleException(CsvException e, long lineNumber, CsvExceptionHandler exceptionHandler, BlockingQueue<OrderedObject<CsvException>> queue)

A function to consolidate code common to handling exceptions thrown during reading or writing of CSV files. The proper line number is set for the exception, the exception handler is run, and the exception is queued or thrown as necessary.

Parameters
e:CsvException

The exception originally thrown

lineNumber:long

The line or record number that caused the exception

exceptionHandler:CsvExceptionHandler

The exception handler

queue:BlockingQueue<OrderedObject<CsvException>>

The queue for captured exceptions

Since
5.2
queueRefuseToAcceptDefeatback to summary
public static <E> void queueRefuseToAcceptDefeat(BlockingQueue<E> queue, E object)

I find it annoying that when I want to queue something in a blocking queue, the thread might be interrupted and I have to try again; this method fixes that.

Parameters
<E>
The type of the object to be queued
queue:BlockingQueue<E>

The queue the object should be added to

object:E

The object to be queued

Since
4.0
verifyFormatStringback to summary
public static void verifyFormatString(String format, Class<?> callingClass, Locale errorLocale)

Verifies that the given format string works with one string parameter.

Parameters
format:String

A format string for java.lang.String#format(String, Object...)

callingClass:Class<?>

The class from which this method is being called. Used for generating helpful exceptions.

errorLocale:Locale

The locale to be used for error messages. If null, the default locale is used.