Using Regular expression objects for splitting and replacing in strings. (#1059)

* Where possible, replace .split() and .replace() methods with compiled Regex patterns.
This prevents a regular expression from being compiled multiple times during runtime.

* Forgot making one instance `private static final`.
This commit is contained in:
Koen De Groote authored and Rob Bygrave committed 2017-07-06 20:14:51 +12:00
1 parent 08263c4588
commit 36a5610e25
12 files changed
+68 -24

No files matched your search

@@ -1,5 +1,6 @@
package io.ebeaninternal.extraddl.model;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,6 +16,8 @@ public class ExtraDdlXmlReader {
private static final Logger logger = LoggerFactory.getLogger(ExtraDdlXmlReader.class);
private static final Pattern PLATFORM_REGEX_SPLIT = Pattern.compile("[,;]");
/**
* Return the combined extra DDL that should be run given the platform name.
*/
@@ -44,7 +47,8 @@ public class ExtraDdlXmlReader {
if (platforms == null || platforms.trim().isEmpty()) {
return true;
}
String[] names = platforms.split("[,;]");
String[] names = PLATFORM_REGEX_SPLIT.split(platforms);
for (String name : names) {
if (name.trim().toLowerCase().contains(platformName)) {
return true;
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.core.bootup;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -21,6 +22,8 @@ class ManifestReader {
private final Set<String> packageSet = new HashSet<>();
private static final Pattern PACKAGE_DELIMITER = Pattern.compile("[,; ]");
/**
* Read the packages from ebean.mf manifest files found as resources.
*/
@@ -73,7 +76,7 @@ class ManifestReader {
* Collect each individual package splitting by delimiters.
*/
private void add(String packages) {
String[] split = packages.split("[,; ]");
String[] split = PACKAGE_DELIMITER.split(packages);
for (String aSplit : split) {
String pkg = aSplit.trim();
if (!pkg.isEmpty()) {
@@ -3,6 +3,8 @@ package io.ebeaninternal.server.query;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.el.ElPropertyDeploy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.PersistenceException;
import java.util.HashMap;
import java.util.HashSet;
@@ -15,6 +17,8 @@ import java.util.TreeSet;
*/
class SqlTreeAlias {
private static final Pattern TABLE_ALIAS_REPLACE = Pattern.compile("${}", Pattern.LITERAL);
private int counter;
private int manyWhereCounter;
@@ -178,9 +182,9 @@ class SqlTreeAlias {
private String parseRootAlias(String clause) {
if (rootTableAlias == null) {
return clause.replace("${}", "");
return TABLE_ALIAS_REPLACE.matcher(clause).replaceAll("");
} else {
return clause.replace("${}", rootTableAlias + ".");
return TABLE_ALIAS_REPLACE.matcher(clause).replaceAll(Matcher.quoteReplacement(rootTableAlias + "."));
}
}
@@ -9,6 +9,7 @@ import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Set properties for a UpdateQuery.
@@ -19,6 +20,8 @@ public class OrmUpdateProperties {
private static final NoneValue NONE_VALUE = new NoneValue();
private static final Pattern TABLE_ALIAS_REPLACE = Pattern.compile("${}", Pattern.LITERAL);
/**
* Bind value used in the set clause for update query.
* It may/may not have bind values etc.
@@ -188,8 +191,9 @@ public class OrmUpdateProperties {
if (setCount++ > 0) {
sb.append(", ");
}
// translate to db columns and remove table alias placeholders
sb.append(deployParser.parse(property).replace("${}", ""));
sb.append(TABLE_ALIAS_REPLACE.matcher(deployParser.parse(property)).replaceAll(""));
sb.append(entry.getValue().bindClause());
}
return sb.toString();
@@ -21,6 +21,8 @@ import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Rob Bygrave: This is a copy of the google guava InetAddresses class
@@ -115,6 +117,10 @@ public final class ConvertInetAddresses {
private static final int IPV4_PART_COUNT = 4;
private static final int IPV6_PART_COUNT = 8;
public static final String DOUBLE_COLON = "::";
private static final Pattern IPSTRING_REPLACE = Pattern.compile(DOUBLE_COLON, Pattern.LITERAL);
private ConvertInetAddresses() {
}
@@ -186,7 +192,7 @@ public final class ConvertInetAddresses {
// handle IPv6 forms of IPv4 addresses
if (ipString.toUpperCase(Locale.US).startsWith("::FFFF:")) {
ipString = ipString.substring(7);
} else if (ipString.startsWith("::")) {
} else if (ipString.startsWith(DOUBLE_COLON)) {
ipString = ipString.substring(2);
isIpv6 = true;
}
@@ -260,13 +266,14 @@ public final class ConvertInetAddresses {
// Fill in any omitted colons
private static String padIpString(String ipString) {
if (ipString.contains("::")) {
if (ipString.contains(DOUBLE_COLON)) {
int count = numberOfColons(ipString);
StringBuilder buffer = new StringBuilder("::");
StringBuilder buffer = new StringBuilder(DOUBLE_COLON);
for (int i = 0; i + count < 7; i++) {
buffer.append(":");
}
ipString = ipString.replace("::", buffer);
ipString = IPSTRING_REPLACE.matcher(ipString).replaceAll(Matcher.quoteReplacement(buffer.toString()));
}
return ipString;
}