/** * 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, io.vavr.collection.List<Node<T>> children) { Objects.requireNonNull(children, "children is null"); this.value = value; this.children = children; this.size = children.foldLeft(1, (acc, child) -> acc + child.size); }
/** * A combinator that checks if <strong>all</strong> of the given {@code predicates} are satisfied. * <p> * By definition {@code allOf} is satisfied if the given {@code predicates} are empty. * * <pre>{@code * Predicate<Integer> isGreaterThanOne = i -> i > 1; * Predicate<Integer> isGreaterThanTwo = i -> i > 2; * allOf().test(0); // true * allOf(isGreaterThanOne, isGreaterThanTwo).test(3); // true * allOf(isGreaterThanOne, isGreaterThanTwo).test(2); // false * }</pre> * * @param predicates An array of predicates * @param <T> closure over tested object types * @return A new {@code Predicate} * @throws NullPointerException if {@code predicates} is null */ @SuppressWarnings({ "unchecked", "varargs" }) @SafeVarargs public static <T> Predicate<T> allOf(Predicate<T>... predicates) { Objects.requireNonNull(predicates, "predicates is null"); return t -> List.of(predicates).foldLeft(true, (bool, pred) -> bool && pred.test(t)); }
@Override default List<T> reverse() { return (length() <= 1) ? this : foldLeft(empty(), List::prepend); }
/** * Enqueues the given elements. A queue has FIFO order, i.e. the first of the given elements is * the first which will be retrieved. * * @param elements An Iterable of elements, may be empty * @return a new {@code Queue} instance, containing the new elements * @throws NullPointerException if elements is null */ @SuppressWarnings("unchecked") public Queue<T> enqueueAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty() && elements instanceof Queue) { return (Queue<T>) elements; } else { return io.vavr.collection.List.ofAll(elements).foldLeft(this, Queue::enqueue); } }
@Override default List<T> prependAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); return isEmpty() ? ofAll(elements) : ofAll(elements).reverse().foldLeft(this, List::prepend); }
/** * Counts the number of branches of this tree. The empty tree and a leaf have no branches. * * @return The number of branches of this tree. */ default int branchCount() { if (isEmpty() || isLeaf()) { return 0; } else { return getChildren().foldLeft(1, (count, child) -> count + child.branchCount()); } }
static <T> Stream<Node<T>> traversePreOrder(Node<T> node) { return node.getChildren().foldLeft(Stream.of(node), (acc, child) -> acc.appendAll(traversePreOrder(child))); }
/** * Counts the number of leaves of this tree. The empty tree has no leaves. * * @return The number of leaves of this tree. */ default int leafCount() { if (isEmpty()) { return 0; } else if (isLeaf()) { return 1; } else { return getChildren().foldLeft(0, (count, child) -> count + child.leafCount()); } }
static <T> Stream<Node<T>> traversePostOrder(Node<T> node) { return node .getChildren() .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traversePostOrder(child))) .append(node); }
@Override default List<T> removeFirst(Predicate<T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); List<T> init = empty(); List<T> tail = this; while (!tail.isEmpty() && !predicate.test(tail.head())) { init = init.prepend(tail.head()); tail = tail.tail(); } if (tail.isEmpty()) { return this; } else { return init.foldLeft(tail.tail(), List::prepend); } }
@Override default List<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return this; } else { final List<T> filtered = foldLeft(empty(), (xs, x) -> predicate.test(x) ? xs.prepend(x) : xs); if (filtered.isEmpty()) { return empty(); } else if (filtered.length() == length()) { return this; } else { return filtered.reverse(); } } }
static <T> Stream<Node<T>> traverseInOrder(Node<T> node) { if (node.isLeaf()) { return Stream.of(node); } else { final io.vavr.collection.List<Node<T>> children = node.getChildren(); return children .tail() .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traverseInOrder(child))) .prepend(node) .prependAll(traverseInOrder(children.head())); } }
@Override default List<List<T>> permutations() { if (isEmpty()) { return Nil.instance(); } else { final List<T> tail = tail(); if (tail.isEmpty()) { return of(this); } else { final List<List<T>> zero = Nil.instance(); return distinct().foldLeft(zero, (xs, x) -> { final Function<List<T>, List<T>> prepend = l -> l.prepend(x); return xs.appendAll(remove(x).permutations().map(prepend)); }); } } }
@Override public final boolean matches(T target) { return matchers.foldLeft(false, (v, m) -> v || m.matches(target)); } }
@Override public final Builder<?> transitionResult(Builder<?> source, TypeDescription typeDescription) { return transitions.<Builder<?>>foldLeft( source, (state, transition) -> transition.transitionResult(state, typeDescription) ); } }
@Override default List<T> prependAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); return isEmpty() ? ofAll(elements) : ofAll(elements).reverse().foldLeft(this, List::prepend); }
/** * Counts the number of branches of this tree. The empty tree and a leaf have no branches. * * @return The number of branches of this tree. */ default int branchCount() { if (isEmpty() || isLeaf()) { return 0; } else { return getChildren().foldLeft(1, (count, child) -> count + child.branchCount()); } }
static <T> Stream<Node<T>> traversePreOrder(Node<T> node) { return node.getChildren().foldLeft(Stream.of(node), (acc, child) -> acc.appendAll(traversePreOrder(child))); }
static <T> Stream<Node<T>> traversePostOrder(Node<T> node) { return node .getChildren() .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traversePostOrder(child))) .append(node); }
static <T> Stream<Node<T>> traverseInOrder(Node<T> node) { if (node.isLeaf()) { return Stream.of(node); } else { final io.vavr.collection.List<Node<T>> children = node.getChildren(); return children .tail() .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traverseInOrder(child))) .prepend(node) .prependAll(traverseInOrder(children.head())); } }