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 com.puppycrawl.tools.checkstyle.api.TokenTypes;
9   import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.function.Consumer;
13  
14  /**
15   * Method or constructor arguments.
16   *
17   * @since 0.18.18
18   */
19  public class Arguments {
20  
21      /**
22       * Parameters.
23       */
24      private final Parameters parameters;
25  
26      /**
27       * Secondary ctor.
28       *
29       * @param node Constructor or method definition node
30       */
31      public Arguments(final DetailAST node) {
32          this(
33              new Parameters(
34                  node, TokenTypes.PARAMETERS, TokenTypes.PARAMETER_DEF
35              )
36          );
37      }
38  
39      /**
40       * Primary ctor.
41       *
42       * @param parameters Parameters
43       */
44      public Arguments(final Parameters parameters) {
45          this.parameters = parameters;
46      }
47  
48      /**
49       * Return number of arguments.
50       *
51       * @return Number of arguments
52       */
53      public final int count() {
54          return this.parameters.count();
55      }
56  
57      /**
58       * Checks for consistency the order of arguments and their Javadoc
59       * parameters.
60       *
61       * @param tags Javadoc parameter tags
62       * @param consumer Consumer accepts JavadocTag which is located out of
63       *  order
64       */
65      public final void checkOrder(
66          final List<JavadocTag> tags, final Consumer<JavadocTag> consumer
67      ) {
68          final List<DetailAST> params = this.parameters.parameters();
69          if (tags.size() < params.size()) {
70              throw new IllegalStateException(
71                  "Number of Javadoc parameters does not match the number of arguments"
72              );
73          }
74          final Iterator<JavadocTag> iterator = tags.listIterator();
75          for (final DetailAST param : params) {
76              final String type =
77                  param.findFirstToken(TokenTypes.IDENT).getText();
78              final JavadocTag tag = iterator.next();
79              final String arg = tag.getFirstArg();
80              if (!arg.equals(type)) {
81                  consumer.accept(tag);
82              }
83          }
84      }
85  }