Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration service to allow primitive configurations to be add…
…ed, modified, and read.
- Loading branch information
Showing
4 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
core/src/main/java/io/atomix/core/config/ConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
/* | |||
* Copyright 2018-present Open Networking Foundation | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package io.atomix.core.config; | |||
|
|||
import io.atomix.primitive.PrimitiveConfig; | |||
|
|||
/** | |||
* Configuration service. | |||
*/ | |||
public interface ConfigService { | |||
|
|||
/** | |||
* Returns the registered configuration for the given primitive. | |||
* | |||
* @param primitiveName the primitive name | |||
* @param <C> the configuration type | |||
* @return the primitive configuration | |||
*/ | |||
<C extends PrimitiveConfig<C>> C getConfig(String primitiveName); | |||
|
|||
/** | |||
* Adds a primitive configuration. | |||
* <p> | |||
* If a configuration is already registered it will not be overridden. The returned configuration represents the | |||
* configuration that will be used when constructing a new primitive with the given name. | |||
* | |||
* @param config the configuration to add | |||
* @return the registered primitive configuration | |||
*/ | |||
PrimitiveConfig addConfig(PrimitiveConfig config); | |||
|
|||
} |
49 changes: 49 additions & 0 deletions
49
core/src/main/java/io/atomix/core/config/impl/DefaultConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
/* | |||
* Copyright 2018-present Open Networking Foundation | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package io.atomix.core.config.impl; | |||
|
|||
import com.google.common.collect.Maps; | |||
import io.atomix.core.config.ConfigService; | |||
import io.atomix.primitive.PrimitiveConfig; | |||
|
|||
import java.util.Collection; | |||
import java.util.Map; | |||
|
|||
import static com.google.common.base.Preconditions.checkNotNull; | |||
|
|||
/** | |||
* Default configuration service. | |||
*/ | |||
public class DefaultConfigService implements ConfigService { | |||
private final Map<String, PrimitiveConfig> configs = Maps.newConcurrentMap(); | |||
|
|||
public DefaultConfigService(Collection<PrimitiveConfig> configs) { | |||
configs.forEach(config -> this.configs.put(config.getName(), config)); | |||
} | |||
|
|||
@Override | |||
@SuppressWarnings("unchecked") | |||
public <C extends PrimitiveConfig<C>> C getConfig(String primitiveName) { | |||
return (C) configs.get(primitiveName); | |||
} | |||
|
|||
@Override | |||
public PrimitiveConfig addConfig(PrimitiveConfig config) { | |||
checkNotNull(config, "config cannot be null"); | |||
PrimitiveConfig previous = configs.putIfAbsent(config.getName(), config); | |||
return previous != null ? previous : config; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters