Settings.setting() supports generic return type

Issue gh-117
This commit is contained in:
Joe Grandja 2020-10-13 05:17:39 -04:00
parent 628c8bece3
commit ca94d02abc
3 changed files with 35 additions and 4 deletions

View File

@ -68,13 +68,15 @@ public class Settings implements Serializable {
*
* @param name the name of the setting
* @param value the value of the setting
* @param <T> the type of the {@link Settings}
* @return the {@link Settings}
*/
public Settings setting(String name, Object value) {
@SuppressWarnings("unchecked")
public <T extends Settings> T setting(String name, Object value) {
Assert.hasText(name, "name cannot be empty");
Assert.notNull(value, "value cannot be null");
this.settings.put(name, value);
return this;
return (T) this;
}
/**
@ -91,10 +93,12 @@ public class Settings implements Serializable {
* allowing the ability to add, replace, or remove.
*
* @param settingsConsumer a {@link Consumer} of the configuration settings {@code Map}
* @param <T> the type of the {@link Settings}
* @return the {@link Settings}
*/
public Settings settings(Consumer<Map<String, Object>> settingsConsumer) {
@SuppressWarnings("unchecked")
public <T extends Settings> T settings(Consumer<Map<String, Object>> settingsConsumer) {
settingsConsumer.accept(this.settings);
return this;
return (T) this;
}
}

View File

@ -53,4 +53,18 @@ public class ClientSettingsTests {
ClientSettings clientSettings = new ClientSettings().requireUserConsent(true);
assertThat(clientSettings.requireUserConsent()).isTrue();
}
@Test
public void settingWhenCalledThenReturnClientSettings() {
ClientSettings clientSettings = new ClientSettings()
.<ClientSettings>setting("name1", "value1")
.requireProofKey(true)
.<ClientSettings>settings(settings -> settings.put("name2", "value2"))
.requireUserConsent(true);
assertThat(clientSettings.settings()).hasSize(4);
assertThat(clientSettings.requireProofKey()).isTrue();
assertThat(clientSettings.requireUserConsent()).isTrue();
assertThat(clientSettings.<String>setting("name1")).isEqualTo("value1");
assertThat(clientSettings.<String>setting("name2")).isEqualTo("value2");
}
}

View File

@ -49,4 +49,17 @@ public class TokenSettingsTests {
TokenSettings tokenSettings = new TokenSettings().accessTokenTimeToLive(accessTokenTimeToLive);
assertThat(tokenSettings.accessTokenTimeToLive()).isEqualTo(accessTokenTimeToLive);
}
@Test
public void settingWhenCalledThenReturnTokenSettings() {
Duration accessTokenTimeToLive = Duration.ofMinutes(10);
TokenSettings tokenSettings = new TokenSettings()
.<TokenSettings>setting("name1", "value1")
.accessTokenTimeToLive(accessTokenTimeToLive)
.<TokenSettings>settings(settings -> settings.put("name2", "value2"));
assertThat(tokenSettings.settings()).hasSize(3);
assertThat(tokenSettings.accessTokenTimeToLive()).isEqualTo(accessTokenTimeToLive);
assertThat(tokenSettings.<String>setting("name1")).isEqualTo("value1");
assertThat(tokenSettings.<String>setting("name2")).isEqualTo("value2");
}
}