java.lang.Object
io.github.darvil.utils.LoopPool<T>
- Type Parameters:
T- The type of the elements in the sequence.
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 TypeMethodDescriptionat(int relativeIndex) Returns the element at the given relative index.static <T> LoopPool<T>atRandomIndex(T... pool) Creates a newLoopPoolwith the given elements.current()Returns the current element in the sequence.intgetIndex()Returns the internal index.next()Returns the next element in the sequence and increments the internal index.static <T> LoopPool<T>of(int startAt, T... pool) Creates a newLoopPoolwith the given elements.static <T> LoopPool<T>of(T... pool) Creates a newLoopPoolwith the given elements.prev()Returns the previous element in the sequence and decrements the internal index.voidsetIndex(int index) Sets the internal index to the given index.
-
Method Details
-
of
Creates a newLoopPoolwith 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
LoopPoolwith the given elements.
-
of
Creates a newLoopPoolwith 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
LoopPoolwith the given elements.
-
atRandomIndex
Creates a newLoopPoolwith 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
LoopPoolwith 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
Returns the next element in the sequence and increments the internal index.- Returns:
- The next element in the sequence.
-
prev
Returns the previous element in the sequence and decrements the internal index.- Returns:
- The previous element in the sequence.
-
current
Returns the current element in the sequence.- Returns:
- The current element in the sequence.
-
at
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.
-