Class SizableIterable<T>

java.lang.Object
org.apache.tinkerpop.gremlin.algorithm.generator.SizableIterable<T>
All Implemented Interfaces:
Iterable<T>

public class SizableIterable<T> extends Object implements Iterable<T>
Utility class to make size determinations for Iterable for efficient.

If the number of elements in an Iterable is known, then the Iterable can be wrapped in this class to make determining the size a constant time operation rather than an iteration over the whole Iterable.

Some generators need to know the number of vertices prior to generation. Using this class can speed up the generator.

Author:
Matthias Broecheler (me@matthiasb.com)
  • Constructor Details

    • SizableIterable

      public SizableIterable(Iterable<T> iterable, int size)
      Wraps the given Iterable with the given size.

      NOTE: the size MUST be the size of the Iterable. This is not verified.

  • Method Details

    • size

      public int size()
      Returns the size of the Iterable
      Returns:
      Size of the Iterable
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • sizeOf

      public static <T> int sizeOf(Iterable<T> iterable)
      Utility method to determine the number of elements in an Iterable.

      Checks if the given Iterable is an instance of a Collection or SizableIterable before iterating to improve performance.

      Returns:
      The size of the given Iterable.