Skip to content

Package: Configuration

Configuration

nameinstructionbranchcomplexitylinemethod
Configuration()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
addConfiguration(Map, Collection)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getProperty(ConfigurationParameter)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getProperty(ConfigurationParameter, Class)
M: 43 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getProperty(ConfigurationParameter, Class, Object)
M: 37 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getProperty(ConfigurationParameter, Object)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$addConfiguration$0(Map, ConfigurationParameter)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$addConfiguration$1(Map, ConfigurationParameter)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setProperty(ConfigurationParameter, Object)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package cz.cvut.kbss.ontodriver.config;
2:
3: import java.util.Collection;
4: import java.util.Map;
5: import java.util.Objects;
6: import java.util.concurrent.ConcurrentHashMap;
7:
8: /**
9: * Holds configuration of the OntoDriver.
10: */
11: public final class Configuration {
12: // TODO Tests
13:
14: private final ConcurrentHashMap<ConfigurationParameter, Object> configuration = new ConcurrentHashMap<>();
15:
16: /**
17: * Loads configuration of parameters specified by {@code parameters} from the provided properties map.
18: *
19: * @param properties Map of configuration values
20: * @param parameters Parameters to extract from the map
21: */
22: public void addConfiguration(Map<String, String> properties, Collection<ConfigurationParameter> parameters) {
23: Objects.requireNonNull(properties);
24: Objects.requireNonNull(parameters);
25: parameters.stream().filter(p -> properties.containsKey(p.toString()))
26: .forEach(p -> setProperty(p, properties.get(p.toString())));
27: }
28:
29: /**
30: * Sets configuration of the specified parameter to the specified value.
31: * <p>
32: * Overrides any previous setting.
33: *
34: * @param property Parameter
35: * @param value Value
36: */
37: public void setProperty(ConfigurationParameter property, Object value) {
38: Objects.requireNonNull(property);
39: configuration.put(property, value);
40: }
41:
42: /**
43: * Gets value of the specified property.
44: *
45: * @param property Parameter
46: * @return Value of the property or {@code null}, if it is not set
47: */
48: public Object getProperty(ConfigurationParameter property) {
49: Objects.requireNonNull(property);
50: return configuration.get(property);
51: }
52:
53: /**
54: * Gets value of the specified property or the default value, if the property is not set.
55: *
56: * @param property Parameter
57: * @param defaultValue Value to return if the property is not set
58: * @return Value of the property or {@code defaultValue}, if it is not set
59: */
60: public Object getProperty(ConfigurationParameter property, Object defaultValue) {
61: Objects.requireNonNull(property);
62: return configuration.getOrDefault(property, defaultValue);
63: }
64:
65: /**
66: * Returns value of the specified property as the specified type (if it can be cast to it).
67: *
68: * @param property Parameter
69: * @param targetType Target type
70: * @return Property value or {@code null}, if it is not set
71: * @throws IllegalArgumentException if property value cannot be cast to {@code targetType}
72: */
73: public <T> T getProperty(ConfigurationParameter property, Class<T> targetType) {
74:• if (configuration.containsKey(property)) {
75: final Object value = configuration.get(property);
76:• if (value != null && targetType.isAssignableFrom(value.getClass())) {
77: return targetType.cast(value);
78: } else {
79: throw new IllegalArgumentException(
80: "Cannot return value " + value + " of property " + property + " as type " + targetType);
81: }
82: }
83: return null;
84: }
85:
86: /**
87: * Returns value of the specified property as the specified type (if it can be cast to it).
88: *
89: * @param property Parameter
90: * @param targetType Target type
91: * @param defaultValue Value to return if the property is not set
92: * @return Value of the property or {@code defaultValue}, if it is not set
93: * @throws IllegalArgumentException if property value cannot be cast to {@code targetType}
94: */
95: public <T> T getProperty(ConfigurationParameter property, Class<T> targetType, T defaultValue) {
96: final Object value = configuration.getOrDefault(property, defaultValue);
97:• if (value != null && targetType.isAssignableFrom(value.getClass())) {
98: return targetType.cast(value);
99: } else {
100: throw new IllegalArgumentException(
101: "Cannot return value " + value + " of property " + property + " as type " + targetType);
102: }
103: }
104: }