ListUtils { public static void shiftItemsAsMuchAsPossible(List<?> list, int[] indexArray, int offset) { Arrays.sort(indexArray); if (offset > 0) { int last = list.size(); for (int i = indexArray.length - 1; i >= 0; i--) { last = shiftItem(list.subList(0, last), indexArray[i], offset); } } else { int last = -1; for (int i = 0; i < indexArray.length; i++) { int index = indexArray[i] - (last + 1); last = shiftItem(list.subList(last + 1, list.size()), index, offset); } } }  static int[] toIntArray(List<Integer> intList); static int shiftItem(List<?> list, int index, int offset); static void shiftItemToFront(List<?> list, int index); static void shiftItemToBack(List<?> list, int index); static void shiftItemsAsMuchAsPossible(List<?> list, int[] indexArray, int offset); static void shiftItemsWithFixedDistance(List<?> list, int[] indexArray, int offset);  }
@Test public void testShiftItemsAsMuchAsPossibleNormal() { List<String> list = Arrays.asList("A", "B", "C", "D", "E"); ListUtils.shiftItemsAsMuchAsPossible(list, new int[]{0, 2}, 1); Assert.assertArrayEquals(new String[]{"B", "A", "D", "C", "E"}, list.toArray()); ListUtils.shiftItemsAsMuchAsPossible(list, new int[]{1, 3}, -1); Assert.assertArrayEquals(new String[]{"A", "B", "C", "D", "E"}, list.toArray()); }
@Test public void testShiftItemsAsMuchAsPossibleOverflow() { List<String> list = Arrays.asList("A", "B", "C", "D", "E"); ListUtils.shiftItemsAsMuchAsPossible(list, new int[]{0, 2}, 10); Assert.assertArrayEquals(new String[]{"B", "D", "E", "A", "C"}, list.toArray()); ListUtils.shiftItemsAsMuchAsPossible(list, new int[]{1, 3}, -10); Assert.assertArrayEquals(new String[]{"D", "A", "B", "E", "C"}, list.toArray()); }