-
Notifications
You must be signed in to change notification settings - Fork 447
chore: Fix ResourceSet iterator to allow multiple iterations and reduce code duplication #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…erator() method Co-authored-by: tiwarishubham635 <[email protected]>
Add inline comments highlighting why the firstPage attribute is added |
Co-authored-by: tiwarishubham635 <[email protected]>
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|
* chore: updating maven repository link in release notes (#876) * chore: Fix ResourceSet iterator to allow multiple iterations and reduce code duplication (#883) --------- Co-authored-by: Copilot <[email protected]>
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.java
andbearertoken/ResourceSet.java
to 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
ResourceSetIterationHelper
to 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.