Don't repeat relaxed names that already showed up

pull/85/head
Dave Syer 11 years ago
parent 97f93bfa64
commit b8bdf22209

@ -17,7 +17,8 @@
package org.springframework.boot.bind; package org.springframework.boot.bind;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -33,6 +34,8 @@ public final class RelaxedNames implements Iterable<String> {
private final String name; private final String name;
private Set<String> values = new LinkedHashSet<String>();
/** /**
* Create a new {@link RelaxedNames} instance. * Create a new {@link RelaxedNames} instance.
* *
@ -41,45 +44,27 @@ public final class RelaxedNames implements Iterable<String> {
*/ */
public RelaxedNames(String name) { public RelaxedNames(String name) {
this.name = name; this.name = name;
initialize(RelaxedNames.this.name, this.values);
} }
@Override @Override
public Iterator<String> iterator() { public Iterator<String> iterator() {
return new RelaxedNamesIterator(); return this.values.iterator();
} }
private class RelaxedNamesIterator implements Iterator<String> { private void initialize(String name, Set<String> values) {
if (values.contains(name)) {
private int variation = 0; return;
private int manipulation = 0;
@Override
public boolean hasNext() {
return (this.variation < Variation.values().length);
} }
for (Variation variation : Variation.values()) {
@Override for (Manipulation manipulation : Manipulation.values()) {
public String next() { String result = name;
if (!hasNext()) { result = manipulation.apply(result);
throw new NoSuchElementException(); result = variation.apply(result);
} values.add(result);
String result = RelaxedNames.this.name; initialize(result, values);
result = Manipulation.values()[this.manipulation].apply(result);
result = Variation.values()[this.variation].apply(result);
this.manipulation++;
if (this.manipulation >= Manipulation.values().length) {
this.variation++;
this.manipulation = 0;
} }
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
} }
} }
static enum Variation { static enum Variation {
@ -118,6 +103,22 @@ public final class RelaxedNames implements Iterable<String> {
return value.replace("-", "_"); return value.replace("-", "_");
} }
}, },
UNCAMELCASE {
@Override
public String apply(String value) {
value = value.replaceAll("([^A-Z-])([A-Z])", "$1_$2");
StringBuilder builder = new StringBuilder();
for (String field : value.split("_")) {
if (builder.length() == 0) {
builder.append(field);
}
else {
builder.append("_").append(StringUtils.uncapitalize(field));
}
}
return builder.toString();
}
},
CAMELCASE { CAMELCASE {
@Override @Override
public String apply(String value) { public String apply(String value) {

@ -27,6 +27,7 @@ import static org.junit.Assert.assertThat;
* Tests for {@link RelaxedNames}. * Tests for {@link RelaxedNames}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer
*/ */
public class RelaxedNamesTests { public class RelaxedNamesTests {
@ -43,18 +44,36 @@ public class RelaxedNamesTests {
assertThat(iterator.next(), equalTo("MY_RELAXED_PROPERTY")); assertThat(iterator.next(), equalTo("MY_RELAXED_PROPERTY"));
assertThat(iterator.next(), equalTo("MYRELAXEDPROPERTY")); assertThat(iterator.next(), equalTo("MYRELAXEDPROPERTY"));
assertThat(iterator.hasNext(), equalTo(false)); assertThat(iterator.hasNext(), equalTo(false));
}
iterator = new RelaxedNames("nes_ted").iterator(); @Test
assertThat(iterator.next(), equalTo("nes_ted")); public void fromUnderscores() throws Exception {
Iterator<String> iterator = new RelaxedNames("nes_ted").iterator();
assertThat(iterator.next(), equalTo("nes_ted")); assertThat(iterator.next(), equalTo("nes_ted"));
assertThat(iterator.next(), equalTo("nesTed")); assertThat(iterator.next(), equalTo("nesTed"));
assertThat(iterator.next(), equalTo("nes_ted"));
assertThat(iterator.next(), equalTo("nes_ted"));
assertThat(iterator.next(), equalTo("nested")); assertThat(iterator.next(), equalTo("nested"));
assertThat(iterator.next(), equalTo("NES_TED")); assertThat(iterator.next(), equalTo("NES_TED"));
assertThat(iterator.next(), equalTo("NES_TED"));
assertThat(iterator.next(), equalTo("NESTED")); assertThat(iterator.next(), equalTo("NESTED"));
assertThat(iterator.hasNext(), equalTo(false)); assertThat(iterator.hasNext(), equalTo(false));
} }
@Test
public void fromPlain() throws Exception {
Iterator<String> iterator = new RelaxedNames("plain").iterator();
assertThat(iterator.next(), equalTo("plain"));
assertThat(iterator.next(), equalTo("PLAIN"));
assertThat(iterator.hasNext(), equalTo(false));
}
@Test
public void fromCamelCase() throws Exception {
Iterator<String> iterator = new RelaxedNames("caMel").iterator();
assertThat(iterator.next(), equalTo("caMel"));
assertThat(iterator.next(), equalTo("ca_mel"));
assertThat(iterator.next(), equalTo("camel"));
assertThat(iterator.next(), equalTo("CAMEL"));
assertThat(iterator.next(), equalTo("CA_MEL"));
assertThat(iterator.hasNext(), equalTo(false));
}
} }

Loading…
Cancel
Save