1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.opencsv.bean.concurrent;
17
18 import com.opencsv.bean.MappingStrategy;
19 import com.opencsv.bean.exceptionhandler.CsvExceptionHandler;
20 import com.opencsv.bean.util.OpencsvUtils;
21 import com.opencsv.bean.util.OrderedObject;
22 import com.opencsv.exceptions.CsvChainedException;
23 import com.opencsv.exceptions.CsvException;
24 import com.opencsv.exceptions.CsvFieldAssignmentException;
25 import com.opencsv.exceptions.CsvRuntimeException;
26
27 import java.util.SortedSet;
28 import java.util.concurrent.BlockingQueue;
29
30
31
32
33
34
35
36
37 public class ProcessCsvBean<T> implements Runnable {
38
39 private final long lineNumber;
40 private final MappingStrategy<T> mappingStrategy;
41 private final T bean;
42 private final BlockingQueue<OrderedObject<String[]>> resultantLineQueue;
43 private final BlockingQueue<OrderedObject<CsvException>> thrownExceptionsQueue;
44 private final SortedSet<Long> expectedRecords;
45 private final CsvExceptionHandler exceptionHandler;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public ProcessCsvBean(long lineNumber, MappingStrategy<T> mappingStrategy,
62 T bean, BlockingQueue<OrderedObject<String[]>> resultantLineQueue,
63 BlockingQueue<OrderedObject<CsvException>> thrownExceptionsQueue,
64 SortedSet<Long> expectedRecords, CsvExceptionHandler exceptionHandler) {
65 this.lineNumber = lineNumber;
66 this.mappingStrategy = mappingStrategy;
67 this.bean = bean;
68 this.resultantLineQueue = resultantLineQueue;
69 this.thrownExceptionsQueue = thrownExceptionsQueue;
70 this.expectedRecords = expectedRecords;
71 this.exceptionHandler = exceptionHandler;
72 }
73
74 @Override
75 public void run() {
76 try {
77 OpencsvUtils.queueRefuseToAcceptDefeat(resultantLineQueue,
78 new OrderedObject<>(lineNumber, mappingStrategy.transmuteBean(bean)));
79 }
80 catch (CsvFieldAssignmentException | CsvChainedException e) {
81 expectedRecords.remove(lineNumber);
82 OpencsvUtils.handleException(e, lineNumber, exceptionHandler, thrownExceptionsQueue);
83 }
84 catch(CsvRuntimeException csvre) {
85 expectedRecords.remove(lineNumber);
86
87 throw csvre;
88 }
89 catch(Exception t) {
90 expectedRecords.remove(lineNumber);
91 throw new RuntimeException(t);
92 }
93 }
94
95 }