View Javadoc
1   package com.opencsv.bean.concurrent;
2   
3   import com.opencsv.bean.MappingStrategy;
4   import com.opencsv.bean.exceptionhandler.CsvExceptionHandler;
5   
6   import java.util.Locale;
7   
8   /**
9    * A specific derivative of {@link IntolerantThreadPoolExecutor} intended for
10   * submitting beans to be converted to {@link java.lang.String}s for writing.
11   *
12   * @param <T> The type of the bean being converted
13   * @author Andrew Rucker Jones
14   * @since 5.0
15   */
16  public class BeanExecutor<T> extends IntolerantThreadPoolExecutor<String[]> {
17  
18      /**
19       * The only constructor available for this class.
20       * @param orderedResults Whether order should be preserved in the results
21       * @param errorLocale The locale to use for error messages
22       */
23      public BeanExecutor(boolean orderedResults, Locale errorLocale) {
24          super(orderedResults, errorLocale);
25      }
26  
27      /**
28       * Submit one bean for conversion.
29       *
30       * @param lineNumber Which record in the output file is being processed
31       * @param mappingStrategy The mapping strategy to be used
32       * @param bean The bean to be transformed into a line of output
33       * @param exceptionHandler The handler for exceptions thrown during record
34       *                         processing
35       */
36      public void submitBean(
37              long lineNumber, MappingStrategy<T> mappingStrategy,
38              T bean, CsvExceptionHandler exceptionHandler) {
39          if (accumulateThread != null) {
40              expectedRecords.add(lineNumber);
41          }
42          try {
43              execute(new ProcessCsvBean<>(lineNumber, mappingStrategy, bean, resultQueue,
44                      thrownExceptionsQueue, expectedRecords, exceptionHandler));
45          } catch (Exception e) {
46              if(accumulateThread != null) {
47                  expectedRecords.remove(lineNumber);
48                  accumulateThread.setMustStop(true);
49              }
50              throw e;
51          }
52      }
53  }