#Creating a custom stream from a class object that has a method that fetches values one by one.

13 messages · Page 1 of 1 (latest)

robust kraken
#

Hi, I have a class object that's like a database cursor, it has a next() method that reads the next table row.

class MyClass 
   Row next() throws DatabaseException {
    ...
   }

I want to convert this to a stream since this also behaves like a lazy operation. I tried using Supplier which is accepted by Stream.generate(). Supplier have a method get() which has to be overridden for custom implementations.

Stream<Row> stream = Stream.generate(new Supplier<Row>(){
  @Override
  public Row get() {
    // custom implementation
    ... 
  }
});

The only problem using this is that MyClass.next() throws an exception while Supplier.get() doesnt allow exceptions. Is there any other way to create a custom stream for such class?

compact hamletBOT
#

This post has been reserved for your question.

Hey @robust kraken! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

prime mantle
#

........ try/catch maybe?

hearty night
#

or throw unchecked exceptions

compact hamletBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

robust kraken
# prime mantle ........ try/catch maybe?

I want the stream to throw the error as well in case that happens, or either some other way to identify that it failed.

Returning null won't help since that's what will be returned when all rows have been read

robust kraken
#

Ah nvm understood what you meant

tawny narwhal
#

Sneaky tbrows kekw

idle pollen
#

If you want to view this lazily, I'd save some time and just implement Java's Iterator Class.

You already have next, with an exception.
Now DatabaseException turns into an extension of NoSuchElementException.

This gives you the following.

class MyClass implements Iterator<Row> {
   boolean hasNext() {
      // required for Iterator Interface
   }

   Row next() {
      // Whatever your next code does
   }
   // everything else we can use the default on
}

class RowStreamer implements Iterable<Row>{
  // Assume setter or constructor involving a Iterator<Row>, which is `MyClass`
  public Stream<T> stream() {
    if (rowIterator == null) {
      throw new InvalidStateException("Cannot build a stream before getting a row Iterator");
    }
    return StreamSupport.stream(spliterator(), false);
  }
}

And now you can Stream it, Iterate it, etc etc. And it's not a lot of additional Code.
you want to use RowStreamer just to keep the idea of Data, Data Access, and Data Modification separate.

compact hamletBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

robust kraken
#

Thanks!

compact hamletBOT
# robust kraken Thanks!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.