Skip to content

refactor: make AbstractConfigurationService easier to use by sub-classes #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

public abstract class AbstractConfigurationService implements ConfigurationService {

Expand All @@ -17,10 +18,21 @@ public AbstractConfigurationService(Version version) {
}

protected <R extends CustomResource> void register(ControllerConfiguration<R> config) {
put(config, true);
}

protected <R extends CustomResource> void replace(ControllerConfiguration<R> config) {
put(config, false);
}

private <R extends CustomResource> void put(
ControllerConfiguration<R> config, boolean failIfExisting) {
final var name = config.getName();
final var existing = configurations.get(name);
if (existing != null) {
throwExceptionOnNameCollision(config.getAssociatedControllerClassName(), existing);
if (failIfExisting) {
final var existing = configurations.get(name);
if (existing != null) {
throwExceptionOnNameCollision(config.getAssociatedControllerClassName(), existing);
}
}
configurations.put(name, config);
}
Expand All @@ -39,7 +51,19 @@ protected void throwExceptionOnNameCollision(
@Override
public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
ResourceController<R> controller) {
return configurations.get(ControllerUtils.getNameFor(controller));
return configurations.get(keyFor(controller));
}

protected String keyFor(ResourceController controller) {
return ControllerUtils.getNameFor(controller);
}

protected ControllerConfiguration getFor(String controllerName) {
return configurations.get(controllerName);
}

protected Stream<ControllerConfiguration> controllerConfigurations() {
return configurations.values().stream();
}

@Override
Expand Down