1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3 * SPDX-License-Identifier: MIT
4 */
5 package com.qulice.checkstyle;
6
7 import com.puppycrawl.tools.checkstyle.api.DetailAST;
8 import java.util.stream.Stream;
9
10 /**
11 * Utility class which simplifies traversing DetailAST objects.
12 *
13 * <p>DetailAST APIs for working with child nodes require writing
14 * imperative code, which generally looks less readable then
15 * declarative Stream manipulations. This class integrates DetailAST
16 * with Java Streams.</p>
17 *
18 * @since 1.0
19 */
20 class ChildStream {
21
22 /**
23 * Node, whose children will be traversed by this ChildStream object.
24 */
25 private final DetailAST node;
26
27 /**
28 * Creates a new child stream factory.
29 *
30 * @param node Node which will used by this object
31 */
32 ChildStream(final DetailAST node) {
33 this.node = node;
34 }
35
36 /**
37 * Creates a new stream which sequentially yields all
38 * children. Any two streams returned by this method are
39 * independent.
40 *
41 * <p>Implementation may be simplified using Stream.iterate when Java 8 support
42 * is dropped.</p>
43 *
44 * @return Stream of children
45 */
46 Stream<DetailAST> children() {
47 final Stream.Builder<DetailAST> builder = Stream.builder();
48 DetailAST child = this.node.getFirstChild();
49 while (child != null) {
50 builder.accept(child);
51 child = child.getNextSibling();
52 }
53 return builder.build();
54 }
55 }