1
2
3
4
5
6
7
8
9
10
11
12
13
14 package olg.csv.base;
15
16 import java.io.IOException;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21
22
23
24
25
26
27 public class IgnoreNullWriter implements IWriter {
28
29
30
31 private static final Logger LOGGER = LoggerFactory.getLogger(IgnoreNullWriter.class);
32
33
34
35 private final IWriter writer;
36
37
38
39 private int nextRowNum = 1;
40
41
42
43
44
45
46 public IgnoreNullWriter(IWriter writer) {
47 super();
48 if (writer == null) {
49 throw new IllegalArgumentException(IgnoreNullWriter.class.getSimpleName()
50 + " Constructor IWriter argument must not be null");
51 }
52 this.writer = writer;
53
54 }
55
56
57
58
59 public void close() {
60 try {
61 writer.close();
62 } catch (IOException e) {
63 LOGGER.info("Error on closing reader", e);
64 }
65 }
66
67
68
69
70
71
72
73 public void addRow(Row row) {
74
75 if (isValid(row)) {
76 Row copy = row.copy(nextRowNum++);
77 writer.addRow(copy);
78 }
79 }
80
81
82
83
84
85
86
87 public void addLine(String[] values) {
88 if (isValid(values)) {
89 writer.addLine(values);
90 nextRowNum++;
91 }
92
93 }
94
95
96
97
98 public boolean isWithHeaders() {
99 return writer.isWithHeaders();
100 }
101
102
103
104
105
106
107
108
109 private boolean isValid(Row row) {
110 return (row != null && !row.isEmpty());
111
112 }
113
114
115
116
117
118
119
120
121 private boolean isValid(String[] values) {
122 boolean retour = false;
123 if (values != null) {
124
125 for (String value : values) {
126 if (value != null) {
127 retour = true;
128 break;
129 }
130 }
131 }
132 return retour;
133 }
134
135 }