1 package com.opencsv.bean.concurrent;
2
3 import com.opencsv.CSVReader;
4 import com.opencsv.bean.BeanVerifier;
5 import com.opencsv.bean.CsvToBeanFilter;
6 import com.opencsv.bean.MappingStrategy;
7 import com.opencsv.bean.exceptionhandler.CsvExceptionHandler;
8 import org.apache.commons.lang3.ObjectUtils;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 /**
14 * Implements a separate thread for reading input and siphoning it to a
15 * {@link LineExecutor}.
16 * @param <T> The type of bean being created
17 * @author Andrew Rucker Jones
18 * @since 5.2
19 */
20 public class CompleteFileReader<T> extends SingleLineReader implements Runnable {
21
22 /** Filter to be applied to the input. */
23 private final CsvToBeanFilter filter;
24
25 /** The mapping strategy in use. */
26 private final MappingStrategy<? extends T> mappingStrategy;
27
28 /** Whether exceptions in processing should be thrown or collected. */
29 private final CsvExceptionHandler exceptionHandler;
30
31 /** Verifiers to be applied to the beans created. */
32 private final List<BeanVerifier<T>> verifiers;
33
34 /** Counts how many records have been read from the input. */
35 private long lineProcessed;
36
37 /** The exception that brought execution to a grinding halt. */
38 private Throwable terminalException;
39
40 /** The executor that takes lines of input and converts them to beans. */
41 private LineExecutor<T> executor;
42
43 /**
44 *
45 * @param csvReader The {@link CSVReader} from which input is read
46 * @param filter Filter to be applied to the input
47 * @param ignoreEmptyLines Whether empty lines of input should be ignored
48 * @param mappingStrategy The mapping strategy in use
49 * @param exceptionHandler Determines the exception handling behavior
50 * @param verifiers Verifiers to be applied to the beans created
51 */
52 public CompleteFileReader(CSVReader csvReader, CsvToBeanFilter filter,
53 boolean ignoreEmptyLines,
54 MappingStrategy<? extends T> mappingStrategy,
55 CsvExceptionHandler exceptionHandler,
56 List<BeanVerifier<T>> verifiers) {
57 super(csvReader, ignoreEmptyLines);
58 this.filter = filter;
59 this.mappingStrategy = mappingStrategy;
60 this.exceptionHandler = exceptionHandler;
61 this.verifiers = ObjectUtils.defaultIfNull(verifiers, Collections.<BeanVerifier<T>>emptyList());
62 }
63
64 /**
65 * @return The exception that brought execution to a halt
66 */
67 public Throwable getTerminalException() {return terminalException;}
68
69 /**
70 * @return How many lines have been processed thus far
71 */
72 public long getLineProcessed() {return lineProcessed;}
73
74 /**
75 * Sets the executor that will convert text input to bean output.
76 * @param executor The executor to use
77 */
78 public void setExecutor(LineExecutor<T> executor) {
79 if(this.executor == null) {
80 this.executor = executor;
81 }
82 }
83
84 /**
85 * Runs a nice, tight loop to simply read input and submit for conversion.
86 */
87 @Override
88 public void run() {
89 // Parse through each line of the file
90 try {
91 while (null != readNextLine()) {
92 lineProcessed = csvReader.getLinesRead();
93 executor.submitLine(lineProcessed, mappingStrategy, filter,
94 verifiers, line, exceptionHandler);
95 }
96
97 // Since only this thread knows when reading is over, it is responsible
98 // for telling the executor it's finished.
99 executor.complete();
100 } catch(Exception e) {
101 terminalException = e;
102 }
103 }
104 }