Skip to content

Commit bbb5742

Browse files
committed
Merge pull request #21523 from dreis2211
* pr/21523: Polish 'Optimize SystemEnvironmentPropertyMapper' Optimize SystemEnvironmentPropertyMapper Call append only when necessary Use chars rather than strings Closes gh-21523
2 parents de8970e + 47c1928 commit bbb5742

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,18 @@ public static ConfigurationPropertyName of(CharSequence name) {
510510
return of(name, false);
511511
}
512512

513+
/**
514+
* Return a {@link ConfigurationPropertyName} for the specified string or {@code null}
515+
* if the name is not valid.
516+
* @param name the source name
517+
* @return a {@link ConfigurationPropertyName} instance
518+
* @throws InvalidConfigurationPropertyNameException if the name is not valid
519+
* @since 2.3.1
520+
*/
521+
public static ConfigurationPropertyName ofIfValid(CharSequence name) {
522+
return of(name, true);
523+
}
524+
513525
/**
514526
* Return a {@link ConfigurationPropertyName} for the specified string.
515527
* @param name the source name

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private String convertName(ConfigurationPropertyName name, int numberOfElements)
5858
StringBuilder result = new StringBuilder();
5959
for (int i = 0; i < numberOfElements; i++) {
6060
if (result.length() > 0) {
61-
result.append("_");
61+
result.append('_');
6262
}
6363
result.append(name.getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH));
6464
}
@@ -69,7 +69,7 @@ private String convertLegacyName(ConfigurationPropertyName name) {
6969
StringBuilder result = new StringBuilder();
7070
for (int i = 0; i < name.getNumberOfElements(); i++) {
7171
if (result.length() > 0) {
72-
result.append("_");
72+
result.append('_');
7373
}
7474
result.append(convertLegacyNameElement(name.getElement(i, Form.ORIGINAL)));
7575
}
@@ -116,13 +116,19 @@ private boolean isLegacyAncestorOf(ConfigurationPropertyName name, Configuration
116116
if (!hasDashedEntries(name)) {
117117
return false;
118118
}
119+
ConfigurationPropertyName legacyCompatibleName = buildLegacyCompatibleName(name);
120+
return legacyCompatibleName != null && legacyCompatibleName.isAncestorOf(candidate);
121+
}
122+
123+
private ConfigurationPropertyName buildLegacyCompatibleName(ConfigurationPropertyName name) {
119124
StringBuilder legacyCompatibleName = new StringBuilder();
120125
for (int i = 0; i < name.getNumberOfElements(); i++) {
121-
legacyCompatibleName.append((i != 0) ? "." : "");
126+
if (i != 0) {
127+
legacyCompatibleName.append('.');
128+
}
122129
legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.'));
123130
}
124-
return ConfigurationPropertyName.isValid(legacyCompatibleName)
125-
&& ConfigurationPropertyName.of(legacyCompatibleName).isAncestorOf(candidate);
131+
return ConfigurationPropertyName.ofIfValid(legacyCompatibleName);
126132
}
127133

128134
boolean hasDashedEntries(ConfigurationPropertyName name) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ void ofWhenNameIsEmptyShouldReturnEmptyName() {
210210
assertThat(name.append("foo").toString()).isEqualTo("foo");
211211
}
212212

213+
@Test
214+
void ofIfValidWhenNameIsValidReturnsName() {
215+
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo-ot");
216+
assertThat(name).hasToString("spring.bo-ot");
217+
}
218+
219+
@Test
220+
void ofIfValidWhenNameIsNotValidReturnsNull() {
221+
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo!oot");
222+
assertThat(name).isNull();
223+
}
224+
213225
@Test
214226
void adaptWhenNameIsNullShouldThrowException() {
215227
assertThatIllegalArgumentException().isThrownBy(() -> ConfigurationPropertyName.adapt(null, '.'))

0 commit comments

Comments
 (0)