Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
  • Loading branch information
Kuanysh-kst committed May 12, 2023
1 parent de93caa commit 5c36308
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,21 @@ public synchronized Settings applySettings(Settings newSettings) {

/**
* Adds a settings consumer with a predicate that is only evaluated at update time.
* <p>
* Note: Only settings registered in {@link SettingsModule} can be changed dynamically.
* </p>
* @param validator an additional validator that is only applied to updates of this setting.
* This is useful to add additional validation to settings at runtime compared to at startup time.
* This method allows registering an additional validator that is only applied to updates of a specific setting.
* It is useful to add additional validation to settings at runtime compared to at startup time.
* Please note that only settings registered in the {@link SettingsModule} can be changed dynamically.
*
* @param setting The setting for which the consumer is registered.
* @param consumer The consumer to be invoked when the setting is updated.
* @param validator An additional validator that is only applied to updates of this setting.
* @throws SettingsException if the setting is not registered for the given key.
* @throws NullPointerException if the setting is not registered.
*/
public synchronized <T> void addSettingsUpdateConsumer(Setting<T> setting, Consumer<T> consumer, Consumer<T> validator) {
if (!setting.equals(get(setting.getKey()))) {
if (setting.getKey() != null && !setting.equals(get(setting.getKey()))) {
throw new SettingsException("Setting is not registered for key [" + setting.getKey() + "]");
} else if (setting.getKey() == null) {
logger.error("not registered setting key");
throw new NullPointerException("Setting is not registered");
} else {
addSettingsUpdater(setting.newUpdater(consumer, logger, validator));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1472,8 +1472,12 @@ public void testAddSettingsUpdateConsumer() {
service.addSettingsUpdateConsumer(testSetting2, consumer2::set, (s) -> assertTrue(s > 0));
Setting<Integer> wrongKeySetting = Setting.intSetting("foo.bar.wrong", 1, Property.Dynamic, Property.NodeScope);

expectThrows(SettingsException.class, () -> {
service.addSettingsUpdateConsumer(wrongKeySetting, consumer2::set, (i) -> { if (i == 42) throw new AssertionError("boom"); });
});
expectThrows(SettingsException.class, () -> service.addSettingsUpdateConsumer(wrongKeySetting, consumer2::set, (i) -> {
if (i == 42) throw new AssertionError("wrong key");
}));

expectThrows(NullPointerException.class, () -> service.addSettingsUpdateConsumer(null, consumer2::set, (i) -> {
if (i == 42) throw new AssertionError("empty key");
}));
}
}

0 comments on commit 5c36308

Please sign in to comment.