Coverage Report - com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
OuterTypeNumberCheck
100%
17/17
75%
3/4
1.333
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  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.sizes;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 import com.puppycrawl.tools.checkstyle.api.Check;
 24  
 
 25  
 /**
 26  
  * Checks for the number of defined types at the "outer" level.
 27  
  * @author oliverb
 28  
  */
 29  2
 public class OuterTypeNumberCheck extends Check
 30  
 {
 31  
     /** The maximum allowed number of outer types. */
 32  2
     private int mMax = 1;
 33  
     /** Tracks the current depth in types. */
 34  
     private int mCurrentDepth;
 35  
     /** Tracks the number of outer types found. */
 36  
     private int mOuterNum;
 37  
 
 38  
     @Override
 39  
     public int[] getDefaultTokens()
 40  
     {
 41  2
         return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
 42  
             TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, };
 43  
     }
 44  
 
 45  
     @Override
 46  
     public void beginTree(DetailAST aAst)
 47  
     {
 48  2
         mCurrentDepth = 0;
 49  2
         mOuterNum = 0;
 50  2
     }
 51  
 
 52  
     @Override
 53  
     public void finishTree(DetailAST aAst)
 54  
     {
 55  2
         if (mMax < mOuterNum) {
 56  1
             log(aAst, "maxOuterTypes", mOuterNum, mMax);
 57  
         }
 58  2
     }
 59  
 
 60  
     @Override
 61  
     public void visitToken(DetailAST aAst)
 62  
     {
 63  6
         if (0 == mCurrentDepth) {
 64  6
             mOuterNum++;
 65  
         }
 66  6
         mCurrentDepth++;
 67  6
     }
 68  
 
 69  
     @Override
 70  
     public void leaveToken(DetailAST aAst)
 71  
     {
 72  6
         mCurrentDepth--;
 73  6
     }
 74  
 
 75  
     /**
 76  
      * Sets the maximum allowed number of outer types.
 77  
      * @param aTo the new number.
 78  
      */
 79  
     public void setMax(int aTo)
 80  
     {
 81  1
         mMax = aTo;
 82  1
     }
 83  
 }