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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.configuration;
20 import java.util.ArrayList;
21 import javax.xml.bind.annotation.XmlTransient;
22 import static info.globalcode.sql.dk.Functions.findByName;
23 import java.util.Collections;
26 * <p>List of configurables.</p>
28 * <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
30 * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
31 * in default properties.</p>
33 * <p>Typical use: </p>
35 * <li>this instance – ad-hoc properties from CLI options</li>
36 * <li>default properties – from config file</li>
37 * <li>defaultValue – hardcoded default</li>
40 * @author Ing. František Kučera (frantovo.cz)
42 public class Properties extends ArrayList<Property> implements Cloneable {
44 private Properties defaults;
49 public Properties(int initialCapacity) {
50 super(initialCapacity);
54 public Properties getDefaults() {
58 public void setDefaults(Properties defaults) {
59 this.defaults = defaults;
63 * @param defaults the last/deepest defaults
65 public void setLastDefaults(Properties defaults) {
66 if (this.defaults == null) {
67 this.defaults = defaults;
69 this.defaults.setLastDefaults(defaults);
73 private Property findProperty(String name) {
74 Property p = findByName(this, name);
75 if (p == null && defaults != null) {
76 p = defaults.findProperty(name);
81 public String getString(String name, String defaultValue) {
82 Property p = findProperty(name);
83 return p == null ? defaultValue : p.getValue();
86 public boolean getBoolean(String name, boolean defaultValue) {
87 Property p = findProperty(name);
88 return p == null ? defaultValue : Boolean.valueOf(p.getValue());
91 public int getInteger(String name, int defaultValue) {
92 Property p = findProperty(name);
93 return p == null ? defaultValue : Integer.valueOf(p.getValue());
96 public boolean hasProperty(String name) {
97 return findByName(this, name) != null;
101 public Properties clone() {
102 Properties clone = new Properties(size());
103 Collections.copy(clone, this);
108 * @return merged this and backing defaults as Java Properties
110 public java.util.Properties getJavaProperties() {
111 java.util.Properties javaProperties = new java.util.Properties();
112 duplicateTo(javaProperties);
113 return javaProperties;
116 private void duplicateTo(java.util.Properties javaProperties) {
117 if (defaults != null) {
118 defaults.duplicateTo(javaProperties);
120 for (Property p : this) {
121 javaProperties.setProperty(p.getName(), p.getValue());