From the Mailing list:
I was profiling some of our code which uses HL7, and I noticed that we had a large number of calls to java.util.Pattern#compile.
I tracked a number of them down to SegmentFinder#matches method. I would therefore suggest the patch below, which cuts down the number of Pattern#compile calls from 4 to 1.
The unit tests passes. I have written the code myself and would like to submit it under the same GPL/MPL license as the rest of the code.
Regards,
Niels Harremoës
Index: SegmentFinder.java
===================================================================
--- SegmentFinder.java (revision 983)
+++ SegmentFinder.java (working copy)
@@ -157,8 +157,13 @@
}
return matches;
}*/
-
+
/**
+ * Patterns used for matching can only contain these characters
+ */
+ private static final Pattern VALID_PATTERN_PATTERN = Pattern.compile("[\\w\\*\\?]*");
+
+ /**
* Tests whether the given name matches the given pattern.
*/
private boolean matches(String pattern, String candidate) {
@@ -166,12 +171,12 @@
if (pattern.equals(candidate)) {
return true;
}
-
- if (!Pattern.matches("[\\w\\*\\?]*", pattern))
+
+ if (!VALID_PATTERN_PATTERN.matcher(pattern).matches())
throw new IllegalArgumentException("The pattern " + pattern + " is not valid. Only [\\w\\*\\?]* allowed.");
- pattern = Pattern.compile("\\*").matcher(pattern).replaceAll(".*");
- pattern = Pattern.compile("\\?").matcher(pattern).replaceAll(".");
+ pattern = pattern.replace("*",".*");
+ pattern = pattern.replace('?','.');
return Pattern.matches(pattern, candidate);
}