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
This commit is contained in:
parent
86303c017b
commit
d938dd97bf
@ -23,6 +23,7 @@ import java.util.Collections;
|
|||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
@ -224,10 +225,24 @@ public final class ConfigData {
|
|||||||
* @param option the option to exclude
|
* @param option the option to exclude
|
||||||
* @return a new {@link Options} instance
|
* @return a new {@link Options} instance
|
||||||
*/
|
*/
|
||||||
Options without(Option option) {
|
public Options without(Option option) {
|
||||||
|
return copy((options) -> options.remove(option));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link Options} instance that contains the options in this set
|
||||||
|
* including the given option.
|
||||||
|
* @param option the option to include
|
||||||
|
* @return a new {@link Options} instance
|
||||||
|
*/
|
||||||
|
public Options with(Option option) {
|
||||||
|
return copy((options) -> options.add(option));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Options copy(Consumer<EnumSet<Option>> processor) {
|
||||||
EnumSet<Option> options = EnumSet.noneOf(Option.class);
|
EnumSet<Option> options = EnumSet.noneOf(Option.class);
|
||||||
options.addAll(this.options);
|
options.addAll(this.options);
|
||||||
options.remove(option);
|
processor.accept(options);
|
||||||
return new Options(options);
|
return new Options(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +129,22 @@ class ConfigDataTests {
|
|||||||
assertThat(Options.NONE.asSet()).isEmpty();
|
assertThat(Options.NONE.asSet()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void optionsWithoutReturnsNewOptions() {
|
||||||
|
Options options = Options.of(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
|
||||||
|
Options without = options.without(Option.IGNORE_PROFILES);
|
||||||
|
assertThat(options.asSet()).containsExactly(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
|
||||||
|
assertThat(without.asSet()).containsExactly(Option.IGNORE_IMPORTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void optionsWithReturnsNewOptions() {
|
||||||
|
Options options = Options.of(Option.IGNORE_IMPORTS);
|
||||||
|
Options with = options.with(Option.IGNORE_PROFILES);
|
||||||
|
assertThat(options.asSet()).containsExactly(Option.IGNORE_IMPORTS);
|
||||||
|
assertThat(with.asSet()).containsExactly(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void propertySourceOptionsAlwaysReturnsSameOptionsEachTime() {
|
void propertySourceOptionsAlwaysReturnsSameOptionsEachTime() {
|
||||||
PropertySourceOptions options = PropertySourceOptions.always(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
|
PropertySourceOptions options = PropertySourceOptions.always(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user