java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 31 Dec 2013 17:35:33 +0100
branchv_0
changeset 104 245f1b88a3e6
child 105 39d8b427e20f
permissions -rw-r--r--
formatter/database properties
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk.configuration;
    19 
    20 import java.util.ArrayList;
    21 import javax.xml.bind.annotation.XmlTransient;
    22 import static info.globalcode.sql.dk.Functions.findByName;
    23 
    24 /**
    25  * <p>List of configurables.</p>
    26  *
    27  * <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
    28  * look
    29  * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
    30  * in default properties.</p>
    31  *
    32  * <p>Typical use: </p>
    33  * <ul>
    34  * <li>this instance – ad-hoc properties from CLI options</li>
    35  * <li>default properties – from config file</li>
    36  * <li>defaultValue – hardcoded default</li>
    37  * </ul>
    38  *
    39  * @author Ing. František Kučera (frantovo.cz)
    40  */
    41 public class Properties extends ArrayList<Property> {
    42 
    43 	private Properties defaults;
    44 
    45 	@XmlTransient
    46 	public Properties getDefaults() {
    47 		return defaults;
    48 	}
    49 
    50 	public void setDefaults(Properties defaults) {
    51 		this.defaults = defaults;
    52 	}
    53 
    54 	private Property findProperty(String name) {
    55 		Property p = findByName(this, name);
    56 		if (p == null) {
    57 			p = findByName(defaults, name);
    58 		}
    59 		return p;
    60 	}
    61 
    62 	public String getString(String name, String defaultValue) {
    63 		Property p = findProperty(name);
    64 		return p == null ? defaultValue : p.getValue();
    65 	}
    66 
    67 	public boolean getBoolean(String name, boolean defaultValue) {
    68 		Property p = findProperty(name);
    69 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    70 	}
    71 
    72 	public int getInteger(String name, int defaultValue) {
    73 		Property p = findProperty(name);
    74 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    75 	}
    76 }