Package com.opencsv.bean.util
Class OpencsvUtils
- java.lang.Object
-
- com.opencsv.bean.util.OpencsvUtils
-
public final class OpencsvUtils extends java.lang.Object
This class is meant to be a collection of general purpose static methods useful in internal processing for opencsv.- Since:
- 3.9
-
-
Constructor Summary
Constructors Modifier Constructor Description private
OpencsvUtils()
This class can't be instantiated.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.regex.Pattern
compilePattern(java.lang.String regex, int regexFlags, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
Compiles a regular expression into aPattern
, throwing an exception that is proper in the context of opencsv if the regular expression is not valid.static java.util.regex.Pattern
compilePatternAtLeastOneGroup(java.lang.String regex, int regexFlags, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
Compiles a regular expression into aPattern
, 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.static <T> MappingStrategy<T>
determineMappingStrategy(java.lang.Class<? extends T> type, java.util.Locale errorLocale, java.lang.String profile)
Determines which mapping strategy is appropriate for this bean.static void
handleException(CsvException e, long lineNumber, CsvExceptionHandler exceptionHandler, java.util.concurrent.BlockingQueue<OrderedObject<CsvException>> queue)
A function to consolidate code common to handling exceptions thrown during reading or writing of CSV files.static <E> void
queueRefuseToAcceptDefeat(java.util.concurrent.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.static void
verifyFormatString(java.lang.String format, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
Verifies that the given format string works with one string parameter.
-
-
-
Method Detail
-
determineMappingStrategy
public static <T> MappingStrategy<T> determineMappingStrategy(java.lang.Class<? extends T> type, java.util.Locale errorLocale, java.lang.String profile)
Determines which mapping strategy is appropriate for this bean. The algorithm is:- If annotations
CsvBindByPosition
,CsvCustomBindByPosition
,CsvBindAndSplitByPosition
orCsvBindAndJoinByPosition
are present,ColumnPositionMappingStrategy
is chosen. - Otherwise,
HeaderColumnNameMappingStrategy
is chosen. If annotations are present, they will be used, otherwise the field names will be used as the column names.
- Type Parameters:
T
- The type of the bean for which the mapping strategy is sought- Parameters:
type
- The class of the bean for which the mapping strategy is soughterrorLocale
- The locale to use for all error messages. If null, the default locale is used.profile
- The profile to use when configuring bean fields- Returns:
- A functional mapping strategy for the bean in question
- If annotations
-
queueRefuseToAcceptDefeat
public static <E> void queueRefuseToAcceptDefeat(java.util.concurrent.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.- Type Parameters:
E
- The type of the object to be queued- Parameters:
queue
- The queue the object should be added toobject
- The object to be queued- Since:
- 4.0
-
handleException
public static void handleException(CsvException e, long lineNumber, CsvExceptionHandler exceptionHandler, java.util.concurrent.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
- The exception originally thrownlineNumber
- The line or record number that caused the exceptionexceptionHandler
- The exception handlerqueue
- The queue for captured exceptions- Since:
- 5.2
-
compilePatternAtLeastOneGroup
public static java.util.regex.Pattern compilePatternAtLeastOneGroup(java.lang.String regex, int regexFlags, java.lang.Class<?> callingClass, java.util.Locale errorLocale) throws CsvBadConverterException
Compiles a regular expression into aPattern
, 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
- The regular expression to be compiled. May benull
or an empty string, in which casenull
is returned. Must have at least one capturing group if notnull
or empty.regexFlags
- Flags for compiling the regular expression, as inPattern.compile(String, int)
.callingClass
- The class from which this method is being called. Used for generating helpful exceptions.errorLocale
- The locale to be used for error messages. Ifnull
, the default locale is used.- Returns:
- A compiled pattern, or
null
if the input was null or empty - Throws:
CsvBadConverterException
- If the regular expression is not empty but invalid or valid but does not have at least one capturing group- Since:
- 4.3
-
compilePattern
public static java.util.regex.Pattern compilePattern(java.lang.String regex, int regexFlags, java.lang.Class<?> callingClass, java.util.Locale errorLocale) throws CsvBadConverterException
Compiles a regular expression into aPattern
, 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
- The regular expression to be compiled. May benull
or an empty string, in which casenull
is returned.regexFlags
- Flags for compiling the regular expression, as inPattern.compile(String, int)
.callingClass
- The class from which this method is being called. Used for generating helpful exceptions.errorLocale
- The locale to be used for error messages. Ifnull
, the default locale is used.- Returns:
- A compiled pattern, or
null
if the input was null or empty - Throws:
CsvBadConverterException
- If the regular expression is not empty but invalid- Since:
- 4.3
-
verifyFormatString
public static void verifyFormatString(java.lang.String format, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
Verifies that the given format string works with one string parameter.- Parameters:
format
- A format string forString.format(String, Object...)
callingClass
- The class from which this method is being called. Used for generating helpful exceptions.errorLocale
- The locale to be used for error messages. Ifnull
, the default locale is used.
-
-