Skip to content

Method: getStorageProperties()

1: /**
2: * Copyright (C) 2016 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 Configuration {
28:
29: private final Map<ConfigurationParameter, String> configuration = new HashMap<>();
30: private final OntologyStorageProperties storageProperties;
31:
32: public Configuration(OntologyStorageProperties storageProperties) {
33: Objects.requireNonNull(storageProperties);
34: this.storageProperties = storageProperties;
35: }
36:
37: /**
38: * Loads configuration of parameters specified by {@code parameters} from the provided properties map.
39: *
40: * @param properties Map of configuration values
41: * @param parameters Parameters to extract from the map
42: */
43: public void addConfiguration(Map<String, String> properties, Collection<ConfigurationParameter> parameters) {
44: Objects.requireNonNull(properties);
45: Objects.requireNonNull(parameters);
46: parameters.stream().filter(p -> properties.containsKey(p.toString()))
47: .forEach(p -> setProperty(p, properties.get(p.toString())));
48: }
49:
50: /**
51: * Sets configuration of the specified parameter to the specified value.
52: * <p>
53: * Overrides any previous setting.
54: *
55: * @param property Parameter
56: * @param value Value
57: */
58: public void setProperty(ConfigurationParameter property, String value) {
59: Objects.requireNonNull(property);
60: configuration.put(property, value);
61: }
62:
63: /**
64: * Gets value of the specified property.
65: *
66: * @param property Parameter
67: * @return Value of the property or {@code null}, if it is not set
68: */
69: public String getProperty(ConfigurationParameter property) {
70: Objects.requireNonNull(property);
71: return configuration.get(property);
72: }
73:
74: /**
75: * Gets value of the specified property or the default value, if the property is not set.
76: *
77: * @param property Parameter
78: * @param defaultValue Value to return if the property is not set
79: * @return Value of the property or {@code defaultValue}, if it is not set
80: */
81: public String getProperty(ConfigurationParameter property, String defaultValue) {
82: Objects.requireNonNull(property);
83: return configuration.getOrDefault(property, defaultValue);
84: }
85:
86: /**
87: * Returns value of the specified property as boolean.
88: * <p>
89: * If the property is not configured, {@code false} is returned.
90: *
91: * @param property Parameter
92: * @return Property value (false for unknown)
93: */
94: public boolean is(ConfigurationParameter property) {
95: if (configuration.containsKey(property)) {
96: final String value = configuration.get(property);
97: return Boolean.parseBoolean(value);
98: }
99: return false;
100: }
101:
102: /**
103: * Checks whether the specified property is set in this configuration.
104: *
105: * @param property The property to check
106: * @return Whether the specified property is set here
107: */
108: public boolean isSet(ConfigurationParameter property) {
109: return configuration.containsKey(property);
110: }
111:
112: public OntologyStorageProperties getStorageProperties() {
113: return storageProperties;
114: }
115:
116: @Override
117: public boolean equals(Object o) {
118: if (this == o) return true;
119: if (o == null || getClass() != o.getClass()) return false;
120:
121: Configuration that = (Configuration) 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: }