Class KiwiIterators

java.lang.Object
org.kiwiproject.collect.KiwiIterators

public final class KiwiIterators extends Object
Utility methods for working with Iterator instances. Analogous to Guava's Iterators
  • Method Details

    • cycleForever

      public static <T> Iterator<T> cycleForever(Iterable<T> iterable)
      Returns a thread-safe iterator that cycles indefinitely over the elements of iterable, base on Guava's Iterables.cycle(Iterable). The differences from Guava is that the returned iterator provides thread-safety; makes an immutable copy of the provided iterable; and does not support element removal regardless of whether the original iterable does.

      Typical use cases include round-robin scenarios, such as round-robin between replicated service registries like Netflix Eureka.

      The returned iterator does not support Iterator.remove() nor does it support Iterator.forEachRemaining(Consumer), as the entire point is to cycle forever.

      Type Parameters:
      T - the type of objects in the Iterable
      Parameters:
      iterable - the Iterable to cycle
      Returns:
      an Iterator that cycles through the given Iterable without terminating
    • cycleForever

      @SafeVarargs public static <T> Iterator<T> cycleForever(T... elements)
      Returns a thread-safe iterator that cycles indefinitely over the elements of iterable, based on Guava's Iterables.cycle(Iterable). The differences from Guava are that the returned iterator provides thread-safety; makes an immutable copy of the provided iterable; and does not support element removal regardless of whether the original iterable does.

      Typical use cases include round-robin scenarios, such as round-robin between replicated service registries like Netflix Eureka.

      The returned iterator does not support Iterator.remove() nor does it support Iterator.forEachRemaining(Consumer), as the entire point is to cycle forever.

      Type Parameters:
      T - the type of the elements
      Parameters:
      elements - the values to cycle
      Returns:
      an Iterator that cycles through the given iterable without terminating