View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle.parameters;
6   
7   import com.puppycrawl.tools.checkstyle.api.DetailAST;
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.List;
11  
12  /**
13   * Abstract parameters. Is used for Generic type parameters or
14   *  method(constructor) arguments.
15   * @since 0.18.18
16   */
17  public class Parameters {
18  
19      /**
20       * Class, interface, constructor or method definition node.
21       */
22      private final DetailAST node;
23  
24      /**
25       * Parent TokenType (TYPE_PARAMETERS or PARAMETERS).
26       * @see com.puppycrawl.tools.checkstyle.api.TokenTypes
27       */
28      private final int parent;
29  
30      /**
31       * Children TokenType (TYPE_PARAMETER or PARAMETER_DEF).
32       * @see com.puppycrawl.tools.checkstyle.api.TokenTypes
33       */
34      private final int children;
35  
36      /**
37       * Primary ctor.
38       * @param node Class, interface, constructor or method definition node
39       * @param parent Parent TokenType (TYPE_PARAMETERS or PARAMETERS)
40       * @param children Children TokenType (TYPE_PARAMETER or PARAMETER_DEF)
41       */
42      public Parameters(
43          final DetailAST node, final int parent, final int children
44      ) {
45          this.node = node;
46          this.parent = parent;
47          this.children = children;
48      }
49  
50      /**
51       * Return number of arguments.
52       * @return Number of parameters
53       */
54      public final int count() {
55          final int result;
56          final DetailAST params = this.node.findFirstToken(this.parent);
57          if (params == null) {
58              result = 0;
59          } else {
60              result = params.getChildCount(this.children);
61          }
62          return result;
63      }
64  
65      /**
66       * Return parameters for this node.
67       * @return Parameters for this node
68       */
69      public final List<DetailAST> parameters() {
70          final List<DetailAST> result;
71          final int count = this.count();
72          if (count == 0) {
73              result = Collections.emptyList();
74          } else {
75              final DetailAST params = this.node.findFirstToken(this.parent);
76              result = new ArrayList<>(count);
77              DetailAST child = params.getFirstChild();
78              while (child != null) {
79                  if (child.getType() == this.children) {
80                      result.add(child);
81                  }
82                  child = child.getNextSibling();
83              }
84          }
85          return result;
86      }
87  }