Skip to content
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

Utility methods on CloseableIterator to make life a little easier. #589

Merged
merged 2 commits into from
Apr 27, 2016

Conversation

tfenne
Copy link
Member

@tfenne tfenne commented Apr 27, 2016

Description

Scala has spoiled me, and I'm tired of Java's iterators being second class citizens. This commit adds a pair of default methods to CloseableIterator which will benefit most iterators in htsjdk, and allows easy transformation to a List and a Stream.

Checklist

  • Code compiles correctly
  • New tests covering changes and new functionality
  • All tests passing
  • Extended the README / documentation, if necessary


/** Returns a Stream that will consume from the underlying iterator. */
default Stream<T> stream() {
final Spliterator<T> s = Spliterators.spliteratorUnknownSize(this, Spliterator.ORDERED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tfenne Is there any way that we can set it up so the iterator is automatically closed when the stream is consumed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be nice, but I'm not sure how possible it is without a bunch of wrapper-ing of the iterators. Let me see if I can come up with something that's not too ugly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an easy way unless stream offers some sort of on-closed callback... I was hoping you'd know some magic...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahah, there is exactly such a method. Stream.onClose(Runnable)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - I just found that too. Updated to use it.

@tfenne
Copy link
Member Author

tfenne commented Apr 27, 2016

@lbergelson Changes are all made, can you take a quick final look please?

@lbergelson
Copy link
Member

👍 Thanks for this. I definitely seem myself using the stream function in the future. Merge when ready.

@tfenne tfenne merged commit 5dfe920 into master Apr 27, 2016
@tfenne tfenne deleted the tf_closeable_iterator_utility branch April 27, 2016 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants