View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle.parameters;
32  
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.function.Consumer;
39  
40  /**
41   * Class, interface, constructor or method generic type parameters.
42   *
43   * @since 0.18.18
44   */
45  public class TypeParameters {
46  
47      /**
48       * Parameters.
49       */
50      private final Parameters parameters;
51  
52      /**
53       * Secondary ctor.
54       * @param node Class, interface, constructor or method defenition node.
55       */
56      public TypeParameters(final DetailAST node) {
57          this(
58              new Parameters(
59                  node, TokenTypes.TYPE_PARAMETERS, TokenTypes.TYPE_PARAMETER
60              )
61          );
62      }
63  
64      /**
65       * Primary ctor.
66       * @param parameters Parameters.
67       */
68      public TypeParameters(final Parameters parameters) {
69          this.parameters = parameters;
70      }
71  
72      /**
73       * Return number of arguments.
74       * @return Number of arguments.
75       */
76      public final int count() {
77          return this.parameters.count();
78      }
79  
80      /**
81       * Checks for consistency the order of generic type parameters and
82       * their Javadoc parameters.
83       * @param tags Javadoc parameter tags.
84       * @param consumer Consumer accepts JavadocTag which is located out of
85       *  order.
86       */
87      public final void checkOrder(
88          final List<JavadocTag> tags, final Consumer<JavadocTag> consumer
89      ) {
90          final List<DetailAST> params = this.parameters.parameters();
91          if (tags.size() < params.size()) {
92              throw new IllegalStateException(
93                  "Number of Javadoc parameters does not match the number of type parameters"
94              );
95          }
96          final Iterator<JavadocTag> iterator =
97              tags.listIterator(tags.size() - params.size());
98          for (final DetailAST param : params) {
99              final String type =
100                 param.findFirstToken(TokenTypes.IDENT).getText();
101             final JavadocTag tag = iterator.next();
102             final String arg = tag.getFirstArg();
103             if (!arg.equals(String.format("<%s>", type))) {
104                 consumer.accept(tag);
105             }
106         }
107     }
108 }