3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.configuration;
19 import java.util.ArrayList;
20 import javax.xml.bind.annotation.XmlTransient;
21 import static info.globalcode.sql.dk.Functions.findByName;
22 import java.util.Collections;
26 * List of configurables.</p>
29 * Can be backed by defaults – if value for given name is nof found in this instance, we will
30 * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
31 * even in default properties.</p>
36 * <li>this instance – ad-hoc properties from CLI options</li>
37 * <li>default properties – from config file</li>
38 * <li>defaultValue – hardcoded default</li>
41 * @author Ing. František Kučera (frantovo.cz)
43 public class Properties extends ArrayList<Property> implements Cloneable {
45 private Properties defaults;
50 public Properties(int initialCapacity) {
51 super(initialCapacity);
55 public Properties getDefaults() {
59 public void setDefaults(Properties defaults) {
60 this.defaults = defaults;
64 * @param defaults the last/deepest defaults
66 public void setLastDefaults(Properties defaults) {
67 if (this.defaults == null) {
68 this.defaults = defaults;
70 this.defaults.setLastDefaults(defaults);
74 private Property findProperty(String name) {
75 Property p = findByName(this, name);
76 if (p == null && defaults != null) {
77 p = defaults.findProperty(name);
82 public String getString(String name, String defaultValue) {
83 Property p = findProperty(name);
84 return p == null ? defaultValue : p.getValue();
87 public boolean getBoolean(String name, boolean defaultValue) {
88 Property p = findProperty(name);
89 return p == null ? defaultValue : Boolean.valueOf(p.getValue());
92 public int getInteger(String name, int defaultValue) {
93 Property p = findProperty(name);
94 return p == null ? defaultValue : Integer.valueOf(p.getValue());
97 public boolean hasProperty(String name) {
98 return findByName(this, name) != null;
102 public Properties clone() {
103 Properties clone = new Properties(size());
104 Collections.copy(clone, this);
109 * @return merged this and backing defaults as Java Properties
111 public java.util.Properties getJavaProperties() {
112 java.util.Properties javaProperties = new java.util.Properties();
113 duplicateTo(javaProperties);
114 return javaProperties;
117 private void duplicateTo(java.util.Properties javaProperties) {
118 if (defaults != null) {
119 defaults.duplicateTo(javaProperties);
121 for (Property p : this) {
122 String value = p.getValue();
124 javaProperties.setProperty(p.getName(), value);