-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP19.java
More file actions
49 lines (43 loc) · 1.28 KB
/
P19.java
File metadata and controls
49 lines (43 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package lists;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Examples:
* ?- rotate([a,b,c,d,e,f,g,h],3,X).
* X = [d,e,f,g,h,a,b,c]
* <p>
* ?- rotate([a,b,c,d,e,f,g,h],-2,X).
* X = [g,h,a,b,c,d,e,f]
*/
public final class P19 {
private P19() {
}
/**
* Rotate list of item.
*
* @param list list of item
* @param countOfItems how many items will be rotate
* @param <T> type of item
* @return rotated list
*/
public static <T> List<T> rotate(final List<T> list,
final int countOfItems) {
if (countOfItems == 0) {
return list;
}
if (countOfItems > 0) {
return extractAndContactList(P17.split(list, countOfItems));
}
return extractAndContactList(P17.split(
list, countOfItems + list.size()));
}
private static <T> List<T> extractAndContactList(
final Map<Boolean, List<T>> booleanListMap) {
List<T> firstPart = booleanListMap.get(false);
List<T> secondPart = booleanListMap.get(true);
return Stream.concat(firstPart.stream(), secondPart.stream())
.collect(Collectors.toList());
}
}