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   * Method or constructor arguments.
42   *
43   * @since 0.18.18
44   */
45  public class Arguments {
46  
47      /**
48       * Parameters.
49       */
50      private final Parameters parameters;
51  
52      /**
53       * Secondary ctor.
54       * @param node Constructor or method defenition node.
55       */
56      public Arguments(final DetailAST node) {
57          this(
58              new Parameters(
59                  node, TokenTypes.PARAMETERS, TokenTypes.PARAMETER_DEF
60              )
61          );
62      }
63  
64      /**
65       * Primary ctor.
66       * @param parameters Parameters.
67       */
68      public Arguments(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 arguments and their Javadoc
82       *  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 arguments"
94              );
95          }
96          final Iterator<JavadocTag> iterator = tags.listIterator();
97          for (final DetailAST param : params) {
98              final String type =
99                  param.findFirstToken(TokenTypes.IDENT).getText();
100             final JavadocTag tag = iterator.next();
101             final String arg = tag.getFirstArg();
102             if (!arg.equals(type)) {
103                 consumer.accept(tag);
104             }
105         }
106     }
107 }