java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java
branchv_0
changeset 238 4a1864c3e867
parent 191 862d0a8747ac
child 250 aae5009bd0af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java	Mon Mar 04 20:15:24 2019 +0100
     1.3 @@ -0,0 +1,129 @@
     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 +import java.util.Collections;
    1.27 +
    1.28 +/**
    1.29 + * <p>
    1.30 + * List of configurables.</p>
    1.31 + *
    1.32 + * <p>
    1.33 + * Can be backed by defaults – if value for given name is nof found in this instance, we will
    1.34 + * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
    1.35 + * even in default properties.</p>
    1.36 + *
    1.37 + * <p>
    1.38 + * Typical use: </p>
    1.39 + * <ul>
    1.40 + * <li>this instance – ad-hoc properties from CLI options</li>
    1.41 + * <li>default properties – from config file</li>
    1.42 + * <li>defaultValue – hardcoded default</li>
    1.43 + * </ul>
    1.44 + *
    1.45 + * @author Ing. František Kučera (frantovo.cz)
    1.46 + */
    1.47 +public class Properties extends ArrayList<Property> implements Cloneable {
    1.48 +
    1.49 +	private Properties defaults;
    1.50 +
    1.51 +	public Properties() {
    1.52 +	}
    1.53 +
    1.54 +	public Properties(int initialCapacity) {
    1.55 +		super(initialCapacity);
    1.56 +	}
    1.57 +
    1.58 +	@XmlTransient
    1.59 +	public Properties getDefaults() {
    1.60 +		return defaults;
    1.61 +	}
    1.62 +
    1.63 +	public void setDefaults(Properties defaults) {
    1.64 +		this.defaults = defaults;
    1.65 +	}
    1.66 +
    1.67 +	/**
    1.68 +	 * @param defaults the last/deepest defaults
    1.69 +	 */
    1.70 +	public void setLastDefaults(Properties defaults) {
    1.71 +		if (this.defaults == null) {
    1.72 +			this.defaults = defaults;
    1.73 +		} else {
    1.74 +			this.defaults.setLastDefaults(defaults);
    1.75 +		}
    1.76 +	}
    1.77 +
    1.78 +	private Property findProperty(String name) {
    1.79 +		Property p = findByName(this, name);
    1.80 +		if (p == null && defaults != null) {
    1.81 +			p = defaults.findProperty(name);
    1.82 +		}
    1.83 +		return p;
    1.84 +	}
    1.85 +
    1.86 +	public String getString(String name, String defaultValue) {
    1.87 +		Property p = findProperty(name);
    1.88 +		return p == null ? defaultValue : p.getValue();
    1.89 +	}
    1.90 +
    1.91 +	public boolean getBoolean(String name, boolean defaultValue) {
    1.92 +		Property p = findProperty(name);
    1.93 +		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    1.94 +	}
    1.95 +
    1.96 +	public int getInteger(String name, int defaultValue) {
    1.97 +		Property p = findProperty(name);
    1.98 +		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    1.99 +	}
   1.100 +
   1.101 +	public boolean hasProperty(String name) {
   1.102 +		return findByName(this, name) != null;
   1.103 +	}
   1.104 +
   1.105 +	@Override
   1.106 +	public Properties clone() {
   1.107 +		Properties clone = new Properties(size());
   1.108 +		Collections.copy(clone, this);
   1.109 +		return clone;
   1.110 +	}
   1.111 +
   1.112 +	/**
   1.113 +	 * @return merged this and backing defaults as Java Properties
   1.114 +	 */
   1.115 +	public java.util.Properties getJavaProperties() {
   1.116 +		java.util.Properties javaProperties = new java.util.Properties();
   1.117 +		duplicateTo(javaProperties);
   1.118 +		return javaProperties;
   1.119 +	}
   1.120 +
   1.121 +	private void duplicateTo(java.util.Properties javaProperties) {
   1.122 +		if (defaults != null) {
   1.123 +			defaults.duplicateTo(javaProperties);
   1.124 +		}
   1.125 +		for (Property p : this) {
   1.126 +			String value = p.getValue();
   1.127 +			if (value != null) {
   1.128 +				javaProperties.setProperty(p.getName(), value);
   1.129 +			}
   1.130 +		}
   1.131 +	}
   1.132 +}