Class LoopPool<T>

java.lang.Object
io.github.darvil.utils.LoopPool<T>
Type Parameters:
T - The type of the elements in the sequence.

public class LoopPool<T> extends Object
A sequence of elements that can be looped through. The sequence can be looped through in both directions. This may be done by calling next() or prev().

An internal index is used to keep track of the current element. This index can be also be set manually by calling setIndex(int).

  • Method Summary

    Modifier and Type
    Method
    Description
    at(int relativeIndex)
    Returns the element at the given relative index.
    static <T> LoopPool<T>
    atRandomIndex(T... pool)
    Creates a new LoopPool with the given elements.
    Returns the current element in the sequence.
    int
    Returns the internal index.
    Returns the next element in the sequence and increments the internal index.
    static <T> LoopPool<T>
    of(int startAt, T... pool)
    Creates a new LoopPool with the given elements.
    static <T> LoopPool<T>
    of(T... pool)
    Creates a new LoopPool with the given elements.
    Returns the previous element in the sequence and decrements the internal index.
    void
    setIndex(int index)
    Sets the internal index to the given index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      @SafeVarargs public static <T> LoopPool<T> of(T... pool)
      Creates a new LoopPool with the given elements. The index will start at 0.
      Type Parameters:
      T - The type of the elements.
      Parameters:
      pool - The elements to loop through.
      Returns:
      A new LoopPool with the given elements.
    • of

      @SafeVarargs public static <T> LoopPool<T> of(int startAt, T... pool)
      Creates a new LoopPool with the given elements. The index will start at the given index.
      Type Parameters:
      T - The type of the elements.
      Parameters:
      startAt - The index to start at.
      pool - The elements to loop through.
      Returns:
      A new LoopPool with the given elements.
    • atRandomIndex

      @SafeVarargs public static <T> LoopPool<T> atRandomIndex(T... pool)
      Creates a new LoopPool with the given elements. The index will start at a random index between 0 and the number of elements provided.
      Type Parameters:
      T - The type of the elements.
      Parameters:
      pool - The elements to loop through.
      Returns:
      A new LoopPool with the given elements.
    • setIndex

      public void setIndex(int index)
      Sets the internal index to the given index. If the index goes out of bounds, it will always wrap around.
      Parameters:
      index - The index to set.
    • getIndex

      public int getIndex()
      Returns the internal index.
      Returns:
      The internal index.
    • next

      public T next()
      Returns the next element in the sequence and increments the internal index.
      Returns:
      The next element in the sequence.
    • prev

      public T prev()
      Returns the previous element in the sequence and decrements the internal index.
      Returns:
      The previous element in the sequence.
    • current

      public T current()
      Returns the current element in the sequence.
      Returns:
      The current element in the sequence.
    • at

      public T at(int relativeIndex)
      Returns the element at the given relative index. The index is relative to the internal index.
      Parameters:
      relativeIndex - The relative index.
      Returns:
      The element at the given relative index.