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