Class OpencsvUtils

java.lang.Object
com.opencsv.bean.util.OpencsvUtils

public final class OpencsvUtils extends Object
This class is meant to be a collection of general purpose static methods useful in internal processing for opencsv.
Since:
3.9
  • Constructor Details

    • OpencsvUtils

      private OpencsvUtils()
      This class can't be instantiated.
  • Method Details

    • determineMappingStrategy

      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.
      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 sought
      errorLocale - 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
    • queueRefuseToAcceptDefeat

      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.
      Type Parameters:
      E - The type of the object to be queued
      Parameters:
      queue - The queue the object should be added to
      object - The object to be queued
      Since:
      4.0
    • handleException

      public static 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 - The exception originally thrown
      lineNumber - The line or record number that caused the exception
      exceptionHandler - The exception handler
      queue - The queue for captured exceptions
      Since:
      5.2
    • compilePatternAtLeastOneGroup

      public static Pattern compilePatternAtLeastOneGroup(String regex, int regexFlags, Class<?> callingClass, Locale errorLocale) throws CsvBadConverterException
      Compiles a regular expression into a 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 - 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 - Flags for compiling the regular expression, as in Pattern.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. If null, 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 Pattern compilePattern(String regex, int regexFlags, Class<?> callingClass, Locale errorLocale) throws CsvBadConverterException
      Compiles a regular expression into a 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 - The regular expression to be compiled. May be null or an empty string, in which case null is returned.
      regexFlags - Flags for compiling the regular expression, as in Pattern.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. If null, 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(String format, Class<?> callingClass, Locale errorLocale)
      Verifies that the given format string works with one string parameter.
      Parameters:
      format - A format string for String.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. If null, the default locale is used.