Debian Codegraph @

src/main/org/h2/engine/SettingsBase.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.engine;
7
8 import java.util.Arrays;
9 import java.util.Comparator;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import org.h2.api.ErrorCode;
15 import org.h2.message.DbException;
16 import org.h2.util.Utils;
17
18 /**
19 * The base class for settings.
20 */
21 public class SettingsBase {
22
23 private final HashMap<String, String> settings;
24
25 protected SettingsBase(HashMap<String, String> s) {
26 this.settings = s;
27 }
28
29 /**
30 * Get the setting for the given key.
31 *
32 * @param key the key
33 * @param defaultValue the default value
34 * @return the setting
35 */
36 protected boolean get(String key, boolean defaultValue) {
37 String s = get(key, Boolean.toString(defaultValue));
38 try {
39 return Utils.parseBoolean(s, defaultValue, true);
40 } catch (IllegalArgumentException e) {
41 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1,
42 e, "key:" + key + " value:" + s);
43 }
44 }
45
46 /**
47 * Set an entry in the key-value pair.
48 *
49 * @param key the key
50 * @param value the value
51 */
52 void set(String key, boolean value) {
53 settings.put(key, Boolean.toString(value));
54 }
55
56 /**
57 * Get the setting for the given key.
58 *
59 * @param key the key
60 * @param defaultValue the default value
61 * @return the setting
62 */
63 protected int get(String key, int defaultValue) {
64 String s = get(key, Integer.toString(defaultValue));
65 try {
66 return Integer.decode(s);
67 } catch (NumberFormatException e) {
68 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1,
69 e, "key:" + key + " value:" + s);
70 }
71 }
72
73 /**
74 * Get the setting for the given key.
75 *
76 * @param key the key
77 * @param defaultValue the default value
78 * @return the setting
79 */
80 protected String get(String key, String defaultValue) {
81 String v = settings.get(key);
82 if (v != null) {
83 return v;
84 }
85 StringBuilder buff = new StringBuilder("h2.");
86 boolean nextUpper = false;
87 for (int i = 0, l = key.length(); i < l; i++) {
88 char c = key.charAt(i);
89 if (c == '_') {
90 nextUpper = true;
91 } else {
92 // Character.toUpperCase / toLowerCase ignores the locale
93 buff.append(nextUpper ? Character.toUpperCase(c) : Character.toLowerCase(c));
94 nextUpper = false;
95 }
96 }
97 String sysProperty = buff.toString();
98 v = Utils.getProperty(sysProperty, defaultValue);
99 settings.put(key, v);
100 return v;
101 }
102
103 /**
104 * Check if the settings contains the given key.
105 *
106 * @param k the key
107 * @return true if they do
108 */
109 protected boolean containsKey(String k) {
110 return settings.containsKey(k);
111 }
112
113 /**
114 * Get all settings.
115 *
116 * @return the settings
117 */
118 public HashMap<String, String> getSettings() {
119 return settings;
120 }
121
122 /**
123 * Get all settings in alphabetical order.
124 *
125 * @return the settings
126 */
127 public Entry<String, String>[] getSortedSettings() {
128 @SuppressWarnings("unchecked")
129 Map.Entry<String, String>[] entries = settings.entrySet().toArray(new Map.Entry[0]);
130 Arrays.sort(entries, Comparator.comparing(Entry::getKey));
131 return entries;
132 }
133
134 }