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