View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 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   * 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.
17   *
18   * @since 1.0
19   */
20  class ChildStream {
21      /**
22       * Node, whose children will be traversed by this ChildStream object.
23       */
24      private final DetailAST node;
25  
26      /**
27       * Creates a new child stream factory.
28       *
29       * @param node Node which will used by this object.
30       */
31      ChildStream(final DetailAST node) {
32          this.node = node;
33      }
34  
35      /**
36       * Creates a new stream which sequentially yields all
37       * children. Any two streams returned by this method are
38       * independent.
39       *
40       * Implementation may be simplified using Stream.iterate when Java 8 support
41       * is dropped.
42       *
43       * @return Stream of children
44       */
45      Stream<DetailAST> children() {
46          final Stream.Builder<DetailAST> builder = Stream.builder();
47          DetailAST child = this.node.getFirstChild();
48          while (child != null) {
49              builder.accept(child);
50              child = child.getNextSibling();
51          }
52          return builder.build();
53      }
54  }