| 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.*; |
| 11 | |
| 12 | import java.util.*; |
| 13 | import java.util.function.*; |
| 14 | import java.util.stream.*; |
| 15 | |
| 16 | /** |
| 17 | * XdmStream extends the capabilities of the standard JDK {@link Stream}. |
| 18 | * |
| 19 | * <p>The extensions are:</p> |
| 20 | * <ul> |
| 21 | * <li>Additional terminal operations are provided, allowing the results of the |
| 22 | * stream to be delivered for example as a <code>List<XdmNode></code> or |
| 23 | * an <code>Optional<XdmNode></code> more conveniently than using the general-purpose |
| 24 | * {@link Collector} interface.</li> |
| 25 | * <li>Many of these terminal operations are short-circuiting, that is, they |
| 26 | * stop processing input when no further input is required.</li> |
| 27 | * <li>The additional terminal operations throw a checked exception if a dynamic |
| 28 | * error occurs while generating the content of the stream.</li> |
| 29 | * </ul> |
| 30 | * |
| 31 | * <p>The implementation is customized to streams of {@link XdmItem}s.</p> |
| 32 | * @implNote This class is implemented by wrapping a base stream. Generally, the |
| 33 | * methods on this class delegate to the base stream; those methods that |
| 34 | * return a stream wrap the stream returned by the base class. The context object can be used by a terminal |
| 35 | * method on the XdmStream to signal to the originator of the stream |
| 36 | * that no further input is required.</p> |
| 37 | * |
| 38 | * @param <T> The type of items delivered by the stream. |
| 39 | */ |
| 40 | |
| 41 | public class XdmStream<T extends XdmItem> implements Stream<T> { |
| 42 | |
| 43 | Stream<T> base; |
| 44 | |
| 45 | /** |
| 46 | * Create an {@link XdmStream} from a general {@link Stream} that returns XDM items. |
| 47 | * @param base the stream of items |
| 48 | */ |
| 49 | |
| 50 | public XdmStream(Stream<T> base) { |
| 51 | this.base = base; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create an {@link XdmStream} consisting of zero or one items, supplied in the form |
| 56 | * of an <code>Optional<XdmItem></code> |
| 57 | * |
| 58 | * @param input the optional item |
| 59 | */ |
| 60 | |
| 61 | @SuppressWarnings("OptionalUsedAsFieldOrParameterType") |
| 62 | public XdmStream(Optional<T> input) { |
| 63 | this.base = input.map(Stream::of).orElseGet(Stream::empty); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Filter a stream of items, to create a new stream containing only those items |
| 68 | * that satisfy a supplied condition |
| 69 | * @param predicate the supplied condition |
| 70 | * @return the filtered stream |
| 71 | */ |
| 72 | |
| 73 | @Override |
| 74 | public XdmStream<T> filter(Predicate<? super T> predicate) { |
| 75 | return new XdmStream<>(base.filter(predicate)); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public <R> Stream<R> map(Function<? super T, ? extends R> mapper) { |
| 80 | return base.map(mapper); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public IntStream mapToInt(ToIntFunction<? super T> mapper) { |
| 85 | return base.mapToInt(mapper); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public LongStream mapToLong(ToLongFunction<? super T> mapper) { |
| 90 | return base.mapToLong(mapper); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) { |
| 95 | return base.mapToDouble(mapper); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) { |
| 100 | return base.flatMap(mapper); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Create a new {@link XdmStream} by applying a mapping function (specifically, a {@link Step}) |
| 105 | * to each item in the stream. The {@link Step} returns a sequence of items, which are inserted |
| 106 | * into the result sequence in place of the original item. |
| 107 | * @param mapper the mapping function |
| 108 | * @param <U> the type of items returned by the mapping function |
| 109 | * @return a new stream of items |
| 110 | */ |
| 111 | |
| 112 | public <U extends XdmItem> XdmStream<U> flatMapToXdm(Step<U> mapper) { |
| 113 | return new XdmStream<>(base.flatMap(mapper)); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) { |
| 118 | return base.flatMapToInt(mapper); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper) { |
| 123 | return base.flatMapToLong(mapper); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) { |
| 128 | return base.flatMapToDouble(mapper); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public XdmStream<T> distinct() { |
| 133 | return new XdmStream<>(base.distinct()); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public XdmStream<T> sorted() { |
| 138 | return new XdmStream<>(base.sorted()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public XdmStream<T> sorted(Comparator<? super T> comparator) { |
| 143 | return new XdmStream<>(base.sorted(comparator)); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public XdmStream<T> peek(Consumer<? super T> action) { |
| 148 | return new XdmStream<>(base.peek(action)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public XdmStream<T> limit(long maxSize) { |
| 153 | return new XdmStream<>(base.limit(maxSize)); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public XdmStream<T> skip(long n) { |
| 158 | return new XdmStream<>(base.skip(n)); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void forEach(Consumer<? super T> action) { |
| 163 | base.forEach(action); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void forEachOrdered(Consumer<? super T> action) { |
| 168 | base.forEachOrdered(action); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Object[] toArray() { |
| 173 | return base.toArray(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public <A> A[] toArray(IntFunction<A[]> generator) { |
| 178 | return base.toArray(generator); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public T reduce(T identity, BinaryOperator<T> accumulator) { |
| 183 | return base.reduce(identity, accumulator); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Optional<T> reduce(BinaryOperator<T> accumulator) { |
| 188 | return base.reduce(accumulator); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) { |
| 193 | return base.reduce(identity, accumulator, combiner); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) { |
| 198 | return base.collect(supplier, accumulator, combiner); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public <R, A> R collect(Collector<? super T, A, R> collector) { |
| 203 | return base.collect(collector); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Optional<T> min(Comparator<? super T> comparator) { |
| 208 | return base.min(comparator); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Optional<T> max(Comparator<? super T> comparator) { |
| 213 | return base.max(comparator); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public long count() { |
| 218 | return base.count(); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public boolean anyMatch(Predicate<? super T> predicate) { |
| 223 | return base.anyMatch(predicate); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean allMatch(Predicate<? super T> predicate) { |
| 228 | return base.allMatch(predicate); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean noneMatch(Predicate<? super T> predicate) { |
| 233 | return base.noneMatch(predicate); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Optional<T> findFirst() { |
| 238 | return base.findFirst(); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public Optional<T> findAny() { |
| 243 | return base.findAny(); |
| 244 | } |
| 245 | |
| 246 | public Iterator<T> iterator() { |
| 247 | return base.iterator(); |
| 248 | } |
| 249 | |
| 250 | public Spliterator<T> spliterator() { |
| 251 | return base.spliterator(); |
| 252 | } |
| 253 | |
| 254 | public boolean isParallel() { |
| 255 | return base.isParallel(); |
| 256 | } |
| 257 | |
| 258 | public Stream<T> sequential() { |
| 259 | return new XdmStream<>(base.sequential()); |
| 260 | } |
| 261 | |
| 262 | public Stream<T> parallel() { |
| 263 | return new XdmStream<>(base.parallel()); |
| 264 | } |
| 265 | |
| 266 | public Stream<T> unordered() { |
| 267 | return new XdmStream<>(base.unordered()); |
| 268 | } |
| 269 | |
| 270 | public Stream<T> onClose(Runnable closeHandler) { |
| 271 | return new XdmStream<>(base.onClose(closeHandler)); |
| 272 | } |
| 273 | |
| 274 | public void close() { |
| 275 | base.close(); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Return the result of the stream as an XdmValue. This is a terminal operation. |
| 280 | * @return the contents of the stream, as an XdmValue. |
| 281 | */ |
| 282 | |
| 283 | public XdmValue asXdmValue() { |
| 284 | return base.collect(XdmCollectors.asXdmValue()); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Return the result of the stream as a <code>List<XdmItem></code>. This is a terminal operation. |
| 289 | * @return the contents of the stream, as a <code>List<XdmItem></code>. |
| 290 | */ |
| 291 | |
| 292 | public List<T> asList() { |
| 293 | return base.collect(Collectors.toList()); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Return the result of the stream as a <code>List<XdmNode></code>. This is a terminal operation. |
| 298 | * @return the list of nodes delivered by the stream |
| 299 | * @throws ClassCastException if the stream contains an item that is not a node |
| 300 | */ |
| 301 | |
| 302 | public List<XdmNode> asListOfNodes() { |
| 303 | return base.collect(XdmCollectors.asListOfNodes()); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Return the result of the stream as an <code>Optional<XdmNode></code>. This is a terminal operation. |
| 308 | * @return the single node delivered by the stream, or absent if the stream is empty |
| 309 | * @throws XdmCollectors.MultipleItemException if the stream contains more than one node |
| 310 | * @throws ClassCastException if the stream contains an item that is not a node |
| 311 | |
| 312 | */ |
| 313 | |
| 314 | public Optional<XdmNode> asOptionalNode() { |
| 315 | return base.collect(XdmCollectors.asOptionalNode()); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Return the result of the stream as an {@link XdmNode}. This is a terminal operation. |
| 320 | * @return the single node delivered by the stream |
| 321 | * @throws ClassCastException if the stream contains an item that is not a node |
| 322 | * @throws XdmCollectors.MultipleItemException if the stream contains |
| 323 | * more than one item |
| 324 | * @throws NoSuchElementException if the stream is empty |
| 325 | */ |
| 326 | |
| 327 | public XdmNode asNode() { |
| 328 | return base.collect(XdmCollectors.asNode()); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Return the result of the stream as a <code>List<XdmAtomicValue></code>. This is a terminal operation. |
| 333 | * |
| 334 | * @return the list of atomic values delivered by the stream |
| 335 | * @throws ClassCastException if the stream contains an item that is not an atomic value |
| 336 | */ |
| 337 | |
| 338 | public List<XdmAtomicValue> asListOfAtomic() { |
| 339 | return base.collect(XdmCollectors.asListOfAtomic()); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Return the result of the stream as an <code>Optional<XdmAtomicValue></code>. This is a terminal operation. |
| 344 | * |
| 345 | * @return the string value of the single item delivered by the stream, or absent if the stream is empty |
| 346 | * @throws XdmCollectors.MultipleItemException if the stream contains more than one item |
| 347 | * @throws ClassCastException if the stream contains an item that is not an atomic value |
| 348 | */ |
| 349 | |
| 350 | public Optional<XdmAtomicValue> asOptionalAtomic() { |
| 351 | return base.collect(XdmCollectors.asOptionalAtomic()); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Return the result of the stream as an {@link XdmAtomicValue}. This is a terminal operation. |
| 356 | * |
| 357 | * @return the string value of the single item delivered by the stream, or a zero-length string |
| 358 | * if the stream is empty |
| 359 | * @throws ClassCastException if the stream contains an item that is not atomic |
| 360 | * @throws XdmCollectors.MultipleItemException if the stream contains more than one item |
| 361 | * @throws NoSuchElementException if the stream is empty |
| 362 | */ |
| 363 | |
| 364 | public XdmAtomicValue asAtomic() { |
| 365 | return base.collect(XdmCollectors.asAtomic()); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | /** |
| 370 | * Return the result of the stream as an <code>Optional<String></code>. This is a terminal operation. |
| 371 | * |
| 372 | * @return the string value of the single item delivered by the stream, or absent if the stream is empty |
| 373 | * @throws XdmCollectors.MultipleItemException if the stream contains more than one item |
| 374 | * @throws UnsupportedOperationException if the stream contains an item that has no string value, |
| 375 | * for example a function item |
| 376 | */ |
| 377 | |
| 378 | public Optional<String> asOptionalString() { |
| 379 | return base.collect(XdmCollectors.asOptionalString()); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Return the result of the stream as an {@link String}. This is a terminal operation. |
| 384 | * |
| 385 | * @return the string value of the single item delivered by the stream |
| 386 | * @throws UnsupportedOperationException if the stream contains an item that has no string value, |
| 387 | * for example a function item |
| 388 | * @throws XdmCollectors.MultipleItemException if the stream contains more than one item |
| 389 | * @throws NoSuchElementException if the stream is empty |
| 390 | */ |
| 391 | |
| 392 | public String asString() { |
| 393 | return base.collect(XdmCollectors.asString()); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Return the first item of this stream, if there is one, discarding the remainder. |
| 398 | * This is a short-circuiting operation similar to {@link #findFirst}, but it returns |
| 399 | * <code>XdmStream<T></code> rather than <code>Optional<T></code> so that further operations such |
| 400 | * as {@code atomize()} can be applied, and so that a typed result can be returned |
| 401 | * using a method such as {@link #asOptionalNode()} or {@link #asOptionalString()} |
| 402 | */ |
| 403 | |
| 404 | public XdmStream<T> first() { |
| 405 | Optional<T> result = base.findFirst(); |
| 406 | return new XdmStream<>(result); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Return true if the stream is non-empty. |
| 411 | * This is a short-circuiting terminal operation. |
| 412 | * @return true if at least one item is present in the stream. |
| 413 | */ |
| 414 | |
| 415 | public boolean exists() { |
| 416 | Optional<T> result = base.findFirst(); |
| 417 | return result.isPresent(); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Return the last item of this stream, if there is one, discarding the remainder. |
| 422 | * This is a short-circuiting operation similar to {@link #first}; it returns |
| 423 | * <code>XdmStream<T></code> rather than <code>Optional<T></code> so that further operations such |
| 424 | * {@code atomize()} can be applied, and so that a typed result can be returned |
| 425 | * using a method such as {@link #asOptionalNode()} or {@link #asOptionalString()} |
| 426 | * @return a stream containing only the last item in the stream, or an empty stream |
| 427 | * if the input is empty. |
| 428 | */ |
| 429 | |
| 430 | public XdmStream<T> last() { |
| 431 | Optional<T> result = base.reduce((first, second) -> second); |
| 432 | return new XdmStream<>(result); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Return the item at a given position in the stream. This is a short-circuiting terminal operation. |
| 437 | * @param position the required position; items in the stream are numbered from zero. |
| 438 | * @return the item at the given position if there is one; otherwise, <code>Optional.empty()</code> |
| 439 | */ |
| 440 | |
| 441 | public Optional<T> at(int position) { |
| 442 | return base.skip(position).findFirst(); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Return the items at a given range of positions in the stream. For example, subStream(0, 3) returns |
| 447 | * the first three items in the stream. |
| 448 | * This is a short-circuiting terminal operation. |
| 449 | * |
| 450 | * @param start the position of the first required item; items in the stream are numbered from zero. |
| 451 | * @param end the position immediately after the last required item. |
| 452 | * @return a stream containing those items whose zero-based position is greater-than-or-equal-to start, and |
| 453 | * less-than end. No error occurs if either start or end is out of range, or if end is less than start. |
| 454 | */ |
| 455 | |
| 456 | public XdmStream<T> subStream(int start, int end) { |
| 457 | if (start < 0) { |
| 458 | start = 0; |
| 459 | } |
| 460 | if (end <= start) { |
| 461 | return new XdmStream<>(Stream.empty()); |
| 462 | } |
| 463 | return new XdmStream<>(base.skip(start).limit(end - start)); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Experimental method to return the content of a stream up to the first item |
| 468 | * that satisfies a given predicate, including that item |
| 469 | * @param predicate a condition that determines when the stream should stop |
| 470 | * @return a stream containing all items in the base stream up to and including |
| 471 | * the first item that satisfies a given predicate. |
| 472 | */ |
| 473 | |
| 474 | public XdmStream<T> untilFirstInclusive(Predicate<? super XdmItem> predicate) { |
| 475 | Stream<T> stoppable = base.peek(item -> { |
| 476 | if (predicate.test(item)) { |
| 477 | base.close(); |
| 478 | } |
| 479 | }); |
| 480 | return new XdmStream<>(stoppable); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Experimental method to return the content of a stream up to the first item |
| 485 | * that satisfies a given predicate, excluding that item |
| 486 | * |
| 487 | * @param predicate a condition that determines when the stream should stop |
| 488 | * @return a stream containing all items in the base stream up to the item immediately before |
| 489 | * the first item that satisfies a given predicate. |
| 490 | */ |
| 491 | |
| 492 | public XdmStream<T> untilFirstExclusive(Predicate<? super XdmItem> predicate) { |
| 493 | Stream<T> stoppable = base.peek(item -> { |
| 494 | if (predicate.test(item)) { |
| 495 | base.close(); |
| 496 | } |
| 497 | }); |
| 498 | return new XdmStream<>(stoppable.filter(predicate.negate())); |
| 499 | } |
| 500 | |
| 501 | } |
| 502 | |
| 503 |