From 8f3da64f6dda463b0ca1a5e69b88c172ebe9ab0c Mon Sep 17 00:00:00 2001 From: Fabian Loewner Date: Sun, 1 Nov 2015 00:38:18 +0100 Subject: [PATCH] Pull #2495: Performance-Optimization: Do not recompile Pattern for each file/line --- .../checkstyle/checks/regexp/DetectorOptions.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/DetectorOptions.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/DetectorOptions.java index 8d03338b4..33d56f93f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/DetectorOptions.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/DetectorOptions.java @@ -50,6 +50,8 @@ class DetectorOptions { private boolean ignoreCase; /** Used to determine whether to suppress a detected match. */ private MatchSuppressor suppressor = NeverSuppress.INSTANCE; + /** Pattern created from format. Lazily initialized. */ + private Pattern pattern; /** * Creates an instance. @@ -69,6 +71,7 @@ class DetectorOptions { */ public DetectorOptions setFormat(String format) { this.format = format; + pattern = null; return this; } @@ -119,6 +122,7 @@ class DetectorOptions { */ public DetectorOptions setIgnoreCase(boolean ignore) { ignoreCase = ignore; + pattern = null; return this; } @@ -175,11 +179,15 @@ class DetectorOptions { * @return the pattern to use when matching. */ public Pattern getPattern() { + if (pattern != null) { + return pattern; + } int options = compileFlags; if (ignoreCase) { options |= Pattern.CASE_INSENSITIVE; } - return Pattern.compile(format, options); + pattern = Pattern.compile(format, options); + return pattern; } }