java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
child 255 099bb96f8d8d
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package info.globalcode.sql.dk.configuration;
    18 
    19 import java.util.ArrayList;
    20 import javax.xml.bind.annotation.XmlTransient;
    21 import static info.globalcode.sql.dk.Functions.findByName;
    22 import java.util.Collections;
    23 
    24 /**
    25  * <p>
    26  * List of configurables.</p>
    27  *
    28  * <p>
    29  * Can be backed by defaults – if value for given name is nof found in this instance, we will
    30  * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
    31  * even in default properties.</p>
    32  *
    33  * <p>
    34  * Typical use: </p>
    35  * <ul>
    36  * <li>this instance – ad-hoc properties from CLI options</li>
    37  * <li>default properties – from config file</li>
    38  * <li>defaultValue – hardcoded default</li>
    39  * </ul>
    40  *
    41  * @author Ing. František Kučera (frantovo.cz)
    42  */
    43 public class Properties extends ArrayList<Property> implements Cloneable {
    44 
    45 	private Properties defaults;
    46 
    47 	public Properties() {
    48 	}
    49 
    50 	public Properties(int initialCapacity) {
    51 		super(initialCapacity);
    52 	}
    53 
    54 	@XmlTransient
    55 	public Properties getDefaults() {
    56 		return defaults;
    57 	}
    58 
    59 	public void setDefaults(Properties defaults) {
    60 		this.defaults = defaults;
    61 	}
    62 
    63 	/**
    64 	 * @param defaults the last/deepest defaults
    65 	 */
    66 	public void setLastDefaults(Properties defaults) {
    67 		if (this.defaults == null) {
    68 			this.defaults = defaults;
    69 		} else {
    70 			this.defaults.setLastDefaults(defaults);
    71 		}
    72 	}
    73 
    74 	private Property findProperty(String name) {
    75 		Property p = findByName(this, name);
    76 		if (p == null && defaults != null) {
    77 			p = defaults.findProperty(name);
    78 		}
    79 		return p;
    80 	}
    81 
    82 	public String getString(String name, String defaultValue) {
    83 		Property p = findProperty(name);
    84 		return p == null ? defaultValue : p.getValue();
    85 	}
    86 
    87 	public boolean getBoolean(String name, boolean defaultValue) {
    88 		Property p = findProperty(name);
    89 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    90 	}
    91 
    92 	public int getInteger(String name, int defaultValue) {
    93 		Property p = findProperty(name);
    94 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    95 	}
    96 
    97 	public boolean hasProperty(String name) {
    98 		return findByName(this, name) != null;
    99 	}
   100 
   101 	@Override
   102 	public Properties clone() {
   103 		Properties clone = new Properties(size());
   104 		Collections.copy(clone, this);
   105 		return clone;
   106 	}
   107 
   108 	/**
   109 	 * @return merged this and backing defaults as Java Properties
   110 	 */
   111 	public java.util.Properties getJavaProperties() {
   112 		java.util.Properties javaProperties = new java.util.Properties();
   113 		duplicateTo(javaProperties);
   114 		return javaProperties;
   115 	}
   116 
   117 	private void duplicateTo(java.util.Properties javaProperties) {
   118 		if (defaults != null) {
   119 			defaults.duplicateTo(javaProperties);
   120 		}
   121 		for (Property p : this) {
   122 			String value = p.getValue();
   123 			if (value != null) {
   124 				javaProperties.setProperty(p.getName(), value);
   125 			}
   126 		}
   127 	}
   128 }