001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.regexp; 021 022import java.util.regex.Pattern; 023 024import com.google.common.base.MoreObjects; 025 026import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter; 027 028/** 029 * Options for a detector. 030 * @author Oliver Burn 031 */ 032public final class DetectorOptions { 033 /** 034 * Flags to compile a regular expression with. 035 * See {@link Pattern#flags()}. 036 */ 037 private int compileFlags; 038 /** Used for reporting violations. */ 039 private AbstractViolationReporter reporter; 040 /** 041 * Format of the regular expression to check for. 042 */ 043 private String format; 044 /** The message to report on detection. If blank, then use the format. */ 045 private String message = ""; 046 /** Minimum number of times regular expression should occur in a file. */ 047 private int minimum; 048 /** Maximum number of times regular expression should occur in a file. */ 049 private int maximum; 050 /** Whether to ignore case when matching. */ 051 private boolean ignoreCase; 052 /** Used to determine whether to suppress a detected match. */ 053 private MatchSuppressor suppressor; 054 /** Pattern created from format. Lazily initialized. */ 055 private Pattern pattern; 056 057 /** Default constructor.*/ 058 private DetectorOptions() { } 059 060 /** 061 * Returns new Builder object. 062 * @return Builder object. 063 * @noinspection ReturnOfInnerClass 064 */ 065 public static Builder newBuilder() { 066 return new DetectorOptions().new Builder(); 067 } 068 069 /** 070 * Format of the regular expression. 071 * @return format of the regular expression. 072 */ 073 public String getFormat() { 074 return format; 075 } 076 077 /** 078 * The violation reporter to use. 079 * @return the violation reporter to use. 080 */ 081 public AbstractViolationReporter getReporter() { 082 return reporter; 083 } 084 085 /** 086 * The message to report errors with. 087 * @return the message to report errors with. 088 */ 089 public String getMessage() { 090 return message; 091 } 092 093 /** 094 * The minimum number of allowed detections. 095 * @return the minimum number of allowed detections. 096 */ 097 public int getMinimum() { 098 return minimum; 099 } 100 101 /** 102 * The maximum number of allowed detections. 103 * @return the maximum number of allowed detections. 104 */ 105 public int getMaximum() { 106 return maximum; 107 } 108 109 /** 110 * The suppressor to use. 111 * @return the suppressor to use. 112 */ 113 public MatchSuppressor getSuppressor() { 114 return suppressor; 115 } 116 117 /** 118 * The pattern to use when matching. 119 * @return the pattern to use when matching. 120 */ 121 public Pattern getPattern() { 122 if (pattern != null) { 123 return pattern; 124 } 125 int options = compileFlags; 126 127 if (ignoreCase) { 128 options |= Pattern.CASE_INSENSITIVE; 129 } 130 pattern = Pattern.compile(format, options); 131 return pattern; 132 } 133 134 /** Class which implements Builder pattern to build DetectorOptions instance. */ 135 public final class Builder { 136 137 /** 138 * Specifies the violation reporter and returns Builder object. 139 * @param val for reporting violations. 140 * @return Builder object. 141 * @noinspection ReturnOfInnerClass 142 */ 143 public Builder reporter(AbstractViolationReporter val) { 144 reporter = val; 145 return this; 146 } 147 148 /** 149 * Specifies the compile flags to compile a regular expression with 150 * and returns Builder object. 151 * @param val the format to use when matching lines. 152 * @return Builder object. 153 * @noinspection ReturnOfInnerClass 154 */ 155 public Builder compileFlags(int val) { 156 compileFlags = val; 157 return this; 158 } 159 160 /** 161 * Specifies the format to use when matching lines and returns Builder object. 162 * @param val the format to use when matching lines. 163 * @return Builder object. 164 * @noinspection ReturnOfInnerClass 165 */ 166 public Builder format(String val) { 167 format = val; 168 return this; 169 } 170 171 /** 172 * Specifies message to use when reporting a match and returns Builder object. 173 * @param val message to use when reporting a match. 174 * @return Builder object. 175 * @noinspection ReturnOfInnerClass 176 */ 177 public Builder message(String val) { 178 message = val; 179 return this; 180 } 181 182 /** 183 * Specifies the minimum allowed number of detections and returns Builder object. 184 * @param val the minimum allowed number of detections. 185 * @return Builder object. 186 * @noinspection ReturnOfInnerClass 187 */ 188 public Builder minimum(int val) { 189 minimum = val; 190 return this; 191 } 192 193 /** 194 * Specifies the maximum allowed number of detections and returns Builder object. 195 * @param val the maximum allowed number of detections. 196 * @return Builder object. 197 * @noinspection ReturnOfInnerClass 198 */ 199 public Builder maximum(int val) { 200 maximum = val; 201 return this; 202 } 203 204 /** 205 * Specifies whether to ignore case when matching and returns Builder object. 206 * @param val whether to ignore case when matching. 207 * @return Builder object. 208 * @noinspection ReturnOfInnerClass 209 */ 210 public Builder ignoreCase(boolean val) { 211 ignoreCase = val; 212 return this; 213 } 214 215 /** 216 * Specifies the suppressor to use and returns Builder object. 217 * @param val the suppressor to use. 218 * @return current instance 219 * @noinspection ReturnOfInnerClass 220 */ 221 public Builder suppressor(MatchSuppressor val) { 222 suppressor = val; 223 return this; 224 } 225 226 /** 227 * Returns new DetectorOptions instance. 228 * @return DetectorOptions instance. 229 */ 230 public DetectorOptions build() { 231 message = MoreObjects.firstNonNull(message, ""); 232 suppressor = MoreObjects.firstNonNull(suppressor, NeverSuppress.INSTANCE); 233 return DetectorOptions.this; 234 } 235 } 236}