Skip to content

Commit

Permalink
Add with/without methods to ConfigData.Options
Browse files Browse the repository at this point in the history
Add convenience methods to ConfigData.Options to allow new Options
instances to be created with options excluded or included.

See gh-25766
  • Loading branch information
philwebb committed Apr 8, 2021
1 parent 86303c0 commit d938dd9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
Expand Down Expand Up @@ -224,10 +225,24 @@ public String toString() {
* @param option the option to exclude
* @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);
options.addAll(this.options);
options.remove(option);
processor.accept(options);
return new Options(options);
}

Expand Down
Expand Up @@ -129,6 +129,22 @@ void optionsNoneReturnsEmptyOptions() {
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
void propertySourceOptionsAlwaysReturnsSameOptionsEachTime() {
PropertySourceOptions options = PropertySourceOptions.always(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
Expand Down

0 comments on commit d938dd9

Please sign in to comment.