Class IterableTestOps

java.lang.Object
org.soliscode.test.util.IterableTestOps

public final class IterableTestOps extends Object

This class consists exclusively of static methods that operate on or return iterables.

The methods of this class all throw a NullPointerException if the iterables or class objects provided to them are null.

Since:
1.0.0
Author:
evanbergstrom
  • Method Details

    • isEmpty

      public static boolean isEmpty(Iterable<?> i)
      Returns true if the iterable contains no elements.
      Parameters:
      i - The iterable.
      Returns:
      true if the iterable contains no elements, false otherwise.
    • size

      public static int size(Iterable<?> i)
      Returns the number of elements in this iterable. If this iterable contains more than Integer.MAX_VALUE elements, returnsInteger.MAX_VALUE.
      Parameters:
      i - The iterable.
      Returns:
      the number of elements in this iterable.
    • contains

      public static boolean contains(Iterable<?> i, Object o)
      Returns true if this iterable contains the specified element. Returns true if and only if this iterable contains at least one element e such that Objects.equals(o, e).
      Parameters:
      i - The iterable.
      o - element whose presence in this iterable is to be tested
      Returns:
      true if this iterable contains the specified element, false otherwise.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this collection
      NullPointerException - if the specified element is null and this iterable does not permit null elements
    • containsByIdentity

      public static boolean containsByIdentity(Iterable<?> i, Object o)
      Returns true if this iterable contains the specified element. Returns true if and only if this iterable contains this element by identity.
      Parameters:
      i - The iterable.
      o - element whose presence in this iterable is to be tested
      Returns:
      true if this iterable contains the specified element, false otherwise.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this collection
      NullPointerException - if the specified element is null and this iterable does not permit null elements
    • empty

      public static <E> Iterable<E> empty()
      Returns an empty iterable.
      Type Parameters:
      E - the element type.
      Returns:
      an empty iterable.
    • equals

      public static boolean equals(Iterable<?> iterable1, Iterable<?> iterable2)
      Parameters:
      iterable1 - The first iterable to compare for equality.
      iterable2 - The second iterable to compare for equality.
      Returns:
      true if the iterables contain elements that are equal at each position.
    • asCollection

      @NotNull public static <E> @NotNull Collection<E> asCollection(@NotNull @NotNull Iterable<E> i)
      Converts the iterable to a collection.
      Type Parameters:
      E - the type of the element.
      Parameters:
      i - the iterable to convert.
      Returns:
      a collection with the same elements as the iterable.
    • asList

      @NotNull public static <E> @NotNull List<E> asList(@NotNull @NotNull Iterable<E> i)
      Converts the iterable to a list. This method will create a new list as the return value, even if the argument is already a instance of List.
      Type Parameters:
      E - the type of the element.
      Parameters:
      i - the iterable to convert.
      Returns:
      a list with the same elements as the iterable.
    • asSet

      @NotNull public static <E> @NotNull Set<E> asSet(@NotNull @NotNull Iterable<E> i)
      Converts the iterable to a set. This method will create a new set as the return value, even if the argument is already a instance of Set.
      Type Parameters:
      E - the type of the element.
      Parameters:
      i - the iterable to convert.
      Returns:
      a set with the same elements as the iterable.
    • copy

      @NotNull public static <E> @NotNull Iterable<E> copy(@NotNull @NotNull Iterable<E> iterable)
      Creates a copy of an iterable. The returned copy weill have the same elements as the original but may have a different implementation.
      Type Parameters:
      E - the type of the elements.
      Parameters:
      iterable - the iterable to copy.
      Returns:
      a copy of the original iterable.
    • copyFirst

      public static <E> Iterable<E> copyFirst(Iterable<E> iterable, int n)
      Creates a copy of an the first N elements of an iterable.
      Type Parameters:
      E - the type of the elements.
      Parameters:
      iterable - the iterable to copy.
      n - the number of elements to copy.
      Returns:
      a copy of the original iterable.
    • newList

      public static <E> List<E> newList(@NotNull @NotNull Iterable<E> iterable)
      Creates a copy of an iterable and returns it as a List.
      Type Parameters:
      E - the type of the elements.
      Parameters:
      iterable - the iterable to copy.
      Returns:
      a list with the same elements as the original iterable.
    • skipFirstIterator

      public static <E> Iterator<E> skipFirstIterator(@NotNull @NotNull Iterable<E> iterable)
      Returns an iterator that will skips the first element.
      Type Parameters:
      E - the element type.
      Parameters:
      iterable - the iterable to get the iterator for.
      Returns:
      An iterator that skips the first element.
    • skipLastIterator

      public static <E> Iterator<E> skipLastIterator(@NotNull @NotNull Iterable<E> iterable)
      Returns an iterator that will skips the last element.
      Type Parameters:
      E - the element type.
      Parameters:
      iterable - the iterable to get the iterator for.
      Returns:
      An iterator that skips the last element.