| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.s9api.streams; |
| 9 | |
| 10 | import net.sf.saxon.s9api.XdmItem; |
| 11 | |
| 12 | import java.util.function.Function; |
| 13 | import java.util.function.Predicate; |
| 14 | import java.util.stream.Stream; |
| 15 | |
| 16 | /** |
| 17 | * A {@code Step} is a function that can be applied to an item |
| 18 | * to return a stream of items. |
| 19 | * @param <T> the type of {@link XdmItem} that is returned by the step. (Note that we |
| 20 | * don't parameterize Steps by the type of input item). |
| 21 | * @see Steps |
| 22 | */ |
| 23 | |
| 24 | public abstract class Step<T extends XdmItem> |
| 25 | implements Function<XdmItem, Stream<? extends T>> { |
| 26 | |
| 27 | /** |
| 28 | * Obtain a {@link Step} that filters the results of this Step using a supplied predicate. |
| 29 | * <p>For example, {@code CHILD.where(isText())} returns a Step whose effect is |
| 30 | * to select the text node children of a supplied element or document node.</p> |
| 31 | * @param predicate the predicate which will be applied to the results of this step |
| 32 | * @return a new Step (that is, a function from one Stream of items to another) that |
| 33 | * filters the results of this step by selecting only the items that satisfy the predicate. |
| 34 | */ |
| 35 | |
| 36 | public Step<T> where(Predicate<? super T> predicate) { |
| 37 | Step<T> base = this; |
| 38 | return new Step<T>() { |
| 39 | @Override |
| 40 | public Stream<? extends T> apply(XdmItem item) { |
| 41 | return base.apply(item).filter(predicate); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Obtain a {@link Step} that concatenates the results of this Step with the result of another |
| 48 | * Step applied to the same input item. |
| 49 | * <p>For example, {@code attribute().cat(child())} returns a step whose effect is |
| 50 | * to select the attributes of a supplied element followed by its children.</p> |
| 51 | * |
| 52 | * @param other the step whose results will be concatenated with the results of this step |
| 53 | * @return a new Step (that is, a function from one Stream of items to another) that |
| 54 | * concatenates the results of applying this step to the input item, followed by the |
| 55 | * results of applying the other step to the input item. |
| 56 | */ |
| 57 | |
| 58 | public Step<T> cat(Step<T> other) { |
| 59 | Step<T> base = this; |
| 60 | return new Step<T>() { |
| 61 | @Override |
| 62 | public Stream<T> apply(XdmItem item) { |
| 63 | return Stream.concat(base.apply(item), other.apply(item)); |
| 64 | } |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Obtain a step that selects the first item in the results of this step |
| 70 | * |
| 71 | * @return a new Step (that is, a function from one Stream of items to another) that |
| 72 | * filters the results of this step by selecting only the first item. |
| 73 | */ |
| 74 | |
| 75 | public Step<T> first() { |
| 76 | final Step<T> base = this; |
| 77 | return new Step<T>() { |
| 78 | @Override |
| 79 | public Stream<? extends T> apply(XdmItem item) { |
| 80 | return base.apply(item).limit(1); |
| 81 | } |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Obtain a step that selects the last item in the results of this step |
| 87 | * |
| 88 | * @return a new Step (that is, a function from one Stream of items to another) that |
| 89 | * filters the results of this step by selecting only the last item. |
| 90 | */ |
| 91 | |
| 92 | public Step<T> last() { |
| 93 | final Step<T> base = this; |
| 94 | return new Step<T>() { |
| 95 | @Override |
| 96 | public Stream<? extends T> apply(XdmItem item) { |
| 97 | return base.apply(item).reduce((first, second) -> second) |
| 98 | .map(Stream::of).orElseGet(Stream::empty); |
| 99 | } |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Obtain a step that selects the Nth item in the results of this step |
| 105 | * |
| 106 | * @param index the zero-based index of the item to be selected |
| 107 | * @return a new Step (that is, a function from one Stream of items to another) that |
| 108 | * filters the results of this step by selecting only the items that satisfy the predicate. |
| 109 | */ |
| 110 | |
| 111 | public Step<T> at(long index) { |
| 112 | final Step<T> base = this; |
| 113 | return new Step<T>() { |
| 114 | @Override |
| 115 | public Stream<? extends T> apply(XdmItem item) { |
| 116 | return base.apply(item).skip(index).limit(1); |
| 117 | } |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Obtain a step that combines the results of this step with the results of another step |
| 124 | * |
| 125 | * @param next the step which will be applied to the results of this step |
| 126 | * @return a new Step (that is, a function from one Stream of items to another) that |
| 127 | * performs this step and the next step in turn. The result is equivalent to the Java {code flatMap()} |
| 128 | * function or the XPath {code !} operator: there is no sorting of nodes into document order, and |
| 129 | * no elimination of duplicates. |
| 130 | */ |
| 131 | |
| 132 | public <U extends XdmItem> Step<U> then(Step<U> next) { |
| 133 | Step<T> me = this; |
| 134 | return new Step<U>() { |
| 135 | @Override |
| 136 | public Stream<? extends U> apply(XdmItem item) { |
| 137 | return me.apply(item).flatMap(next); |
| 138 | } |
| 139 | }; |
| 140 | } |
| 141 | } |
| 142 |