KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > AbstractNestedDepthCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.checks.coding;
20
21 import com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23
24 /**
25  * Abstract class which provides helpers functionality for nestedchecks.
26  *
27  * @author <a HREF="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
28  */

29 public abstract class AbstractNestedDepthCheck extends Check
30 {
31     /** maximum allowed nesting depth */
32     private int mMax;
33     /** curren nesting depth */
34     private int mDepth;
35
36     /**
37      * Creates new instance of checks.
38      * @param aMax default allowed nesting depth.
39      */

40     public AbstractNestedDepthCheck(int aMax)
41     {
42         setMax(aMax);
43     }
44
45     /** {@inheritDoc} */
46     public final int[] getRequiredTokens()
47     {
48         return getDefaultTokens();
49     }
50
51     /** {@inheritDoc} */
52     public void beginTree(DetailAST aRootAST)
53     {
54         mDepth = 0;
55     }
56
57     /**
58      * Getter for maximum allowed nesting depth.
59      * @return maximum allowed nesting depth.
60      */

61     public final int getMax()
62     {
63         return mMax;
64     }
65
66     /**
67      * Setter for maximum allowed nesting depth.
68      * @param aMax maximum allowed nesting depth.
69      */

70     public final void setMax(int aMax)
71     {
72         mMax = aMax;
73     }
74
75     /**
76      * Increasing current nesting depth.
77      * @param aAST note which increases nesting.
78      * @param aMessageId message id for logging error.
79      */

80     protected final void nestIn(DetailAST aAST, String JavaDoc aMessageId)
81     {
82         if (mDepth > mMax) {
83             log(aAST, aMessageId, new Integer JavaDoc(mDepth), new Integer JavaDoc(mMax));
84         }
85         ++mDepth;
86     }
87
88     /** Decreasing current nesting depth */
89     protected final void nestOut()
90     {
91         --mDepth;
92     }
93 }
94
Popular Tags