franta-hg@104
|
1 |
/**
|
franta-hg@104
|
2 |
* SQL-DK
|
franta-hg@104
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@104
|
4 |
*
|
franta-hg@104
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@104
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@104
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@104
|
8 |
* (at your option) any later version.
|
franta-hg@104
|
9 |
*
|
franta-hg@104
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@104
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@104
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@104
|
13 |
* GNU General Public License for more details.
|
franta-hg@104
|
14 |
*
|
franta-hg@104
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@104
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@104
|
17 |
*/
|
franta-hg@104
|
18 |
package info.globalcode.sql.dk.configuration;
|
franta-hg@104
|
19 |
|
franta-hg@104
|
20 |
import java.util.ArrayList;
|
franta-hg@104
|
21 |
import javax.xml.bind.annotation.XmlTransient;
|
franta-hg@104
|
22 |
import static info.globalcode.sql.dk.Functions.findByName;
|
franta-hg@105
|
23 |
import java.util.Collections;
|
franta-hg@104
|
24 |
|
franta-hg@104
|
25 |
/**
|
franta-hg@104
|
26 |
* <p>List of configurables.</p>
|
franta-hg@104
|
27 |
*
|
franta-hg@104
|
28 |
* <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
|
franta-hg@104
|
29 |
* look
|
franta-hg@104
|
30 |
* into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
|
franta-hg@104
|
31 |
* in default properties.</p>
|
franta-hg@104
|
32 |
*
|
franta-hg@104
|
33 |
* <p>Typical use: </p>
|
franta-hg@104
|
34 |
* <ul>
|
franta-hg@104
|
35 |
* <li>this instance – ad-hoc properties from CLI options</li>
|
franta-hg@104
|
36 |
* <li>default properties – from config file</li>
|
franta-hg@104
|
37 |
* <li>defaultValue – hardcoded default</li>
|
franta-hg@104
|
38 |
* </ul>
|
franta-hg@104
|
39 |
*
|
franta-hg@104
|
40 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@104
|
41 |
*/
|
franta-hg@105
|
42 |
public class Properties extends ArrayList<Property> implements Cloneable {
|
franta-hg@104
|
43 |
|
franta-hg@104
|
44 |
private Properties defaults;
|
franta-hg@104
|
45 |
|
franta-hg@105
|
46 |
public Properties() {
|
franta-hg@105
|
47 |
}
|
franta-hg@105
|
48 |
|
franta-hg@105
|
49 |
public Properties(int initialCapacity) {
|
franta-hg@105
|
50 |
super(initialCapacity);
|
franta-hg@105
|
51 |
}
|
franta-hg@105
|
52 |
|
franta-hg@104
|
53 |
@XmlTransient
|
franta-hg@104
|
54 |
public Properties getDefaults() {
|
franta-hg@104
|
55 |
return defaults;
|
franta-hg@104
|
56 |
}
|
franta-hg@104
|
57 |
|
franta-hg@104
|
58 |
public void setDefaults(Properties defaults) {
|
franta-hg@104
|
59 |
this.defaults = defaults;
|
franta-hg@104
|
60 |
}
|
franta-hg@104
|
61 |
|
franta-hg@104
|
62 |
private Property findProperty(String name) {
|
franta-hg@104
|
63 |
Property p = findByName(this, name);
|
franta-hg@104
|
64 |
if (p == null) {
|
franta-hg@104
|
65 |
p = findByName(defaults, name);
|
franta-hg@104
|
66 |
}
|
franta-hg@104
|
67 |
return p;
|
franta-hg@104
|
68 |
}
|
franta-hg@104
|
69 |
|
franta-hg@104
|
70 |
public String getString(String name, String defaultValue) {
|
franta-hg@104
|
71 |
Property p = findProperty(name);
|
franta-hg@104
|
72 |
return p == null ? defaultValue : p.getValue();
|
franta-hg@104
|
73 |
}
|
franta-hg@104
|
74 |
|
franta-hg@104
|
75 |
public boolean getBoolean(String name, boolean defaultValue) {
|
franta-hg@104
|
76 |
Property p = findProperty(name);
|
franta-hg@104
|
77 |
return p == null ? defaultValue : Boolean.valueOf(p.getValue());
|
franta-hg@104
|
78 |
}
|
franta-hg@104
|
79 |
|
franta-hg@104
|
80 |
public int getInteger(String name, int defaultValue) {
|
franta-hg@104
|
81 |
Property p = findProperty(name);
|
franta-hg@104
|
82 |
return p == null ? defaultValue : Integer.valueOf(p.getValue());
|
franta-hg@104
|
83 |
}
|
franta-hg@105
|
84 |
|
franta-hg@108
|
85 |
public boolean hasProperty(String name) {
|
franta-hg@108
|
86 |
return findByName(this, name) != null;
|
franta-hg@108
|
87 |
}
|
franta-hg@108
|
88 |
|
franta-hg@105
|
89 |
@Override
|
franta-hg@105
|
90 |
public Properties clone() {
|
franta-hg@105
|
91 |
Properties clone = new Properties(size());
|
franta-hg@105
|
92 |
Collections.copy(clone, this);
|
franta-hg@105
|
93 |
return clone;
|
franta-hg@105
|
94 |
}
|
franta-hg@106
|
95 |
|
franta-hg@106
|
96 |
/**
|
franta-hg@106
|
97 |
* @return merged this and backing defaults as Java Properties
|
franta-hg@106
|
98 |
*/
|
franta-hg@106
|
99 |
public java.util.Properties getJavaProperties() {
|
franta-hg@106
|
100 |
java.util.Properties javaProperties = new java.util.Properties();
|
franta-hg@106
|
101 |
duplicateTo(javaProperties);
|
franta-hg@106
|
102 |
return javaProperties;
|
franta-hg@106
|
103 |
}
|
franta-hg@106
|
104 |
|
franta-hg@106
|
105 |
private void duplicateTo(java.util.Properties javaProperties) {
|
franta-hg@107
|
106 |
if (defaults != null) {
|
franta-hg@107
|
107 |
defaults.duplicateTo(javaProperties);
|
franta-hg@107
|
108 |
}
|
franta-hg@106
|
109 |
for (Property p : this) {
|
franta-hg@106
|
110 |
javaProperties.setProperty(p.getName(), p.getValue());
|
franta-hg@106
|
111 |
}
|
franta-hg@106
|
112 |
}
|
franta-hg@104
|
113 |
}
|