From e900f1e665bc596b5a1c4a522727fbd19d8a2bac Mon Sep 17 00:00:00 2001 From: BONNe Date: Thu, 5 Sep 2019 13:23:19 +0300 Subject: [PATCH] Add generic methods that allows to get next or previous elements in given array. --- .../bentobox/challenges/utils/Utils.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/main/java/world/bentobox/challenges/utils/Utils.java b/src/main/java/world/bentobox/challenges/utils/Utils.java index 802c822..d5ad9a2 100644 --- a/src/main/java/world/bentobox/challenges/utils/Utils.java +++ b/src/main/java/world/bentobox/challenges/utils/Utils.java @@ -95,4 +95,60 @@ public class Utils 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 Instance of given object. + * @return Next value after currentValue in values array. + */ + public static 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 Instance of given object. + * @return Previous value before currentValue in values array. + */ + public static 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; + } }