mirror of https://github.com/Minestom/Minestom.git
31 lines
953 B
Java
31 lines
953 B
Java
package net.minestom.server.collision;
|
|
|
|
import java.util.List;
|
|
|
|
import static java.util.Arrays.asList;
|
|
import static java.util.Collections.emptyList;
|
|
import static java.util.Optional.of;
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
// https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists#Java
|
|
final class CartesianProduct {
|
|
public static List<?> product(List<?>... a) {
|
|
if (a.length >= 2) {
|
|
List<?> product = a[0];
|
|
for (int i = 1; i < a.length; i++) {
|
|
product = product(product, a[i]);
|
|
}
|
|
return product;
|
|
}
|
|
|
|
return emptyList();
|
|
}
|
|
|
|
private static <A, B> List<?> product(List<A> a, List<B> b) {
|
|
return of(a.stream()
|
|
.map(e1 -> of(b.stream().map(e2 -> asList(e1, e2)).collect(toList())).orElse(emptyList()))
|
|
.flatMap(List::stream)
|
|
.collect(toList())).orElse(emptyList());
|
|
}
|
|
}
|