/** * Creates a Tree of the given elements. * * <p>If the given iterable is a tree, it is returned as result. if the iteration order of the * elements is stable. * * @param <T> Component type of the List. * @param iterable An Iterable of elements. * @return A list containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("unchecked") static <T> Tree<T> ofAll(Iterable<? extends T> iterable) { Objects.requireNonNull(iterable, "iterable is null"); if (iterable instanceof Tree) { return (Tree<T>) iterable; } else { final List<T> list = List.ofAll(iterable); return list.isEmpty() ? Empty.instance() : new Node<>(list.head(), list.tail().map(Tree::of)); } }
/** * Returns a new Node containing the given value and having the given children. * * @param value A value * @param children The child nodes, possibly empty * @param <T> Value type * @return A new Node instance. */ static <T> Node<T> of(T value, Iterable<Node<T>> children) { Objects.requireNonNull(children, "children is null"); return new Node<>(value, List.ofAll(children)); }