parent
c906524df6
commit
b948e61465
|
@ -19,6 +19,8 @@ package org.springframework.boot.bind;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
@ -32,6 +34,8 @@ import org.springframework.util.StringUtils;
|
||||||
*/
|
*/
|
||||||
public final class RelaxedNames implements Iterable<String> {
|
public final class RelaxedNames implements Iterable<String> {
|
||||||
|
|
||||||
|
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])");
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private final Set<String> values = new LinkedHashSet<String>();
|
private final Set<String> values = new LinkedHashSet<String>();
|
||||||
|
@ -126,17 +130,28 @@ public final class RelaxedNames implements Iterable<String> {
|
||||||
CAMELCASE_TO_UNDERSCORE {
|
CAMELCASE_TO_UNDERSCORE {
|
||||||
@Override
|
@Override
|
||||||
public String apply(String value) {
|
public String apply(String value) {
|
||||||
value = value.replaceAll("([^A-Z-])([A-Z])", "$1_$2");
|
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuffer result = new StringBuffer();
|
||||||
for (String field : value.split("_")) {
|
while (matcher.find()) {
|
||||||
if (builder.length() == 0) {
|
matcher.appendReplacement(result, matcher.group(1) + '_'
|
||||||
builder.append(field);
|
+ StringUtils.uncapitalize(matcher.group(2)));
|
||||||
}
|
}
|
||||||
else {
|
matcher.appendTail(result);
|
||||||
builder.append("_").append(StringUtils.uncapitalize(field));
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
CAMELCASE_TO_HYPHEN {
|
||||||
|
@Override
|
||||||
|
public String apply(String value) {
|
||||||
|
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
|
||||||
|
StringBuffer result = new StringBuffer();
|
||||||
|
while (matcher.find()) {
|
||||||
|
matcher.appendReplacement(result, matcher.group(1) + '-'
|
||||||
|
+ StringUtils.uncapitalize(matcher.group(2)));
|
||||||
}
|
}
|
||||||
return builder.toString();
|
matcher.appendTail(result);
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -72,9 +72,24 @@ public class RelaxedNamesTests {
|
||||||
Iterator<String> iterator = new RelaxedNames("caMel").iterator();
|
Iterator<String> iterator = new RelaxedNames("caMel").iterator();
|
||||||
assertThat(iterator.next(), equalTo("caMel"));
|
assertThat(iterator.next(), equalTo("caMel"));
|
||||||
assertThat(iterator.next(), equalTo("ca_mel"));
|
assertThat(iterator.next(), equalTo("ca_mel"));
|
||||||
|
assertThat(iterator.next(), equalTo("ca-mel"));
|
||||||
assertThat(iterator.next(), equalTo("camel"));
|
assertThat(iterator.next(), equalTo("camel"));
|
||||||
assertThat(iterator.next(), equalTo("CAMEL"));
|
assertThat(iterator.next(), equalTo("CAMEL"));
|
||||||
assertThat(iterator.next(), equalTo("CA_MEL"));
|
assertThat(iterator.next(), equalTo("CA_MEL"));
|
||||||
|
assertThat(iterator.next(), equalTo("CA-MEL"));
|
||||||
|
assertThat(iterator.hasNext(), equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromCompoundCamelCase() throws Exception {
|
||||||
|
Iterator<String> iterator = new RelaxedNames("caMelCase").iterator();
|
||||||
|
assertThat(iterator.next(), equalTo("caMelCase"));
|
||||||
|
assertThat(iterator.next(), equalTo("ca_mel_case"));
|
||||||
|
assertThat(iterator.next(), equalTo("ca-mel-case"));
|
||||||
|
assertThat(iterator.next(), equalTo("camelcase"));
|
||||||
|
assertThat(iterator.next(), equalTo("CAMELCASE"));
|
||||||
|
assertThat(iterator.next(), equalTo("CA_MEL_CASE"));
|
||||||
|
assertThat(iterator.next(), equalTo("CA-MEL-CASE"));
|
||||||
assertThat(iterator.hasNext(), equalTo(false));
|
assertThat(iterator.hasNext(), equalTo(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue