Skip to content

Commit

Permalink
Polish 'Optimize SystemEnvironmentPropertyMapper'
Browse files Browse the repository at this point in the history
Introduce a new `ConfigurationPropertyName.ofIfValid` method to
save us needing to throw and catch an exception unnecessarily.

See gh-21523
  • Loading branch information
philwebb committed Jun 5, 2020
1 parent 0378de7 commit 47c1928
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@ public static ConfigurationPropertyName of(CharSequence name) {
return of(name, false);
}

/**
* Return a {@link ConfigurationPropertyName} for the specified string or {@code null}
* if the name is not valid.
* @param name the source name
* @return a {@link ConfigurationPropertyName} instance
* @throws InvalidConfigurationPropertyNameException if the name is not valid
* @since 2.3.1
*/
public static ConfigurationPropertyName ofIfValid(CharSequence name) {
return of(name, true);
}

/**
* Return a {@link ConfigurationPropertyName} for the specified string.
* @param name the source name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,19 @@ private boolean isLegacyAncestorOf(ConfigurationPropertyName name, Configuration
if (!hasDashedEntries(name)) {
return false;
}
StringBuilder legacyCompatibleName = buildLegacyCompatibleName(name);
try {
return ConfigurationPropertyName.of(legacyCompatibleName).isAncestorOf(candidate);
}
catch (Exception ex) {
return false;
}
ConfigurationPropertyName legacyCompatibleName = buildLegacyCompatibleName(name);
return legacyCompatibleName != null && legacyCompatibleName.isAncestorOf(candidate);
}

private StringBuilder buildLegacyCompatibleName(ConfigurationPropertyName name) {
private ConfigurationPropertyName buildLegacyCompatibleName(ConfigurationPropertyName name) {
StringBuilder legacyCompatibleName = new StringBuilder();
for (int i = 0; i < name.getNumberOfElements(); i++) {
if (i != 0) {
legacyCompatibleName.append('.');
}
legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.'));
}
return legacyCompatibleName;
return ConfigurationPropertyName.ofIfValid(legacyCompatibleName);
}

boolean hasDashedEntries(ConfigurationPropertyName name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ void ofWhenNameIsEmptyShouldReturnEmptyName() {
assertThat(name.append("foo").toString()).isEqualTo("foo");
}

@Test
void ofIfValidWhenNameIsValidReturnsName() {
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo-ot");
assertThat(name).hasToString("spring.bo-ot");
}

@Test
void ofIfValidWhenNameIsNotValidReturnsNull() {
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo!oot");
assertThat(name).isNull();
}

@Test
void adaptWhenNameIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> ConfigurationPropertyName.adapt(null, '.'))
Expand Down

0 comments on commit 47c1928

Please sign in to comment.