Top Description Fields Constructors Methods
io.github.darvil.utils

public Class LoopPool<T>

extends Object
Class Inheritance
Type Parameters
<T>
The type of the elements in the sequence.
Imports
org.jetbrains.annotations.NotNull

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).

Field Summary

Modifier and TypeField and Description
private int
private final @NotNull T[]

Constructor Summary

AccessConstructor and Description
private
LoopPool(int startAt, @NotNull T... pool)

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

The element at the given relative index.
at
(int
The relative index.
relativeIndex
)

Returns the element at the given relative index.

public static <
The type of the elements.
T
>
LoopPool<T>

Returns:

A new LoopPool with the given elements.
atRandomIndex
(T...
The elements to loop through.
pool
)

Creates a new LoopPool with the given elements.

public T

Returns:

The current element in the sequence.
current
()

Returns the current element in the sequence.

public int

Returns:

The internal index.
getIndex
()

Returns the internal index.

private int

Returns:

A valid index to use in the pool array.
getValidIndex
(int
The index to validate.
index
)

Returns a valid index to use in the pool array.

public T

Returns:

The next element in the sequence.
next
()

Returns the next element in the sequence and increments the internal index.

public static <
The type of the elements.
T
>
LoopPool<T>

Returns:

A new LoopPool with the given elements.
of
(T...
The elements to loop through.
pool
)

Creates a new LoopPool with the given elements.

public static <
The type of the elements.
T
>
LoopPool<T>

Returns:

A new LoopPool with the given elements.
of
(int
The index to start at.
startAt
,
T...
The elements to loop through.
pool
)

Creates a new LoopPool with the given elements.

public T

Returns:

The previous element in the sequence.
prev
()

Returns the previous element in the sequence and decrements the internal index.

public void
setIndex(int
The index to set.
index
)

Sets the internal index to the given index.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

indexback to summary
private int index
poolback to summary
private final @NotNull T[] pool

Constructor Detail

LoopPoolback to summary
private LoopPool(int startAt, @NotNull T... pool)
Annotations
@SafeVarargs

Method Detail

atback to summary
public T at(int relativeIndex)

Returns the element at the given relative index. The index is relative to the internal index.

Parameters
relativeIndex:int

The relative index.

Returns:T

The element at the given relative index.

atRandomIndexback to summary
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.

Parameters
<T>
The type of the elements.
pool:T[]

The elements to loop through.

Returns:LoopPool<T>

A new LoopPool with the given elements.

Annotations
@SafeVarargs
currentback to summary
public T current()

Returns the current element in the sequence.

Returns:T

The current element in the sequence.

getIndexback to summary
public int getIndex()

Returns the internal index.

Returns:int

The internal index.

getValidIndexback to summary
private int getValidIndex(int index)

Returns a valid index to use in the pool array. If the index goes out of bounds, it will always wrap around.

Parameters
index:int

The index to validate.

Returns:int

A valid index to use in the pool array.

nextback to summary
public T next()

Returns the next element in the sequence and increments the internal index.

Returns:T

The next element in the sequence.

ofback to summary
public static <T> LoopPool<T> of(T... pool)

Creates a new LoopPool with the given elements. The index will start at 0.

Parameters
<T>
The type of the elements.
pool:T[]

The elements to loop through.

Returns:LoopPool<T>

A new LoopPool with the given elements.

Annotations
@SafeVarargs
ofback to summary
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.

Parameters
<T>
The type of the elements.
startAt:int

The index to start at.

pool:T[]

The elements to loop through.

Returns:LoopPool<T>

A new LoopPool with the given elements.

Annotations
@SafeVarargs
prevback to summary
public T prev()

Returns the previous element in the sequence and decrements the internal index.

Returns:T

The previous element in the sequence.

setIndexback to summary
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:int

The index to set.