File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\engine\SettingsBase.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.engine;
8
9 import java.util.HashMap;
10 import org.h2.constant.ErrorCode;
11 import org.h2.message.DbException;
12 import org.h2.util.Utils;
13
14 /**
15 * The base class for settings.
16 */
17 public class SettingsBase {
18
19 private final HashMap<String, String> settings;
20
21 protected SettingsBase(HashMap<String, String> s) {
22 this.settings = s;
23 }
24
25 /**
26 * Get the setting for the given key.
27 *
28 * @param key the key
29 * @param defaultValue the default value
30 * @return the setting
31 */
32 protected boolean get(String key, boolean defaultValue) {
33 String s = get(key, "" + defaultValue);
34 try {
35 return Boolean.parseBoolean(s);
36 } catch (NumberFormatException e) {
37 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, "key:" + key + " value:" + s);
38 }
39 }
40
41 /**
42 * Get the setting for the given key.
43 *
44 * @param key the key
45 * @param defaultValue the default value
46 * @return the setting
47 */
48 protected int get(String key, int defaultValue) {
49 String s = get(key, "" + defaultValue);
50 try {
51 return Integer.decode(s);
52 } catch (NumberFormatException e) {
53 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, "key:" + key + " value:" + s);
54 }
55 }
56
57 /**
58 * Get the setting for the given key.
59 *
60 * @param key the key
61 * @param defaultValue the default value
62 * @return the setting
63 */
64 protected String get(String key, String defaultValue) {
65 StringBuilder buff = new StringBuilder("h2.");
66 boolean nextUpper = false;
67 for (char c : key.toCharArray()) {
68 if (c == '_') {
69 nextUpper = true;
70 } else {
71 // Character.toUpperCase / toLowerCase ignores the locale
72 buff.append(nextUpper ? Character.toUpperCase(c) : Character.toLowerCase(c));
73 nextUpper = false;
74 }
75 }
76 String sysProperty = buff.toString();
77 String v = settings.get(key);
78 if (v == null) {
79 v = Utils.getProperty(sysProperty, defaultValue);
80 settings.put(key, v);
81 }
82 return v;
83 }
84
85 /**
86 * Check if the settings contains the given key.
87 *
88 * @param k the key
89 * @return true if they do
90 */
91 protected boolean containsKey(String k) {
92 return settings.containsKey(k);
93 }
94
95 /**
96 * Get all settings.
97 *
98 * @return the settings
99 */
100 public HashMap<String, String> getSettings() {
101 return settings;
102 }
103
104 }
105