franta-hg@104: /** franta-hg@104: * SQL-DK franta-hg@104: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@104: * franta-hg@104: * This program is free software: you can redistribute it and/or modify franta-hg@104: * it under the terms of the GNU General Public License as published by franta-hg@104: * the Free Software Foundation, either version 3 of the License, or franta-hg@104: * (at your option) any later version. franta-hg@104: * franta-hg@104: * This program is distributed in the hope that it will be useful, franta-hg@104: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@104: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@104: * GNU General Public License for more details. franta-hg@104: * franta-hg@104: * You should have received a copy of the GNU General Public License franta-hg@104: * along with this program. If not, see . franta-hg@104: */ franta-hg@104: package info.globalcode.sql.dk.configuration; franta-hg@104: franta-hg@104: import java.util.ArrayList; franta-hg@104: import javax.xml.bind.annotation.XmlTransient; franta-hg@104: import static info.globalcode.sql.dk.Functions.findByName; franta-hg@105: import java.util.Collections; franta-hg@104: franta-hg@104: /** franta-hg@191: *

franta-hg@191: * List of configurables.

franta-hg@104: * franta-hg@191: *

franta-hg@191: * Can be backed by defaults – if value for given name is nof found in this instance, we will franta-hg@155: * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found franta-hg@155: * even in default properties.

franta-hg@104: * franta-hg@191: *

franta-hg@191: * Typical use:

franta-hg@104: * franta-hg@104: * franta-hg@104: * @author Ing. František Kučera (frantovo.cz) franta-hg@104: */ franta-hg@105: public class Properties extends ArrayList implements Cloneable { franta-hg@104: franta-hg@104: private Properties defaults; franta-hg@104: franta-hg@105: public Properties() { franta-hg@105: } franta-hg@105: franta-hg@105: public Properties(int initialCapacity) { franta-hg@105: super(initialCapacity); franta-hg@105: } franta-hg@105: franta-hg@104: @XmlTransient franta-hg@104: public Properties getDefaults() { franta-hg@104: return defaults; franta-hg@104: } franta-hg@104: franta-hg@104: public void setDefaults(Properties defaults) { franta-hg@104: this.defaults = defaults; franta-hg@104: } franta-hg@104: franta-hg@133: /** franta-hg@133: * @param defaults the last/deepest defaults franta-hg@133: */ franta-hg@133: public void setLastDefaults(Properties defaults) { franta-hg@133: if (this.defaults == null) { franta-hg@133: this.defaults = defaults; franta-hg@133: } else { franta-hg@133: this.defaults.setLastDefaults(defaults); franta-hg@133: } franta-hg@133: } franta-hg@133: franta-hg@104: private Property findProperty(String name) { franta-hg@104: Property p = findByName(this, name); franta-hg@133: if (p == null && defaults != null) { franta-hg@133: p = defaults.findProperty(name); franta-hg@104: } franta-hg@104: return p; franta-hg@104: } franta-hg@104: franta-hg@104: public String getString(String name, String defaultValue) { franta-hg@104: Property p = findProperty(name); franta-hg@104: return p == null ? defaultValue : p.getValue(); franta-hg@104: } franta-hg@104: franta-hg@104: public boolean getBoolean(String name, boolean defaultValue) { franta-hg@104: Property p = findProperty(name); franta-hg@104: return p == null ? defaultValue : Boolean.valueOf(p.getValue()); franta-hg@104: } franta-hg@104: franta-hg@104: public int getInteger(String name, int defaultValue) { franta-hg@104: Property p = findProperty(name); franta-hg@104: return p == null ? defaultValue : Integer.valueOf(p.getValue()); franta-hg@104: } franta-hg@105: franta-hg@108: public boolean hasProperty(String name) { franta-hg@108: return findByName(this, name) != null; franta-hg@108: } franta-hg@108: franta-hg@105: @Override franta-hg@105: public Properties clone() { franta-hg@105: Properties clone = new Properties(size()); franta-hg@105: Collections.copy(clone, this); franta-hg@105: return clone; franta-hg@105: } franta-hg@106: franta-hg@106: /** franta-hg@106: * @return merged this and backing defaults as Java Properties franta-hg@106: */ franta-hg@106: public java.util.Properties getJavaProperties() { franta-hg@106: java.util.Properties javaProperties = new java.util.Properties(); franta-hg@106: duplicateTo(javaProperties); franta-hg@106: return javaProperties; franta-hg@106: } franta-hg@106: franta-hg@106: private void duplicateTo(java.util.Properties javaProperties) { franta-hg@107: if (defaults != null) { franta-hg@107: defaults.duplicateTo(javaProperties); franta-hg@107: } franta-hg@106: for (Property p : this) { franta-hg@191: String value = p.getValue(); franta-hg@191: if (value != null) { franta-hg@191: javaProperties.setProperty(p.getName(), value); franta-hg@191: } franta-hg@106: } franta-hg@106: } franta-hg@104: }