| 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 | |
| 9 | package net.sf.saxon.s9api.streams; |
| 10 | |
| 11 | import net.sf.saxon.s9api.*; |
| 12 | |
| 13 | import java.util.*; |
| 14 | import java.util.function.BiConsumer; |
| 15 | import java.util.function.BinaryOperator; |
| 16 | import java.util.function.Function; |
| 17 | import java.util.function.Supplier; |
| 18 | import java.util.stream.Collector; |
| 19 | import java.util.stream.Stream; |
| 20 | |
| 21 | /** |
| 22 | * This class contains a number of static methods that deliver implementations of the {@link java.util.stream.Collector} |
| 23 | * interface suitable for use with streams processing XDM nodes and other items. |
| 24 | * <p>For example, the method {@code asNode} can be used in an expression such as |
| 25 | * {@code XdmNode n = x.select(child("author")).collect(asNode())} to indicate that the content of the stream |
| 26 | * delivered by the {@code select()} call is to be delivered as a single XdmNode object, and that an exception |
| 27 | * should occur if the result is anything other than a single node.</p> |
| 28 | * <p>Although these methods can be used directly as arguments to {@link Stream#collect}, it is usually more convenient |
| 29 | * to use them indirectly, the form of terminal operations on the class {@link XdmStream}, which extends {@link Stream}. |
| 30 | * So a more usual usage would be {@code XdmNode n = x.select(child("author")).asNode()}</p> |
| 31 | */ |
| 32 | |
| 33 | public class XdmCollectors { |
| 34 | |
| 35 | /** |
| 36 | * Unchecked exception that occurs when a collector method such as {@link #asAtomic} or {@link #asOptionalNode} |
| 37 | * is called, and the sequence contains more than one item. |
| 38 | */ |
| 39 | public static class MultipleItemException extends RuntimeException {} |
| 40 | |
| 41 | /** |
| 42 | * Abstract superclass for collectors of XDM items |
| 43 | * @param <R> the type of the result of the collector, for example {@code XdmNode} or <code>Optional<XdmNode></code> |
| 44 | * @param <I> the type of the items in the stream, for example {@code XdmNode} or {@code XdmAtomicValue} |
| 45 | */ |
| 46 | |
| 47 | private abstract static class XdmCollector<R, I extends XdmItem> implements Collector<XdmItem, List<I>, R> { |
| 48 | |
| 49 | protected void onEmpty() { |
| 50 | } |
| 51 | |
| 52 | protected void onMultiple() { |
| 53 | } |
| 54 | |
| 55 | protected I convert(XdmItem item) { |
| 56 | //noinspection unchecked |
| 57 | return (I)item; |
| 58 | } |
| 59 | |
| 60 | protected abstract R makeResult(List<I> list); |
| 61 | |
| 62 | @Override |
| 63 | public Supplier<List<I>> supplier() { |
| 64 | return ArrayList::new; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public BiConsumer<List<I>, XdmItem> accumulator() { |
| 69 | return (list, next) -> { |
| 70 | I item = convert(next); |
| 71 | if (!list.isEmpty()) { |
| 72 | onMultiple(); |
| 73 | } |
| 74 | list.add(item); |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public BinaryOperator<List<I>> combiner() { |
| 80 | return (list1, list2) -> { |
| 81 | list1.addAll(list2); |
| 82 | if (list1.size() > 1) { |
| 83 | onMultiple(); |
| 84 | } |
| 85 | return list1; |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Function<List<I>, R> finisher() { |
| 91 | return list -> { |
| 92 | if (list.isEmpty()) { |
| 93 | onEmpty(); |
| 94 | } |
| 95 | return makeResult(list); |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Set<Characteristics> characteristics() { |
| 101 | return Collections.emptySet(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * This method provides a Collector that returns the content of a stream as an {@link XdmValue} |
| 107 | * @return a collector that returns the single node delivered by the stream, or null if the stream is empty |
| 108 | * @throws ClassCastException if the stream contains an item that is not a node |
| 109 | */ |
| 110 | |
| 111 | public static XdmCollector<XdmValue, XdmItem> asXdmValue() { |
| 112 | return new XdmCollector<XdmValue, XdmItem>() { |
| 113 | @Override |
| 114 | protected XdmValue makeResult(List<XdmItem> list) { |
| 115 | return new XdmValue(list); |
| 116 | } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * This method provides a Collector that returns the content of a stream as a single {@link XdmNode}. |
| 122 | * @return a collector that returns the single node delivered by the stream |
| 123 | * @throws NoSuchElementException if the stream is empty |
| 124 | * @throws MultipleItemException if the stream contains more than one node |
| 125 | * @throws ClassCastException if the stream contains an item that is not a node |
| 126 | */ |
| 127 | |
| 128 | public static XdmCollector<XdmNode, XdmNode> asNode() { |
| 129 | return new XdmCollector<XdmNode, XdmNode>() { |
| 130 | @Override |
| 131 | protected void onEmpty() { |
| 132 | throw new NoSuchElementException(); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected void onMultiple() { |
| 137 | throw new MultipleItemException(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | protected XdmNode convert(XdmItem item) { |
| 142 | return (XdmNode)item; // Deliberate ClassCastException if not a node |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected XdmNode makeResult(List<XdmNode> list) { |
| 147 | return list.get(0); |
| 148 | } |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * This method provides a Collector that returns the content of a stream as an optional {@link XdmNode} |
| 154 | * (that is, as an instance of <code>Optional<XdmNode></code>) |
| 155 | * |
| 156 | * @return a collector that returns the single node delivered by the stream, or |
| 157 | * {@code Optional#empty()} if the stream is empty |
| 158 | * @throws MultipleItemException if the stream contains more than one node |
| 159 | * @throws ClassCastException if the stream contains an item that is not a node |
| 160 | */ |
| 161 | |
| 162 | public static XdmCollector<Optional<XdmNode>, XdmNode> asOptionalNode() { |
| 163 | return new XdmCollector<Optional<XdmNode>, XdmNode>() { |
| 164 | @Override |
| 165 | protected void onEmpty() { |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void onMultiple() { |
| 170 | throw new MultipleItemException(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | protected XdmNode convert(XdmItem item) { |
| 175 | return (XdmNode) item; // Deliberate ClassCastException if not a node |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | protected Optional<XdmNode> makeResult(List<XdmNode> list) { |
| 180 | return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); |
| 181 | } |
| 182 | }; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * This method provides a Collector that returns the content of a stream as a list of {@link XdmNode} objects |
| 187 | * (that is, as an instance of <code>List<XdmNode></code>) |
| 188 | * |
| 189 | * @return a collector that returns the single node delivered by the stream, or null if the stream is empty |
| 190 | * @throws ClassCastException if the stream contains an item that is not a node |
| 191 | */ |
| 192 | |
| 193 | public static XdmCollector<List<XdmNode>, XdmNode> asListOfNodes() { |
| 194 | return new XdmCollector<List<XdmNode>, XdmNode>() { |
| 195 | @Override |
| 196 | protected void onEmpty() { |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | protected void onMultiple() { |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | protected XdmNode convert(XdmItem item) { |
| 205 | return (XdmNode) item; // Deliberate ClassCastException if not a node |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | protected List<XdmNode> makeResult(List<XdmNode> list) { |
| 210 | return list; |
| 211 | } |
| 212 | }; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * This method provides a Collector that returns the content of a stream as a list of atomic values |
| 217 | * (that is, as an instance of <code>List<XdmAtomicValue></code>) |
| 218 | * |
| 219 | * @return a collector that returns the list of atomic values delivered by the stream |
| 220 | * @throws ClassCastException if the stream contains an item that is not an atomic value |
| 221 | */ |
| 222 | |
| 223 | public static XdmCollector<List<XdmAtomicValue>, XdmAtomicValue> asListOfAtomic() { |
| 224 | return new XdmCollector<List<XdmAtomicValue>, XdmAtomicValue>() { |
| 225 | @Override |
| 226 | protected void onEmpty() { |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | protected void onMultiple() { |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | protected XdmAtomicValue convert(XdmItem item) { |
| 235 | return (XdmAtomicValue) item; // Deliberate ClassCastException if not an atomic value |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | protected List<XdmAtomicValue> makeResult(List<XdmAtomicValue> list) { |
| 240 | return list; |
| 241 | } |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * This method provides a Collector that returns the content of a stream as an optional atomic value |
| 247 | * (that is, as an instance of <code>Optional<XdmAtomicValue></code>) |
| 248 | * |
| 249 | * @return a collector that returns the single atomic value delivered by the stream, or |
| 250 | * {@link Optional#empty()} if the stream is empty |
| 251 | * @throws ClassCastException if the stream contains an item that is not a node |
| 252 | */ |
| 253 | |
| 254 | public static XdmCollector<Optional<XdmAtomicValue>, XdmAtomicValue> asOptionalAtomic() { |
| 255 | return new XdmCollector<Optional<XdmAtomicValue>, XdmAtomicValue>() { |
| 256 | @Override |
| 257 | protected void onEmpty() { |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | protected void onMultiple() { |
| 262 | throw new MultipleItemException(); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | protected XdmAtomicValue convert(XdmItem item) { |
| 267 | return (XdmAtomicValue) item; // Deliberate ClassCastException if not an atomic value |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | protected Optional<XdmAtomicValue> makeResult(List<XdmAtomicValue> list) { |
| 272 | return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); |
| 273 | } |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * This method provides a Collector that returns the content of a stream as a single atomic |
| 280 | * value, that is, an instance of {@link XdmAtomicValue}. |
| 281 | * The stream must deliver a single atomic value. |
| 282 | * |
| 283 | * @return a collector that returns the string value of the single item delivered by the stream |
| 284 | * @throws NoSuchElementException if the stream is empty |
| 285 | * @throws MultipleItemException if the stream contains more than one item |
| 286 | * @throws ClassCastException if the stream delivers an item that is not an atomic value |
| 287 | */ |
| 288 | |
| 289 | public static XdmCollector<XdmAtomicValue, XdmAtomicValue> asAtomic() { |
| 290 | return new XdmCollector<XdmAtomicValue, XdmAtomicValue>() { |
| 291 | @Override |
| 292 | protected void onEmpty() { |
| 293 | throw new NoSuchElementException(); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | protected void onMultiple() { |
| 298 | throw new MultipleItemException(); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | protected XdmAtomicValue convert(XdmItem item) { |
| 303 | return (XdmAtomicValue) item; // Deliberate ClassCastException if not an atomic value |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | protected XdmAtomicValue makeResult(List<XdmAtomicValue> list) { |
| 308 | return list.get(0); |
| 309 | } |
| 310 | }; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * This method provides a Collector that returns the content of a stream as an optional String |
| 315 | * (that is, as an instance of <code>Optional<String></code>) |
| 316 | * The stream must deliver either nothing, or a single {@code XdmItem}; the collector returns |
| 317 | * the string value of that item. |
| 318 | * |
| 319 | * @return a collector that returns the string value of the single item delivered by the stream, if any |
| 320 | * @throws MultipleItemException if the stream contains more than one item |
| 321 | * @throws UnsupportedOperationException if the stream contains an item with no string value (such |
| 322 | * as a function item or an element with element-only content) |
| 323 | */ |
| 324 | |
| 325 | public static XdmCollector<Optional<String>, XdmItem> asOptionalString() { |
| 326 | return new XdmCollector<Optional<String>, XdmItem>() { |
| 327 | |
| 328 | @Override |
| 329 | protected void onMultiple() { |
| 330 | throw new MultipleItemException(); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | protected Optional<String> makeResult(List<XdmItem> list) { |
| 335 | return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0).getStringValue()); |
| 336 | } |
| 337 | }; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * This method provides a Collector that returns the content of a stream as an optional String |
| 342 | * (that is, as an instance of <code>Optional<String></code>) |
| 343 | * The stream must deliver a single {@code XdmItem}; the collector returns the string value |
| 344 | * of that item. |
| 345 | * |
| 346 | * @return a collector that returns the string value of the single item delivered by the stream, if any |
| 347 | * @throws MultipleItemException if the stream contains more than one item |
| 348 | * @throws NoSuchElementException if the stream is empty |
| 349 | * @throws UnsupportedOperationException if the stream contains an item with no string value (such |
| 350 | * as a function item or an element with element-only content) |
| 351 | */ |
| 352 | |
| 353 | public static XdmCollector<String, XdmItem> asString() { |
| 354 | return new XdmCollector<String, XdmItem>() { |
| 355 | |
| 356 | @Override |
| 357 | protected void onEmpty() { |
| 358 | throw new NoSuchElementException(); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | protected void onMultiple() { |
| 363 | throw new MultipleItemException(); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | protected String makeResult(List<XdmItem> list) { |
| 368 | return list.get(0).getStringValue(); |
| 369 | } |
| 370 | }; |
| 371 | } |
| 372 | |
| 373 | } |
| 374 |