You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2022/10/06 11:52:00 UTC

[jira] [Work logged] (CSV-274) CSVParser.iterator() does not iterate over result set as expected.

     [ https://issues.apache.org/jira/browse/CSV-274?focusedWorklogId=814338&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-814338 ]

ASF GitHub Bot logged work on CSV-274:
--------------------------------------

                Author: ASF GitHub Bot
            Created on: 06/Oct/22 11:51
            Start Date: 06/Oct/22 11:51
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on code in PR #270:
URL: https://github.com/apache/commons-csv/pull/270#discussion_r988928581


##########
src/main/java/org/apache/commons/csv/CSVParser.java:
##########
@@ -734,9 +734,24 @@ private boolean isStrictQuoteMode() {
      * {@link IllegalStateException}.
      * </p>
      * <p>
-     * If the parser is closed a call to {@link Iterator#next()} will throw a
+     * If the parser is closed, the iterator will not yield any more records.
+     * A call to {@link Iterator#hasNext()} will return {@code false} and
+     * a call to {@link Iterator#next()} will throw a
      * {@link NoSuchElementException}.
      * </p>
+     * <p>

Review Comment:
   Let's not add an anti-pattern as a _code_ example here, please. IOW: Document what to do _correctly_, especially in code, not what to do _wrong_. There is always some way to shoot yourself in the foot. Describing the behavior as you do above is enough IMO.



##########
src/main/java/org/apache/commons/csv/CSVParser.java:
##########
@@ -799,7 +814,9 @@ CSVRecord nextRecord() throws IOException {
 
     /**
      * Returns a sequential {@code Stream} with this collection as its source.
-     *
+     * <p>
+     * If the parser is closed, the stream will not produce any more values.
+     * See the comments in {@link iterator()}.

Review Comment:
   Close HTML tags.





Issue Time Tracking
-------------------

            Worklog Id:     (was: 814338)
    Remaining Estimate: 0h
            Time Spent: 10m

> CSVParser.iterator() does not iterate over result set as expected.
> ------------------------------------------------------------------
>
>                 Key: CSV-274
>                 URL: https://issues.apache.org/jira/browse/CSV-274
>             Project: Commons CSV
>          Issue Type: Bug
>          Components: Parser
>    Affects Versions: 1.8
>            Reporter: David Guiney
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> To return a stream of `CSVRecords` in a Spliterators, I need to call `CSVParser.getRecords().iterator()`. I worry that the `getRecords()` will load the records from the parser into memory, before creating the iterator which can be a problem with large CSV files.
> My code: 
> {code:java}
>     public Stream<CSVRecord> convertFileToMaps(Path path) throws IOException {
>         try (CSVParser parser = CSVParser.parse(path, Charset.defaultCharset(), CSVFormat.RFC4180
>             .withFirstRecordAsHeader())) {
>             return StreamSupport.stream(Spliterators.spliteratorUnknownSize(parser.iterator(), 0), false);
>         }
> }
> {code}
> and:
> {code:java}
>     public Stream<CSVRecord> convertFileToMaps(Path path) throws IOException {
>         try (CSVParser parser = CSVParser.parse(path, Charset.defaultCharset(), CSVFormat.RFC4180
>             .withFirstRecordAsHeader())) {
>             return StreamSupport.stream(parser.spliterator(), false);
>         }
> }
> {code}
> When I collect the results of my method, it gives me
> {code:java}
> []
> {code}
>  
> If I replace `parser.iterator()` with `parser.getRecords().iterator()` then I get the desired results. Is the iterator not meant to be an iterator of the list of `CSVRecord`.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)