/**
* 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 <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;
}
private Property findProperty(String name) {
Property p = findByName(this, name);
if (p == null) {
p = findByName(defaults, 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());
}
@Override
public Properties clone() {
Properties clone = new Properties(size());
Collections.copy(clone, this);
return clone;
}
}