Coverage Report - com.puppycrawl.tools.checkstyle.checks.regexp.DetectorOptions
 
Classes in this File Line Coverage Branch Coverage Complexity
DetectorOptions
96%
26/27
100%
2/2
1.067
 
 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.regexp;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter;
 22  
 import com.puppycrawl.tools.checkstyle.api.Utils;
 23  
 import java.util.regex.Pattern;
 24  
 
 25  
 /**
 26  
  * Options for a detector.
 27  
  * @author Oliver Burn
 28  
  */
 29  
 class DetectorOptions
 30  
 {
 31  
     /**
 32  
      * Flags to compile a regular expression with.
 33  
      * See {@link Pattern#flags()}.
 34  
      */
 35  
     private final int mCompileFlags;
 36  
     /** Used for reporting violations. */
 37  
     private final AbstractViolationReporter mReporter;
 38  
     /** Format of the regular expression to check for. */
 39  
     private String mFormat;
 40  
     /** The message to report on detection. If blank, then use the format. */
 41  27
     private String mMessage = "";
 42  
     /** Minimum number of times regular expression should occur in a file. */
 43  
     private int mMinimum;
 44  
     /** Maximum number of times regular expression should occur in a file. */
 45  
     private int mMaximum;
 46  
     /** Whether to ignore case when matching. */
 47  
     private boolean mIgnoreCase;
 48  
     /** Used to determine whether to suppress a detected match. */
 49  27
     private MatchSuppressor mSuppressor = NeverSuppress.INSTANCE;
 50  
 
 51  
     /**
 52  
      * Creates an instance.
 53  
      * @param aCompileFlags the flags to create the regular expression with.
 54  
      * @param aReporter used to report violations.
 55  
      */
 56  
     public DetectorOptions(int aCompileFlags,
 57  
             AbstractViolationReporter aReporter)
 58  27
     {
 59  27
         mCompileFlags = aCompileFlags;
 60  27
         mReporter = aReporter;
 61  27
     }
 62  
 
 63  
     /**
 64  
      * The format to use when matching lines.
 65  
      * @param aFormat the format to use when matching lines.
 66  
      * @return current instance
 67  
      */
 68  
     public DetectorOptions setFormat(String aFormat)
 69  
     {
 70  27
         mFormat = aFormat;
 71  27
         return this;
 72  
     }
 73  
 
 74  
     /**
 75  
      * Message to use when reporting a match.
 76  
      * @param aMessage message to use when reporting a match.
 77  
      * @return current instance.
 78  
      */
 79  
     public DetectorOptions setMessage(String aMessage)
 80  
     {
 81  3
         mMessage = aMessage;
 82  3
         return this;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Set the minimum allowed number of detections.
 87  
      * @param aMinimum the minimum allowed number of detections.
 88  
      * @return current instance
 89  
      */
 90  
     public DetectorOptions setMinimum(int aMinimum)
 91  
     {
 92  3
         mMinimum = aMinimum;
 93  3
         return this;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Set the maximum allowed number of detections.
 98  
      * @param aMaximum the maximum allowed number of detections.
 99  
      * @return current instance
 100  
      */
 101  
     public DetectorOptions setMaximum(int aMaximum)
 102  
     {
 103  3
         mMaximum = aMaximum;
 104  3
         return this;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Set the suppressor to use.
 109  
      * @param aSup the suppressor to use.
 110  
      * @return current instance
 111  
      */
 112  
     public DetectorOptions setSuppressor(MatchSuppressor aSup)
 113  
     {
 114  11
         mSuppressor = aSup;
 115  11
         return this;
 116  
     }
 117  
 
 118  
     /**
 119  
      * Set whether to ignore case when matching.
 120  
      * @param aIgnore whether to ignore case when matching.
 121  
      * @return current instance
 122  
      */
 123  
     public DetectorOptions setIgnoreCase(boolean aIgnore)
 124  
     {
 125  6
         mIgnoreCase = aIgnore;
 126  6
         return this;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Format of the regular expression.
 131  
      * @return format of the regular expression.
 132  
      */
 133  
     public String getFormat()
 134  
     {
 135  1
         return mFormat;
 136  
     }
 137  
 
 138  
     /**
 139  
      * The violation reporter to use.
 140  
      * @return the violation reporter to use.
 141  
      */
 142  
     public AbstractViolationReporter getReporter()
 143  
     {
 144  17
         return mReporter;
 145  
     }
 146  
 
 147  
     /**
 148  
      * The message to report errors with.
 149  
      * @return the message to report errors with.
 150  
      */
 151  
     public String getMessage()
 152  
     {
 153  20
         return mMessage;
 154  
     }
 155  
 
 156  
     /**
 157  
      * The minimum number of allowed detections.
 158  
      * @return the minimum number of allowed detections.
 159  
      */
 160  
     public int getMinimum()
 161  
     {
 162  28
         return mMinimum;
 163  
     }
 164  
 
 165  
     /**
 166  
      * The maximum number of allowed detections.
 167  
      * @return the maximum number of allowed detections.
 168  
      */
 169  
     public int getMaximum()
 170  
     {
 171  21
         return mMaximum;
 172  
     }
 173  
 
 174  
     /**
 175  
      * The suppressor to use.
 176  
      * @return the suppressor to use.
 177  
      */
 178  
     public MatchSuppressor getSuppressor()
 179  
     {
 180  28
         return mSuppressor;
 181  
     }
 182  
 
 183  
     /**
 184  
      * Whether to ignore case when matching.
 185  
      * @return whether to ignore case when matching.
 186  
      */
 187  
     public boolean isIgnoreCase()
 188  
     {
 189  0
         return mIgnoreCase;
 190  
     }
 191  
 
 192  
     /**
 193  
      * The pattern to use when matching.
 194  
      * @return the pattern to use when matching.
 195  
      */
 196  
     public Pattern getPattern()
 197  
     {
 198  2480
         final int options = (mIgnoreCase) ? mCompileFlags
 199  
                 | Pattern.CASE_INSENSITIVE : mCompileFlags;
 200  2480
         return Utils.getPattern(mFormat, options);
 201  
     }
 202  
 }