diff -r 7e08730da258 -r 4a1864c3e867 java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java Mon Mar 04 20:15:24 2019 +0100 @@ -0,0 +1,129 @@ +/** + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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 . + */ +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; + +/** + *

+ * List of configurables.

+ * + *

+ * 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.

+ * + *

+ * Typical use:

+ * + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Properties extends ArrayList 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); + } + } + } +}