Refactor this code to use a "static final" Pattern.
You hit the following issue: Regex patterns should not be created needlessly.
The problem is caused by these operations:
.replaceAll(StringUtils.SPACE, StringUtils.EMPTY)
.replaceFirst(StringUtils.LF, StringUtils.EMPTY)
Both methods create new instances of the java.util.regex.Pattern
class. It means that when your method is executed 100 times, then your code creates 300 Pattern
objects. The rule informs that you shouldn't do this, because Pattern
are heavy objects and you can use more efficient code.
If StringUtils
is org.apache.commons.lang3.StringUtils
then the solution is as follow.
In the first operation
.replaceAll(StringUtils.SPACE, StringUtils.EMPTY)
you want to replace all occurrences of " "
(space) by ""
(empty). This code should be changed to:
.replace(StringUtils.SPACE, StringUtils.EMPTY)
The replace
method replaces literals as is. replaceAll
treats the first parameter as regular expressions. There is no sense to parse a space character as a regular expression, because the output will be exactly the same.
Next in
.replaceFirst(StringUtils.LF, StringUtils.EMPTY)
you always create a new Pattern
object from StringUtils.LF
. Instead of creating a new pattern every time, you should compile it once and next reuse it:
private static final Pattern LF_PATTERN = Pattern.compile(StringUtils.LF, Pattern.LITERAL);
LF_PATTERN.matcher(string).replaceFirst(replacement);
The final code should be as follow:
private static final Pattern LF_PATTERN = Pattern.compile(StringUtils.LF, Pattern.LITERAL);
public Boolean validateLabelText(String labelValue, String fieldCell) {
String value1 = labelValue.replace(StringUtils.SPACE, StringUtils.EMPTY);
String value2 = fieldCell.replace(StringUtils.SPACE, StringUtils.EMPTY);
value2 = LF_PATTERN.matcher(value2).replaceFirst(StringUtils.EMPTY);
return value1.equalsIgnoreCase(value2);
}
I don't know what is the business logic of your code, so I named variables as value1
and value2
(I don't know which value is current
and which expected
).