Class 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 Detail

      • 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 Detail

      • size

        public int size()
        Returns the size of the Iterable
        Returns:
        Size of the Iterable
      • 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.