ParseField#getAllNamesIncludedDeprecated to not return duplicate names

This commit is contained in:
Luca Cavanna 2016-04-04 15:08:43 +02:00
parent 7d4ed5b19e
commit f75086fcee
2 changed files with 17 additions and 6 deletions

View File

@ -21,7 +21,9 @@ package org.elasticsearch.common;
import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
/** /**
* Holds a field that can be found in a request while parsing and its different variants, which may be deprecated. * Holds a field that can be found in a request while parsing and its different variants, which may be deprecated.
@ -34,6 +36,7 @@ public class ParseField {
private final String underscoreName; private final String underscoreName;
private final String[] deprecatedNames; private final String[] deprecatedNames;
private String allReplacedWith = null; private String allReplacedWith = null;
private final String[] allNames;
public ParseField(String value, String... deprecatedNames) { public ParseField(String value, String... deprecatedNames) {
camelCaseName = Strings.toCamelCase(value); camelCaseName = Strings.toCamelCase(value);
@ -48,6 +51,11 @@ public class ParseField {
} }
this.deprecatedNames = set.toArray(new String[set.size()]); this.deprecatedNames = set.toArray(new String[set.size()]);
} }
Set<String> allNames = new HashSet<>();
allNames.add(camelCaseName);
allNames.add(underscoreName);
Collections.addAll(allNames, this.deprecatedNames);
this.allNames = allNames.toArray(new String[allNames.size()]);
} }
public String getPreferredName(){ public String getPreferredName(){
@ -55,12 +63,6 @@ public class ParseField {
} }
public String[] getAllNamesIncludedDeprecated() { public String[] getAllNamesIncludedDeprecated() {
String[] allNames = new String[2 + deprecatedNames.length];
allNames[0] = camelCaseName;
allNames[1] = underscoreName;
for (int i = 0; i < deprecatedNames.length; i++) {
allNames[i + 2] = deprecatedNames[i];
}
return allNames; return allNames;
} }

View File

@ -24,6 +24,7 @@ import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
public class ParseFieldTests extends ESTestCase { public class ParseFieldTests extends ESTestCase {
public void testParse() { public void testParse() {
@ -87,4 +88,12 @@ public class ParseFieldTests extends ESTestCase {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> field.match(randomFrom(allValues), true)); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> field.match(randomFrom(allValues), true));
assertThat(e.getMessage(), containsString(" used, replaced by [like]")); assertThat(e.getMessage(), containsString(" used, replaced by [like]"));
} }
public void testGetAllNamesIncludedDeprecated() {
ParseField parseField = new ParseField("terms", "in");
assertThat(parseField.getAllNamesIncludedDeprecated(), arrayContainingInAnyOrder("terms", "in"));
parseField = new ParseField("more_like_this", "mlt");
assertThat(parseField.getAllNamesIncludedDeprecated(), arrayContainingInAnyOrder("more_like_this", "moreLikeThis", "mlt"));
}
} }