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