Size the ElementsParser based on expected number of elements

Previously, the ElementsParser would be created using its default
capacity of 6 even when parsing a String that is expected to
produce a single element.

This commit updates ConfigurationPropertyName to create an
ElementsParser with a capacity of 1 when parsing a String that should
contain only a single element.

See gh-15760
This commit is contained in:
Andy Wilkinson 2019-01-23 14:41:39 +00:00
parent 8ec6c372e4
commit 39e2aaa41c
1 changed files with 9 additions and 4 deletions

View File

@ -195,7 +195,7 @@ public final class ConfigurationPropertyName
if (elementValue == null) {
return this;
}
Elements additionalElements = elementsOf(elementValue);
Elements additionalElements = probablySingleElementOf(elementValue);
return new ConfigurationPropertyName(this.elements.append(additionalElements));
}
@ -424,11 +424,16 @@ public final class ConfigurationPropertyName
return (elements != null) ? new ConfigurationPropertyName(elements) : null;
}
private static Elements elementsOf(CharSequence name) {
return elementsOf(name, false);
private static Elements probablySingleElementOf(CharSequence name) {
return elementsOf(name, false, 1);
}
private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid) {
return elementsOf(name, returnNullIfInvalid, ElementsParser.DEFAULT_CAPACITY);
}
private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid,
int parserCapacity) {
if (name == null) {
Assert.isTrue(returnNullIfInvalid, "Name must not be null");
return null;
@ -443,7 +448,7 @@ public final class ConfigurationPropertyName
throw new InvalidConfigurationPropertyNameException(name,
Collections.singletonList('.'));
}
Elements elements = new ElementsParser(name, '.').parse();
Elements elements = new ElementsParser(name, '.', parserCapacity).parse();
for (int i = 0; i < elements.getSize(); i++) {
if (elements.getType(i) == ElementType.NON_UNIFORM) {
if (returnNullIfInvalid) {