java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 31 Dec 2013 18:11:50 +0100
branchv_0
changeset 105 39d8b427e20f
parent 104 245f1b88a3e6
child 106 e9c3583580c8
permissions -rw-r--r--
cloneable property and 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 import java.util.Collections;
    24 
    25 /**
    26  * <p>List of configurables.</p>
    27  *
    28  * <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
    29  * look
    30  * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
    31  * in default properties.</p>
    32  *
    33  * <p>Typical use: </p>
    34  * <ul>
    35  * <li>this instance – ad-hoc properties from CLI options</li>
    36  * <li>default properties – from config file</li>
    37  * <li>defaultValue – hardcoded default</li>
    38  * </ul>
    39  *
    40  * @author Ing. František Kučera (frantovo.cz)
    41  */
    42 public class Properties extends ArrayList<Property> implements Cloneable {
    43 
    44 	private Properties defaults;
    45 
    46 	public Properties() {
    47 	}
    48 
    49 	public Properties(int initialCapacity) {
    50 		super(initialCapacity);
    51 	}
    52 
    53 	@XmlTransient
    54 	public Properties getDefaults() {
    55 		return defaults;
    56 	}
    57 
    58 	public void setDefaults(Properties defaults) {
    59 		this.defaults = defaults;
    60 	}
    61 
    62 	private Property findProperty(String name) {
    63 		Property p = findByName(this, name);
    64 		if (p == null) {
    65 			p = findByName(defaults, name);
    66 		}
    67 		return p;
    68 	}
    69 
    70 	public String getString(String name, String defaultValue) {
    71 		Property p = findProperty(name);
    72 		return p == null ? defaultValue : p.getValue();
    73 	}
    74 
    75 	public boolean getBoolean(String name, boolean defaultValue) {
    76 		Property p = findProperty(name);
    77 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    78 	}
    79 
    80 	public int getInteger(String name, int defaultValue) {
    81 		Property p = findProperty(name);
    82 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    83 	}
    84 
    85 	@Override
    86 	public Properties clone() {
    87 		Properties clone = new Properties(size());
    88 		Collections.copy(clone, this);
    89 		return clone;
    90 	}
    91 }