java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.configuration;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlTransient;
import static info.globalcode.sql.dk.Functions.findByName;
import java.util.Collections;

/**
 * <p>
 * List of configurables.</p>
 *
 * <p>
 * Can be backed by defaults – if value for given name is nof found in this instance, we will
 * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
 * even in default properties.</p>
 *
 * <p>
 * Typical use: </p>
 * <ul>
 * <li>this instance – ad-hoc properties from CLI options</li>
 * <li>default properties – from config file</li>
 * <li>defaultValue – hardcoded default</li>
 * </ul>
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class Properties extends ArrayList<Property> implements Cloneable {

	private Properties defaults;

	public Properties() {
	}

	public Properties(int initialCapacity) {
		super(initialCapacity);
	}

	@XmlTransient
	public Properties getDefaults() {
		return defaults;
	}

	public void setDefaults(Properties defaults) {
		this.defaults = defaults;
	}

	/**
	 * @param defaults the last/deepest defaults
	 */
	public void setLastDefaults(Properties defaults) {
		if (this.defaults == null) {
			this.defaults = defaults;
		} else {
			this.defaults.setLastDefaults(defaults);
		}
	}

	private Property findProperty(String name) {
		Property p = findByName(this, name);
		if (p == null && defaults != null) {
			p = defaults.findProperty(name);
		}
		return p;
	}

	public String getString(String name, String defaultValue) {
		Property p = findProperty(name);
		return p == null ? defaultValue : p.getValue();
	}

	public boolean getBoolean(String name, boolean defaultValue) {
		Property p = findProperty(name);
		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
	}

	public int getInteger(String name, int defaultValue) {
		Property p = findProperty(name);
		return p == null ? defaultValue : Integer.valueOf(p.getValue());
	}

	public boolean hasProperty(String name) {
		return findByName(this, name) != null;
	}

	@Override
	public Properties clone() {
		Properties clone = new Properties(size());
		Collections.copy(clone, this);
		return clone;
	}

	/**
	 * @return merged this and backing defaults as Java Properties
	 */
	public java.util.Properties getJavaProperties() {
		java.util.Properties javaProperties = new java.util.Properties();
		duplicateTo(javaProperties);
		return javaProperties;
	}

	private void duplicateTo(java.util.Properties javaProperties) {
		if (defaults != null) {
			defaults.duplicateTo(javaProperties);
		}
		for (Property p : this) {
			String value = p.getValue();
			if (value != null) {
				javaProperties.setProperty(p.getName(), value);
			}
		}
	}
}