/** * Safe increment that will always the previous index that is inside [0, list.size() - 1]. * @returns {@code (index - 1 + list.size()) % list.size()}. */ public static int previous(int index, List<?> list) { return wrap(index - 1, list); }
/** * Safe increment that will always the next index that is inside [0, list.size() - 1]. * @returns {@code (index + 1) % list.size()}. */ public static int next(int index, List<?> list) { return wrap(index + 1, list); }
/** * Calls {@link List#set(int, T)}, * but first computes index %= list.size() such that the index is always inside [0, list.size() - 1]. */ public static <T> T setWrap(int index, T newValue, List<T> list) { index = wrap(index, list); return list.set(index, newValue); }
/** * Calls {@link List#get(int)}, * but first computes index %= list.size() such that the index is always inside [0, list.size() - 1]. */ public static <T> T getWrap(int index, List<T> list) { index = wrap(index, list); return list.get(index); }
/** * Removes all the elements from the list from startIndex (included) to endIndex (included). */ public static int removeAllInclusive(int startIndex, int endIndex, List<?> list) { int numberOfElementsToRemove = subLengthInclusive(startIndex, endIndex, list); for (int count = 0; count < numberOfElementsToRemove; count++) { startIndex = wrap(startIndex, list); list.remove(startIndex); } return numberOfElementsToRemove; }
/** * Removes all the elements from the list from startIndex (excluded) to endIndex (excluded). */ public static int removeAllExclusive(int startIndex, int endIndex, List<?> list) { int numberOfElementsToRemove = subLengthExclusive(startIndex, endIndex, list); startIndex = next(startIndex, list); for (int count = 0; count < numberOfElementsToRemove; count++) { startIndex = wrap(startIndex, list); list.remove(startIndex); } return numberOfElementsToRemove; } }