-
Notifications
You must be signed in to change notification settings - Fork 14
/
DriverConfiguration.java
129 lines (112 loc) · 4.39 KB
/
DriverConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.config;
import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Holds configuration of the OntoDriver.
*/
public final class DriverConfiguration {
private final Map<ConfigurationParameter, String> configuration = new HashMap<>();
private final OntologyStorageProperties storageProperties;
public DriverConfiguration(OntologyStorageProperties storageProperties) {
this.storageProperties = Objects.requireNonNull(storageProperties);
}
/**
* Loads configuration of parameters specified by {@code parameters} from the provided properties map.
*
* @param properties Map of configuration values
* @param parameters Parameters to extract from the map
*/
public void addConfiguration(Map<String, String> properties, Collection<ConfigurationParameter> parameters) {
Objects.requireNonNull(properties);
Objects.requireNonNull(parameters);
parameters.stream().filter(p -> properties.containsKey(p.toString()))
.forEach(p -> setProperty(p, properties.get(p.toString())));
}
/**
* Sets configuration of the specified parameter to the specified value.
* <p>
* Overrides any previous setting.
*
* @param property Parameter
* @param value Value
*/
public void setProperty(ConfigurationParameter property, String value) {
Objects.requireNonNull(property);
configuration.put(property, value);
}
/**
* Gets value of the specified property.
*
* @param property Parameter
* @return Value of the property or {@code null}, if it is not set
*/
public String getProperty(ConfigurationParameter property) {
Objects.requireNonNull(property);
return configuration.get(property);
}
/**
* Gets value of the specified property or the default value, if the property is not set.
*
* @param property Parameter
* @param defaultValue Value to return if the property is not set
* @return Value of the property or {@code defaultValue}, if it is not set
*/
public String getProperty(ConfigurationParameter property, String defaultValue) {
Objects.requireNonNull(property);
return configuration.getOrDefault(property, defaultValue);
}
/**
* Returns value of the specified property as boolean.
* <p>
* If the property is not configured, {@code false} is returned.
*
* @param property Parameter
* @return Property value (false for unknown)
*/
public boolean is(ConfigurationParameter property) {
Objects.requireNonNull(property);
return Boolean.parseBoolean(configuration.get(property));
}
/**
* Checks whether the specified property is set in this configuration.
*
* @param property The property to check
* @return Whether the specified property is set here
*/
public boolean isSet(ConfigurationParameter property) {
return configuration.containsKey(property);
}
public OntologyStorageProperties getStorageProperties() {
return storageProperties;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DriverConfiguration that = (DriverConfiguration) o;
return configuration.equals(that.configuration) && storageProperties.equals(that.storageProperties);
}
@Override
public int hashCode() {
int result = configuration.hashCode();
result = 31 * result + storageProperties.hashCode();
return result;
}
}