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 import java.util.NoSuchElementException;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22
23
24
25
26
27
28 public class IgnoreNullReader implements IReader {
29
30
31
32 private static final Logger LOGGER = LoggerFactory.getLogger(IgnoreNullReader.class);
33
34
35
36 private final IReader reader;
37
38
39
40
41 private Row currentRow = null;
42
43
44
45 private boolean next;
46
47
48
49
50
51
52 private ReaderException exception = null;
53
54
55
56
57
58
59 public IgnoreNullReader(IReader reader) {
60 super();
61 if (reader == null) {
62 throw new IllegalArgumentException(IgnoreNullReader.class.getSimpleName()
63 + " Constructor IReader argument must not be null");
64 }
65 this.reader = reader;
66 this.currentRow = setNext();
67 }
68
69
70
71 public boolean hasNext() {
72 return next;
73 }
74
75
76
77
78
79 public Row next() {
80 if (!hasNext()) {
81 throw new NoSuchElementException();
82 }
83 Row retour = currentRow;
84 ReaderException err = this.exception;
85 try {
86 currentRow = setNext();
87 } catch (ReaderException e) {
88 this.exception = e;
89 }
90 if (err != null) {
91
92 throw err;
93 }
94
95 return retour;
96 }
97
98
99
100
101
102 private Row setNext() {
103 Row retour = null;
104
105 do {
106 if (reader.hasNext()) {
107 retour = reader.next();
108 next = true;
109 } else {
110 next = false;
111 retour = null;
112 }
113 } while (retour != null && retour.isEmpty());
114 return retour;
115 }
116
117
118
119
120 public void remove() {
121 throw new UnsupportedOperationException();
122
123 }
124
125
126
127
128 public void close() {
129 try {
130 reader.close();
131 } catch (IOException e) {
132 LOGGER.info("Error on closing reader", e);
133 }
134 }
135
136
137
138
139 public boolean isWithHeaders() {
140 return reader.isWithHeaders();
141 }
142
143 }