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   * Class, interface, constructor or method generic type parameters.
16   *
17   * @since 0.18.18
18   */
19  public class TypeParameters {
20  
21      /**
22       * Parameters.
23       */
24      private final Parameters parameters;
25  
26      /**
27       * Secondary ctor.
28       *
29       * @param node Class, interface, constructor or method definition node
30       */
31      public TypeParameters(final DetailAST node) {
32          this(
33              new Parameters(
34                  node, TokenTypes.TYPE_PARAMETERS, TokenTypes.TYPE_PARAMETER
35              )
36          );
37      }
38  
39      /**
40       * Primary ctor.
41       *
42       * @param parameters Parameters
43       */
44      public TypeParameters(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 generic type parameters and
59       * their Javadoc 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 type parameters"
72              );
73          }
74          final Iterator<JavadocTag> iterator =
75              tags.listIterator(tags.size() - params.size());
76          for (final DetailAST param : params) {
77              final String type =
78                  param.findFirstToken(TokenTypes.IDENT).getText();
79              final JavadocTag tag = iterator.next();
80              final String arg = tag.getFirstArg();
81              if (!arg.equals(String.format("<%s>", type))) {
82                  consumer.accept(tag);
83              }
84          }
85      }
86  }