Skip to content

Method: hashCode()

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