java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
branchv_0
changeset 104 245f1b88a3e6
child 105 39d8b427e20f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java	Tue Dec 31 17:35:33 2013 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.configuration;
    1.22 +
    1.23 +import java.util.ArrayList;
    1.24 +import javax.xml.bind.annotation.XmlTransient;
    1.25 +import static info.globalcode.sql.dk.Functions.findByName;
    1.26 +
    1.27 +/**
    1.28 + * <p>List of configurables.</p>
    1.29 + *
    1.30 + * <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
    1.31 + * look
    1.32 + * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
    1.33 + * in default properties.</p>
    1.34 + *
    1.35 + * <p>Typical use: </p>
    1.36 + * <ul>
    1.37 + * <li>this instance – ad-hoc properties from CLI options</li>
    1.38 + * <li>default properties – from config file</li>
    1.39 + * <li>defaultValue – hardcoded default</li>
    1.40 + * </ul>
    1.41 + *
    1.42 + * @author Ing. František Kučera (frantovo.cz)
    1.43 + */
    1.44 +public class Properties extends ArrayList<Property> {
    1.45 +
    1.46 +	private Properties defaults;
    1.47 +
    1.48 +	@XmlTransient
    1.49 +	public Properties getDefaults() {
    1.50 +		return defaults;
    1.51 +	}
    1.52 +
    1.53 +	public void setDefaults(Properties defaults) {
    1.54 +		this.defaults = defaults;
    1.55 +	}
    1.56 +
    1.57 +	private Property findProperty(String name) {
    1.58 +		Property p = findByName(this, name);
    1.59 +		if (p == null) {
    1.60 +			p = findByName(defaults, name);
    1.61 +		}
    1.62 +		return p;
    1.63 +	}
    1.64 +
    1.65 +	public String getString(String name, String defaultValue) {
    1.66 +		Property p = findProperty(name);
    1.67 +		return p == null ? defaultValue : p.getValue();
    1.68 +	}
    1.69 +
    1.70 +	public boolean getBoolean(String name, boolean defaultValue) {
    1.71 +		Property p = findProperty(name);
    1.72 +		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    1.73 +	}
    1.74 +
    1.75 +	public int getInteger(String name, int defaultValue) {
    1.76 +		Property p = findProperty(name);
    1.77 +		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    1.78 +	}
    1.79 +}