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