Skip to content

Commit d938dd9

Browse files
committed
Add with/without methods to ConfigData.Options
Add convenience methods to ConfigData.Options to allow new Options instances to be created with options excluded or included. See gh-25766
1 parent 86303c0 commit d938dd9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.EnumSet;
2424
import java.util.List;
2525
import java.util.Set;
26+
import java.util.function.Consumer;
2627

2728
import org.springframework.core.env.Environment;
2829
import org.springframework.core.env.PropertySource;
@@ -224,10 +225,24 @@ public String toString() {
224225
* @param option the option to exclude
225226
* @return a new {@link Options} instance
226227
*/
227-
Options without(Option option) {
228+
public Options without(Option option) {
229+
return copy((options) -> options.remove(option));
230+
}
231+
232+
/**
233+
* Create a new {@link Options} instance that contains the options in this set
234+
* including the given option.
235+
* @param option the option to include
236+
* @return a new {@link Options} instance
237+
*/
238+
public Options with(Option option) {
239+
return copy((options) -> options.add(option));
240+
}
241+
242+
private Options copy(Consumer<EnumSet<Option>> processor) {
228243
EnumSet<Option> options = EnumSet.noneOf(Option.class);
229244
options.addAll(this.options);
230-
options.remove(option);
245+
processor.accept(options);
231246
return new Options(options);
232247
}
233248

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ void optionsNoneReturnsEmptyOptions() {
129129
assertThat(Options.NONE.asSet()).isEmpty();
130130
}
131131

132+
@Test
133+
void optionsWithoutReturnsNewOptions() {
134+
Options options = Options.of(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
135+
Options without = options.without(Option.IGNORE_PROFILES);
136+
assertThat(options.asSet()).containsExactly(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
137+
assertThat(without.asSet()).containsExactly(Option.IGNORE_IMPORTS);
138+
}
139+
140+
@Test
141+
void optionsWithReturnsNewOptions() {
142+
Options options = Options.of(Option.IGNORE_IMPORTS);
143+
Options with = options.with(Option.IGNORE_PROFILES);
144+
assertThat(options.asSet()).containsExactly(Option.IGNORE_IMPORTS);
145+
assertThat(with.asSet()).containsExactly(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
146+
}
147+
132148
@Test
133149
void propertySourceOptionsAlwaysReturnsSameOptionsEachTime() {
134150
PropertySourceOptions options = PropertySourceOptions.always(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);

0 commit comments

Comments
 (0)