Merge pull request #8002 from Christoph Dreis

* gh-8002:
  Improve performance of RelaxedNames
This commit is contained in:
Andy Wilkinson 2017-01-18 14:16:11 +00:00
commit 0ae297299b
1 changed files with 22 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -91,7 +91,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.toLowerCase();
return value.isEmpty() ? value : value.toLowerCase();
}
},
@ -100,7 +100,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.toUpperCase();
return value.isEmpty() ? value : value.toUpperCase();
}
};
@ -127,7 +127,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.replace("-", "_");
return value.indexOf('-') != -1 ? value.replace("-", "_") : value;
}
},
@ -136,7 +136,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.replace("_", ".");
return value.indexOf('_') != -1 ? value.replace("_", ".") : value;
}
},
@ -145,7 +145,7 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
return value.replace(".", "_");
return value.indexOf('.') != -1 ? value.replace(".", "_") : value;
}
},
@ -154,7 +154,14 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
if (value.isEmpty()) {
return value;
}
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
if (!matcher.find()) {
return value;
}
matcher = matcher.reset();
StringBuffer result = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(result, matcher.group(1) + '_'
@ -170,7 +177,14 @@ public final class RelaxedNames implements Iterable<String> {
@Override
public String apply(String value) {
if (value.isEmpty()) {
return value;
}
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
if (!matcher.find()) {
return value;
}
matcher = matcher.reset();
StringBuffer result = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(result, matcher.group(1) + '-'
@ -206,7 +220,7 @@ public final class RelaxedNames implements Iterable<String> {
private static String separatedToCamelCase(String value,
boolean caseInsensitive) {
if (value.length() == 0) {
if (value.isEmpty()) {
return value;
}
StringBuilder builder = new StringBuilder();
@ -233,7 +247,7 @@ public final class RelaxedNames implements Iterable<String> {
* @return the relaxed names
*/
public static RelaxedNames forCamelCase(String name) {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
for (char c : name.toCharArray()) {
result.append(Character.isUpperCase(c) && result.length() > 0
&& result.charAt(result.length() - 1) != '-'