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