chore: Fix ResourceSet iterator to allow multiple iterations and reduce code duplication#883
chore: Fix ResourceSet iterator to allow multiple iterations and reduce code duplication#883tiwarishubham635 merged 3 commits intomainfrom
Conversation
…erator() method Co-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
|
Add inline comments highlighting why the firstPage attribute is added |
Co-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
Added inline comments explaining why the firstPage attribute was added and how it enables multiple iterations. The comments clarify its purpose in both the field declaration and where it's used in the constructor and iterator() method. (686053a) |
tiwarishubham635
left a comment
There was a problem hiding this comment.
Looks good to me!
manisha1997
left a comment
There was a problem hiding this comment.
Code has duplication. Reduce duplication.
Reduced code duplication by extracting the common iteration logic to a shared |
|
Sonarcloud reported code duplication, improve. |
cedb4fe to
686053a
Compare
|


The ResourceSet class implements Iterable but had a flawed implementation that only allowed iterating through its elements once. Subsequent calls to iterator() would return an iterator that immediately reported no elements available. Additionally, the fix introduced code duplication between the two ResourceSet implementations.
Problem
The issue was in the ResourceSet's iterator implementation. While
iterator()created a new ResourceSetIterator instance, the iterator relied on shared state variables in the ResourceSet instance:processed- count of elements already iteratediterator- the current page's iteratorpages- current page numberAfter the first iteration completed, these variables remained in their exhausted state, causing subsequent iterations to fail.
Solution
Multiple Iteration Fix
Modified both
ResourceSet.javaandbearertoken/ResourceSet.javato reset state when a new iterator is requested:firstPage) during constructioniterator()method before creating new iterator:processed = 0- reset element countpages = 1- reset to first pagepage = firstPage- reset current page referenceiterator = firstPage.getRecords().iterator()- reset page iteratorCode Duplication Reduction
Created
ResourceSetIterationHelperto eliminate duplication between the two ResourceSet classes:Example
Testing
Added comprehensive tests to verify:
Fixes #684.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.