java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 10 Jan 2014 23:21:28 +0100
branchv_0
changeset 155 eb3676c6929b
parent 133 a1bf1465df89
child 191 862d0a8747ac
permissions -rw-r--r--
more JavaDoc
     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 into defaults. Methods also accept defaultValue parameter – is used if property is nof found
    30  * even 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> implements Cloneable {
    42 
    43 	private Properties defaults;
    44 
    45 	public Properties() {
    46 	}
    47 
    48 	public Properties(int initialCapacity) {
    49 		super(initialCapacity);
    50 	}
    51 
    52 	@XmlTransient
    53 	public Properties getDefaults() {
    54 		return defaults;
    55 	}
    56 
    57 	public void setDefaults(Properties defaults) {
    58 		this.defaults = defaults;
    59 	}
    60 
    61 	/**
    62 	 * @param defaults the last/deepest defaults
    63 	 */
    64 	public void setLastDefaults(Properties defaults) {
    65 		if (this.defaults == null) {
    66 			this.defaults = defaults;
    67 		} else {
    68 			this.defaults.setLastDefaults(defaults);
    69 		}
    70 	}
    71 
    72 	private Property findProperty(String name) {
    73 		Property p = findByName(this, name);
    74 		if (p == null && defaults != null) {
    75 			p = defaults.findProperty(name);
    76 		}
    77 		return p;
    78 	}
    79 
    80 	public String getString(String name, String defaultValue) {
    81 		Property p = findProperty(name);
    82 		return p == null ? defaultValue : p.getValue();
    83 	}
    84 
    85 	public boolean getBoolean(String name, boolean defaultValue) {
    86 		Property p = findProperty(name);
    87 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    88 	}
    89 
    90 	public int getInteger(String name, int defaultValue) {
    91 		Property p = findProperty(name);
    92 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    93 	}
    94 
    95 	public boolean hasProperty(String name) {
    96 		return findByName(this, name) != null;
    97 	}
    98 
    99 	@Override
   100 	public Properties clone() {
   101 		Properties clone = new Properties(size());
   102 		Collections.copy(clone, this);
   103 		return clone;
   104 	}
   105 
   106 	/**
   107 	 * @return merged this and backing defaults as Java Properties
   108 	 */
   109 	public java.util.Properties getJavaProperties() {
   110 		java.util.Properties javaProperties = new java.util.Properties();
   111 		duplicateTo(javaProperties);
   112 		return javaProperties;
   113 	}
   114 
   115 	private void duplicateTo(java.util.Properties javaProperties) {
   116 		if (defaults != null) {
   117 			defaults.duplicateTo(javaProperties);
   118 		}
   119 		for (Property p : this) {
   120 			javaProperties.setProperty(p.getName(), p.getValue());
   121 		}
   122 	}
   123 }