/** * Converts this to a {@link Tree}. * * @return A new {@link Tree}. */ default Tree<T> toTree() { return ValueModule.toTraversable(this, Tree.empty(), Tree::of, Tree::ofAll); }
/** * Returns a Tree containing {@code n} times the given {@code element} * * @param <T> Component type of the Tree * @param n The number of elements in the Tree * @param element The element * @return A Tree of size {@code n}, where each element is the given {@code element}. */ static <T> Tree<T> fill(int n, T element) { return io.vavr.collection.Collections.fillObject(n, element, empty(), Tree::of); }
/** * Returns a Tree containing {@code n} values supplied by a given Supplier {@code s}. * * @param <T> Component type of the Tree * @param n The number of elements in the Tree * @param s The Supplier computing element values * @return A Tree of size {@code n}, where each element contains the result supplied by {@code s}. * @throws NullPointerException if {@code s} is null */ static <T> Tree<T> fill(int n, Supplier<? extends T> s) { Objects.requireNonNull(s, "s is null"); return io.vavr.collection.Collections.fill(n, s, empty(), Tree::of); }
/** * Returns a Tree containing {@code n} values of a given Function {@code f} * over a range of integer values from 0 to {@code n - 1}. * * @param <T> Component type of the Tree * @param n The number of elements in the Tree * @param f The Function computing element values * @return A Tree consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code f} is null */ static <T> Tree<T> tabulate(int n, Function<? super Integer, ? extends T> f) { Objects.requireNonNull(f, "f is null"); return io.vavr.collection.Collections.tabulate(n, f, empty(), Tree::of); }
@SuppressWarnings("unchecked") static <T, U> Tree<U> flatMap(Node<T> node, Function<? super T, ? extends Iterable<? extends U>> mapper) { final Tree<U> mapped = ofAll(mapper.apply(node.getValue())); if (mapped.isEmpty()) { return empty(); } else { final io.vavr.collection.List<Node<U>> children = (io.vavr.collection.List<Node<U>>) (Object) node .getChildren() .map(child -> flatMap(child, mapper)) .filter(Tree::nonEmpty); return of(mapped.get(), children.prependAll(mapped.getChildren())); } }
/** * Returns a Tree containing {@code n} times the given {@code element} * * @param <T> Component type of the Tree * @param n The number of elements in the Tree * @param element The element * @return A Tree of size {@code n}, where each element is the given {@code element}. */ static <T> Tree<T> fill(int n, T element) { return io.vavr.collection.Collections.fillObject(n, element, empty(), Tree::of); }
/** * Converts this to a {@link Tree}. * * @return A new {@link Tree}. */ default Tree<T> toTree() { return ValueModule.toTraversable(this, Tree.empty(), Tree::of, Tree::ofAll); }
/** * Returns a Tree containing {@code n} values supplied by a given Supplier {@code s}. * * @param <T> Component type of the Tree * @param n The number of elements in the Tree * @param s The Supplier computing element values * @return A Tree of size {@code n}, where each element contains the result supplied by {@code s}. * @throws NullPointerException if {@code s} is null */ static <T> Tree<T> fill(int n, Supplier<? extends T> s) { Objects.requireNonNull(s, "s is null"); return io.vavr.collection.Collections.fill(n, s, empty(), Tree::of); }
/** * Returns a Tree containing {@code n} values of a given Function {@code f} * over a range of integer values from 0 to {@code n - 1}. * * @param <T> Component type of the Tree * @param n The number of elements in the Tree * @param f The Function computing element values * @return A Tree consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code f} is null */ static <T> Tree<T> tabulate(int n, Function<? super Integer, ? extends T> f) { Objects.requireNonNull(f, "f is null"); return io.vavr.collection.Collections.tabulate(n, f, empty(), Tree::of); }
@SuppressWarnings("unchecked") static <T, U> Tree<U> flatMap(Node<T> node, Function<? super T, ? extends Iterable<? extends U>> mapper) { final Tree<U> mapped = ofAll(mapper.apply(node.getValue())); if (mapped.isEmpty()) { return empty(); } else { final io.vavr.collection.List<Node<U>> children = (io.vavr.collection.List<Node<U>>) (Object) node .getChildren() .map(child -> flatMap(child, mapper)) .filter(Tree::nonEmpty); return of(mapped.get(), children.prependAll(mapped.getChildren())); } }