Skip to contentMethod: is(String)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.utils;
19:
20: import java.util.Collections;
21: import java.util.HashMap;
22: import java.util.Map;
23: import java.util.Objects;
24:
25: /**
26: * Contains configuration for the persistence provider and utility access methods for it.
27: * <p>
28: * This class is thread safe.
29: */
30: public final class Configuration {
31:
32: private final Map<String, String> properties;
33:
34: public Configuration() {
35: this.properties = new HashMap<>();
36: }
37:
38: public Configuration(Map<String, String> properties) {
39: Objects.requireNonNull(properties);
40: this.properties = new HashMap<>(properties);
41: }
42:
43: /**
44: * Gets the configured value of the specified property.
45: *
46: * @param property Property whose value to get
47: * @return Configured value, {@code null} if property is not configured
48: */
49: public String get(String property) {
50: return get(property, null);
51: }
52:
53: /**
54: * Gets the configured value of the specified property.
55: * <p>
56: * The specified default value is returned if the property is not configured.
57: *
58: * @param property Property whose value to get
59: * @param defaultValue Default value to return if no value is configured
60: * @return Configured value, {@code defaultValue} if property is not configured
61: */
62: public String get(String property, String defaultValue) {
63: return properties.getOrDefault(property, defaultValue);
64: }
65:
66: /**
67: * Gets the configured value of the specified boolean-valued property.
68: *
69: * @param property Property whose value to get
70: * @return {@code true} if the specified property is configured and its value is the boolean true, {@code false}
71: * otherwise
72: */
73: public boolean is(String property) {
74: final String propertyValue = get(property, null);
75: return Boolean.parseBoolean(propertyValue);
76: }
77:
78: /**
79: * Sets the specified value for the specified property.
80: *
81: * @param property Property whose value to set
82: * @param value Value to set
83: */
84: public synchronized void set(String property, String value) {
85: Objects.requireNonNull(property);
86: properties.put(property, value);
87: }
88:
89: /**
90: * Checks whether this configuration contains an explicitly set value of the specified property.
91: *
92: * @param property Property whose presence to check
93: * @return {@code true} if the specified property is configured, {@code false} otherwise
94: */
95: public boolean contains(String property) {
96: return properties.containsKey(property);
97: }
98:
99: /**
100: * Gets an unmodifiable view of the underlying map of configuration.
101: *
102: * @return Unmodifiable Map
103: */
104: public Map<String, String> getProperties() {
105: return Collections.unmodifiableMap(properties);
106: }
107: }