/** * Creates a Tree of the given elements. * * @param <T> Component type of the List. * @param values Zero or more values. * @return A Tree containing the given values. * @throws NullPointerException if {@code values} is null */ @SuppressWarnings("varargs") @SafeVarargs static <T> Tree<T> of(T... values) { Objects.requireNonNull(values, "values is null"); List<T> list = List.of(values); return list.isEmpty() ? Empty.instance() : new Node<>(list.head(), list.tail().map(Tree::of)); }
@SuppressWarnings("unchecked") @Override default <C> Map<C, Seq<T>> groupBy(Function<? super T, ? extends C> classifier) { Objects.requireNonNull(classifier, "classifier is null"); if (isEmpty()) { return HashMap.empty(); } else { return (Map<C, Seq<T>>) traverse().groupBy(classifier); } }
/** * 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)); } }
@Override default <U> Tree<Tuple2<T, U>> zipAll(Iterable<U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); if (isEmpty()) { return Iterator.ofAll(that).map(elem -> Tuple.of(thisElem, elem)).toTree(); } else { final java.util.Iterator<U> thatIter = that.iterator(); final Tree<Tuple2<T, U>> tree = ZipAll.apply((Node<T>) this, thatIter, thatElem); if (thatIter.hasNext()) { final Iterable<Node<Tuple2<T, U>>> remainder = Iterator.ofAll(thatIter).map(elem -> Tree.of(Tuple.of(thisElem, elem))); return new Node<>(tree.getValue(), tree.getChildren().appendAll(remainder)); } else { return tree; } } }
@Override default <U> Seq<U> scanRight(U zero, BiFunction<? super T, ? super U, ? extends U> operation) { Objects.requireNonNull(operation, "operation is null"); return Collections.scanRight( this, zero, operation, List.empty(), List::prepend, Function.identity()); }
/** * 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)); }
/** * Constructs a rose tree branch. * * @param value A value. * @param children A non-empty list of children. * @throws NullPointerException if children is null * @throws IllegalArgumentException if children is empty */ public Node(T value, List<Node<T>> children) { Objects.requireNonNull(children, "children is null"); this.value = value; this.children = children; this.size = Lazy.of(() -> 1 + children.foldLeft(0, (acc, child) -> acc + child.length())); this.hashCode = 31 * 31 + 31 * Objects.hashCode(value) + Objects.hashCode(children); }
@Override public boolean equals(Object o) { if (o == this) { return true; } else if (o instanceof Node) { final Node<?> that = (Node<?>) o; return Objects.equals(this.getValue(), that.getValue()) && Objects.equals(this.getChildren(), that.getChildren()); } else { return false; } }
@Override default Tree<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; }
@Override default <U> U foldRight(U zero, BiFunction<? super T, ? super U, ? extends U> f) { Objects.requireNonNull(f, "f is null"); if (isEmpty()) { return zero; } else { return iterator().foldRight(zero, f); } }
@Override default Seq<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Stream.empty(); } else { return traverse().filter(predicate); } }
@Override default <U> Seq<T> distinctBy(Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); if (isEmpty()) { return Stream.empty(); } else { return traverse().distinctBy(keyExtractor); } }
@Override default Seq<T> distinctBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); if (isEmpty()) { return Stream.empty(); } else { return traverse().distinctBy(comparator); } }
@Override default <U> Tree<Tuple2<T, U>> zip(Iterable<U> that) { Objects.requireNonNull(that, "that is null"); if (isEmpty()) { return Empty.instance(); } else { return Zip.apply((Node<T>) this, that.iterator()); } }
@SuppressWarnings("unchecked") @Override default Tuple2<Seq<T>, Seq<T>> span(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Tuple.of(Stream.empty(), Stream.empty()); } else { return (Tuple2<Seq<T>, Seq<T>>) traverse().span(predicate); } }
@SuppressWarnings("unchecked") @Override default <T1, T2, T3> Tuple3<Tree<T1>, Tree<T2>, Tree<T3>> unzip3( Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (isEmpty()) { return Tuple.of(Empty.instance(), Empty.instance(), Empty.instance()); } else { return (Tuple3<Tree<T1>, Tree<T2>, Tree<T3>>) (Object) Unzip.apply3((Node<T>) this, unzipper); } }
// Idea: // Traverse (depth-first) until a match is found, then stop and rebuild relevant parts of the // tree. // If not found, return the same tree instance. static <T> Node<T> apply(Node<T> node, T currentElement, T newElement) { if (Objects.equals(node.getValue(), currentElement)) { return new Node<>(newElement, node.getChildren()); } else { for (Node<T> child : node.getChildren()) { final Node<T> newChild = Replace.apply(child, currentElement, newElement); final boolean found = newChild != child; if (found) { final List<Node<T>> newChildren = node.getChildren().replace(child, newChild); return new Node<>(node.getValue(), newChildren); } } return node; } }
/** * Traverses the Tree in a specific order. * * @param order the tree traversal order * @return A List containing all elements of this tree, which is List if this tree is empty. * @throws java.lang.NullPointerException if order is null */ default Seq<T> traverse(Order order) { Objects.requireNonNull(order, "order is null"); if (isEmpty()) { return Stream.empty(); } else { switch (order) { case PRE_ORDER: return Traversal.preOrder(this); case IN_ORDER: return Traversal.inOrder(this); case POST_ORDER: return Traversal.postOrder(this); case LEVEL_ORDER: return Traversal.levelOrder(this); default: throw new IllegalStateException("Unknown order: " + order.name()); } } }
@Override default <U> Tree<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return isEmpty() ? Empty.instance() : TreeModule.Map.apply((Node<T>) this, mapper); }
/** * 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. */ @SuppressWarnings("varargs") @SafeVarargs static <T> Node<T> of(T value, Node<T>... children) { Objects.requireNonNull(children, "children is null"); return new Node<>(value, List.of(children)); }
/** * 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 Collections.tabulate(n, f, Tree.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 Collections.fill(n, s, Tree.empty(), Tree::of); }
/** * Transforms this {@code Tree}. * * @param f A transformation * @param <U> Type of transformation result * @return An instance of type {@code U} * @throws NullPointerException if {@code f} is null */ default <U> U transform(Function<? super Tree<? super T>, ? extends U> f) { Objects.requireNonNull(f, "f is null"); return f.apply(this); }
@Override default Seq<T> dropUntil(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return dropWhile(predicate.negate()); }
@Override default Seq<T> takeWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return traverse().takeWhile(predicate); }
@Override default Seq<T> filterNot(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return filter(predicate.negate()); }
@Override default Tree<T> replaceAll(T currentElement, T newElement) { return map(t -> Objects.equals(t, currentElement) ? newElement : t); }
@Override default Seq<T> retainAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); return traverse().retainAll(elements); }