Bug 43905 - Have the HSSFSheet and HSSFRow implement the Iterator interface
Summary: Have the HSSFSheet and HSSFRow implement the Iterator interface
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.0-FINAL
Hardware: PC Windows XP
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-19 09:41 UTC by Noel Markham
Modified: 2008-02-05 08:35 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Noel Markham 2007-11-19 09:41:51 UTC
Have the HSSFSheet and HSSFRow implement the Iterator interface - this would
allow the Java 5 foreach to be used resulting in cleaner code. Considering there
is only one Iterator method on each class, it is clear what the iterator() class
would return.
Comment 1 Nick Burch 2007-12-04 04:06:50 UTC
HSSFRow.cellIterator and HSSFSheet.rowIterator should already provide what you
want. Use row.cellIterator or sheet.rowIterator in your foreach loop
Comment 2 Leif Arne Storset 2008-02-04 23:51:37 UTC
Sadly, Nick Burch is mistaken. If you test the code sample "Iterate over rows 
and cells (including Java 5 foreach loops)" at http://poi.apache.org/hssf/quick-
guide.html#Iterator, you will find that it generates a compiler error "Can only 
iterate over an array or an instance of java.lang.Iterable". (Was this tested 
before publishing?)

For this to work, Noel Markham's original suggestion should be implemented. For 
HSSFSheet and HSSFRow to be Iterable, there must be a method called iterator(). 
Alternatively (and perhaps more logically), you could return Iterable data 
structures through HSSFSheet.rows() and HSSFRow.cells().
Comment 3 Leif Arne Storset 2008-02-05 07:05:04 UTC
Additionally, the Iterator returned by iterator() should be typed - i. e. 
return an Iterator<HSSFRow> or Iterator<HSSFSheet>. This avoids compiler 
warnings about type safety and increases user confidence.
Comment 4 Nick Burch 2008-02-05 07:23:35 UTC
(In reply to comment #3)
> Additionally, the Iterator returned by iterator() should be typed - i. e. 
> return an Iterator<HSSFRow> or Iterator<HSSFSheet>. This avoids compiler 
> warnings about type safety and increases user confidence.

Typing requires Java 1.5, and POI is aiming to stay working with Java 1.4, so
that isn't going to be possible
Comment 5 Nick Burch 2008-02-05 08:35:46 UTC
It seems that the way sun introduced foreach loops in java 1.5 was rather
stupid, and prevents your code from then working on java 1.4. Since we require
java 1.4 compatibility, we can't implement java.lang.Iterable (though trunk does
have the method that that requires). For now, you'll need to just use the normal
iterator methods

In the ooxml branch, we do have some workarounds for java 1.4 vs java 1.5
features, so Sheet/HSSFSheet/XSSFSheet and Row/HSSFRow/XSSFRow do implement
java.lang.Iterable (when build on java 1.5), so foreach loops will work there.