java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Jan 2014 21:21:04 +0100
branchv_0
changeset 133 a1bf1465df89
parent 108 d06d90b28217
child 155 eb3676c6929b
permissions -rw-r--r--
Properties: setLastDefaults()
     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 	/**
    63 	 * @param defaults the last/deepest defaults
    64 	 */
    65 	public void setLastDefaults(Properties defaults) {
    66 		if (this.defaults == null) {
    67 			this.defaults = defaults;
    68 		} else {
    69 			this.defaults.setLastDefaults(defaults);
    70 		}
    71 	}
    72 
    73 	private Property findProperty(String name) {
    74 		Property p = findByName(this, name);
    75 		if (p == null && defaults != null) {
    76 			p = defaults.findProperty(name);
    77 		}
    78 		return p;
    79 	}
    80 
    81 	public String getString(String name, String defaultValue) {
    82 		Property p = findProperty(name);
    83 		return p == null ? defaultValue : p.getValue();
    84 	}
    85 
    86 	public boolean getBoolean(String name, boolean defaultValue) {
    87 		Property p = findProperty(name);
    88 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    89 	}
    90 
    91 	public int getInteger(String name, int defaultValue) {
    92 		Property p = findProperty(name);
    93 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    94 	}
    95 
    96 	public boolean hasProperty(String name) {
    97 		return findByName(this, name) != null;
    98 	}
    99 
   100 	@Override
   101 	public Properties clone() {
   102 		Properties clone = new Properties(size());
   103 		Collections.copy(clone, this);
   104 		return clone;
   105 	}
   106 
   107 	/**
   108 	 * @return merged this and backing defaults as Java Properties
   109 	 */
   110 	public java.util.Properties getJavaProperties() {
   111 		java.util.Properties javaProperties = new java.util.Properties();
   112 		duplicateTo(javaProperties);
   113 		return javaProperties;
   114 	}
   115 
   116 	private void duplicateTo(java.util.Properties javaProperties) {
   117 		if (defaults != null) {
   118 			defaults.duplicateTo(javaProperties);
   119 		}
   120 		for (Property p : this) {
   121 			javaProperties.setProperty(p.getName(), p.getValue());
   122 		}
   123 	}
   124 }