P19 { public static <T> List<T> rotate(List<T> list, int n) { n = n < 0 ? list.size() + n : n; Map<Boolean, List<T>> split = P17.split(list, n); List<T> result = split.get(false); result.addAll(split.get(true)); return result; }  static List<T> rotate(List<T> list, int n);  }
@Test public void shouldRotateAListByThreeElementsWhenNIs3() throws Exception { List<String> rotated = P19.rotate(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), 3); assertThat(rotated, equalTo(Arrays.asList("d", "e", "f", "g", "h", "a", "b", "c"))); }
@Test public void shouldReturnSameListWhenNIs0() throws Exception { List<String> rotated = P19.rotate(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), 0); assertThat(rotated, equalTo(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"))); }
@Test public void shouldRotateWhenNIsNegative() throws Exception { List<String> rotated = P19.rotate(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), -2); assertThat(rotated, equalTo(Arrays.asList("g", "h", "a", "b", "c", "d", "e", "f"))); }