Author: ggregory
Date: Tue Jul 30 16:26:43 2013
New Revision: 1508511
URL: http://svn.apache.org/r1508511
Log:
Sort members.
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java?rev=1508511&r1=1508510&r2=1508511&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
(original)
+++
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
Tue Jul 30 16:26:43 2013
@@ -83,15 +83,15 @@ import java.util.NoSuchElementException;
*/
public class CSVParser implements Iterable<CSVRecord>, Closeable {
- private final Lexer lexer;
- private final Map<String, Integer> headerMap;
- private long recordNumber;
private final CSVFormat format;
+ private final Map<String, Integer> headerMap;
+ private final Lexer lexer;
+ /** A record buffer for getRecord(). Grows as necessary and is reused. */
+ private final List<String> record = new ArrayList<String>();
// the following objects are shared to reduce garbage
- /** A record buffer for getRecord(). Grows as necessary and is reused. */
- private final List<String> record = new ArrayList<String>();
+ private long recordNumber;
private final Token reusableToken = new Token();
/**
@@ -143,17 +143,26 @@ public class CSVParser implements Iterab
this(new StringReader(input), format);
}
+ private void addRecordValue() {
+ final String input = reusableToken.content.toString();
+ final String nullString = this.format.getNullString();
+ if (nullString == null) {
+ record.add(input);
+ } else {
+ record.add(input.equalsIgnoreCase(nullString) ? null : input);
+ }}
+
/**
- * Returns a copy of the header map that iterates in column order.
- * <p>
- * The map keys are column names.
- * The map values are 0-based indices.
- *
- * @return a copy of the header map that iterates in column order.
+ * Closes resources.
+ *
+ * @throws IOException
+ * If an I/O error occurs
*/
- public Map<String, Integer> getHeaderMap() {
- return new LinkedHashMap<String, Integer>(headerMap);
- }
+ public void close() throws IOException {
+ if (lexer != null) {
+ lexer.close();
+ }
+ }
/**
* Returns the current line number in the input stream.
@@ -167,6 +176,18 @@ public class CSVParser implements Iterab
}
/**
+ * Returns a copy of the header map that iterates in column order.
+ * <p>
+ * The map keys are column names.
+ * The map values are 0-based indices.
+ *
+ * @return a copy of the header map that iterates in column order.
+ */
+ public Map<String, Integer> getHeaderMap() {
+ return new LinkedHashMap<String, Integer>(headerMap);
+ }
+
+ /**
* Returns the current record number in the input stream.
* <p/>
* ATTENTION: If your CSV input has multi-line values, the returned number
does not correspond to the line number.
@@ -178,75 +199,6 @@ public class CSVParser implements Iterab
}
/**
- * Parses the next record from the current point in the stream.
- *
- * @return the record as an array of values, or <tt>null</tt> if the end
of the stream has been reached
- * @throws IOException
- * on parse error or input read-failure
- */
- CSVRecord nextRecord() throws IOException {
- CSVRecord result = null;
- record.clear();
- StringBuilder sb = null;
- do {
- reusableToken.reset();
- lexer.nextToken(reusableToken);
- switch (reusableToken.type) {
- case TOKEN:
- this.addRecordValue();
- break;
- case EORECORD:
- this.addRecordValue();
- break;
- case EOF:
- if (reusableToken.isReady) {
- this.addRecordValue();
- }
- break;
- case INVALID:
- throw new IOException("(line " + getCurrentLineNumber() + ")
invalid parse sequence");
- case COMMENT: // Ignored currently
- if (sb == null) { // first comment for this record
- sb = new StringBuilder();
- } else {
- sb.append(Constants.LF);
- }
- sb.append(reusableToken.content);
- reusableToken.type = TOKEN; // Read another token
- break;
- }
- } while (reusableToken.type == TOKEN);
-
- if (!record.isEmpty()) {
- recordNumber++;
- final String comment = sb == null ? null : sb.toString();
- result = new CSVRecord(record.toArray(new String[record.size()]),
headerMap, comment, this.recordNumber);
- }
- return result;
- }
-
- private void addRecordValue() {
- final String input = reusableToken.content.toString();
- final String nullString = this.format.getNullString();
- if (nullString == null) {
- record.add(input);
- } else {
- record.add(input.equalsIgnoreCase(nullString) ? null : input);
- }}
-
- /**
- * Closes resources.
- *
- * @throws IOException
- * If an I/O error occurs
- */
- public void close() throws IOException {
- if (lexer != null) {
- lexer.close();
- }
- }
-
- /**
* Parses the CSV input according to the given format and returns the
content as an array of {@link CSVRecord}
* entries.
* <p/>
@@ -294,6 +246,10 @@ public class CSVParser implements Iterab
return hdrMap;
}
+ public boolean isClosed() {
+ return lexer.isClosed();
+ }
+
/**
* Returns an iterator on the records. IOExceptions occurring during the
iteration are wrapped in a
* RuntimeException.
@@ -346,8 +302,52 @@ public class CSVParser implements Iterab
};
}
- public boolean isClosed() {
- return lexer.isClosed();
- }
+ /**
+ * Parses the next record from the current point in the stream.
+ *
+ * @return the record as an array of values, or <tt>null</tt> if the end
of the stream has been reached
+ * @throws IOException
+ * on parse error or input read-failure
+ */
+ CSVRecord nextRecord() throws IOException {
+ CSVRecord result = null;
+ record.clear();
+ StringBuilder sb = null;
+ do {
+ reusableToken.reset();
+ lexer.nextToken(reusableToken);
+ switch (reusableToken.type) {
+ case TOKEN:
+ this.addRecordValue();
+ break;
+ case EORECORD:
+ this.addRecordValue();
+ break;
+ case EOF:
+ if (reusableToken.isReady) {
+ this.addRecordValue();
+ }
+ break;
+ case INVALID:
+ throw new IOException("(line " + getCurrentLineNumber() + ")
invalid parse sequence");
+ case COMMENT: // Ignored currently
+ if (sb == null) { // first comment for this record
+ sb = new StringBuilder();
+ } else {
+ sb.append(Constants.LF);
+ }
+ sb.append(reusableToken.content);
+ reusableToken.type = TOKEN; // Read another token
+ break;
+ }
+ } while (reusableToken.type == TOKEN);
+
+ if (!record.isEmpty()) {
+ recordNumber++;
+ final String comment = sb == null ? null : sb.toString();
+ result = new CSVRecord(record.toArray(new String[record.size()]),
headerMap, comment, this.recordNumber);
+ }
+ return result;
+ }
}