View Javadoc
1   /*
2    * Copyright 2012 Olivier Godineau
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
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   * A "Proxy" reader which Provides a mecanism to skip empty row.
24   * 
25   * @author Olivier Godineau
26   * 
27   */
28  public class IgnoreNullReader implements IReader {
29  	/**
30  	 * The class logger.
31  	 */
32  	private static final Logger LOGGER = LoggerFactory.getLogger(IgnoreNullReader.class);
33  	/**
34  	 * the original reader.
35  	 */
36  	private final IReader reader;
37  
38  	/**
39  	 * the last row retrieved from the original reader.
40  	 */
41  	private Row currentRow = null;
42  	/**
43  	 * 
44  	 */
45  	private boolean next;
46  	/**
47  	 * because next() method even if returns the next element, loads the
48  	 * following row. During loading the following row a readException could
49  	 * occur. So we need to keep this exception to throw it on the following
50  	 * call of next() method.
51  	 */
52  	private ReaderException exception = null;
53  
54  	/**
55  	 * 
56  	 * @param reader
57  	 *            a concret reader
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  	 * @return true if exists a next not empty row.
70  	 */
71  	public boolean hasNext() {
72  		return next;
73  	}
74  
75  	/**
76  	 * 
77  	 * @return the next not empty row.
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 	 * @return the next not empty row.
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 	 * Throws UnsupportedOperationException.
119 	 */
120 	public void remove() {
121 		throw new UnsupportedOperationException();
122 
123 	}
124 
125 	/**
126 	 * close the original reader.
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 	 * @return the original withHeaders attribute.
138 	 */
139 	public boolean isWithHeaders() {
140 		return reader.isWithHeaders();
141 	}
142 
143 }