All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.util.CloseableSuppliedIterator Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.util;

import org.infinispan.commons.util.CloseableIterator;

import java.util.NoSuchElementException;
import java.util.function.Consumer;

public class CloseableSuppliedIterator implements CloseableIterator {
   private final CloseableSupplier supplier;

   private E next;

   public CloseableSuppliedIterator(CloseableSupplier supplier) {
      if (supplier == null) {
         throw new NullPointerException();
      }
      this.supplier = supplier;
   }

   @Override
   public void close() {
      supplier.close();
   }

   private E getNext() {
      return supplier.get();
   }

   @Override
   public boolean hasNext() {
      if (next == null) {
         next = getNext();
      }
      return next != null;
   }

   @Override
   public E next() {
      E e = next == null ? getNext() : next;
      if (e == null) {
         throw new NoSuchElementException();
      }
      next = null;
      return e;
   }

   @Override
   public void forEachRemaining(Consumer action) {
      E supplied;
      if (next != null) {
         action.accept(next);
      }
      while ((supplied = supplier.get()) != null) {
         action.accept(supplied);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy