mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2025-01-16 13:11:43 +01:00
Add generic methods that allows to get next or previous elements in given array.
This commit is contained in:
parent
28d61870a7
commit
e900f1e665
@ -95,4 +95,60 @@ public class Utils
|
|||||||
map(gameModeAddon -> gameModeAddon.getDescription().getName()).
|
map(gameModeAddon -> gameModeAddon.getDescription().getName()).
|
||||||
orElse(null);
|
orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method allows to get next value from array list after given value.
|
||||||
|
* @param values Array that should be searched for given value.
|
||||||
|
* @param currentValue Value which next element should be found.
|
||||||
|
* @param <T> Instance of given object.
|
||||||
|
* @return Next value after currentValue in values array.
|
||||||
|
*/
|
||||||
|
public static <T extends Object> T getNextValue(T[] values, T currentValue)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < values.length; i++)
|
||||||
|
{
|
||||||
|
if (values[i].equals(currentValue))
|
||||||
|
{
|
||||||
|
if (i + 1 == values.length)
|
||||||
|
{
|
||||||
|
return values[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return values[i + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method allows to get previous value from array list after given value.
|
||||||
|
* @param values Array that should be searched for given value.
|
||||||
|
* @param currentValue Value which previous element should be found.
|
||||||
|
* @param <T> Instance of given object.
|
||||||
|
* @return Previous value before currentValue in values array.
|
||||||
|
*/
|
||||||
|
public static <T extends Object> T getPreviousValue(T[] values, T currentValue)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < values.length; i++)
|
||||||
|
{
|
||||||
|
if (values[i].equals(currentValue))
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
return values[i - 1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return values[values.length - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user