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;
62 private Property findProperty(String name) {
63 Property p = findByName(this, name);
65 p = findByName(defaults, name);
70 public String getString(String name, String defaultValue) {
71 Property p = findProperty(name);
72 return p == null ? defaultValue : p.getValue();
75 public boolean getBoolean(String name, boolean defaultValue) {
76 Property p = findProperty(name);
77 return p == null ? defaultValue : Boolean.valueOf(p.getValue());
80 public int getInteger(String name, int defaultValue) {
81 Property p = findProperty(name);
82 return p == null ? defaultValue : Integer.valueOf(p.getValue());
85 public boolean hasProperty(String name) {
86 return findByName(this, name) != null;
90 public Properties clone() {
91 Properties clone = new Properties(size());
92 Collections.copy(clone, this);
97 * @return merged this and backing defaults as Java Properties
99 public java.util.Properties getJavaProperties() {
100 java.util.Properties javaProperties = new java.util.Properties();
101 duplicateTo(javaProperties);
102 return javaProperties;
105 private void duplicateTo(java.util.Properties javaProperties) {
106 if (defaults != null) {
107 defaults.duplicateTo(javaProperties);
109 for (Property p : this) {
110 javaProperties.setProperty(p.getName(), p.getValue());