Skip to content

Commit

Permalink
Add generic methods that allows to get next or previous elements in g…
Browse files Browse the repository at this point in the history
…iven array.
  • Loading branch information
BONNe committed Sep 5, 2019
1 parent 28d6187 commit e900f1e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/main/java/world/bentobox/challenges/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,60 @@ public static String getGameMode(World world)
map(gameModeAddon -> gameModeAddon.getDescription().getName()).
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;
}
}

0 comments on commit e900f1e

Please sign in to comment.