java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 04 Feb 2024 16:10:37 +0100
branchv_0
changeset 255 099bb96f8d8d
parent 250 aae5009bd0af
permissions -rw-r--r--
tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
     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 import java.util.regex.Pattern;
    24 
    25 /**
    26  * <p>
    27  * List of configurables.</p>
    28  *
    29  * <p>
    30  * Can be backed by defaults – if value for given name is nof found in this
    31  * instance, we will look into defaults. Methods also accept defaultValue
    32  * parameter – is used if property is nof found even in default properties.</p>
    33  *
    34  * <p>
    35  * Typical use: </p>
    36  * <ul>
    37  * <li>this instance – ad-hoc properties from CLI options</li>
    38  * <li>default properties – from config file</li>
    39  * <li>defaultValue – hardcoded default</li>
    40  * </ul>
    41  *
    42  * @author Ing. František Kučera (frantovo.cz)
    43  */
    44 public class Properties extends ArrayList<Property> implements Cloneable {
    45 
    46 	private Properties defaults;
    47 
    48 	public Properties() {
    49 	}
    50 
    51 	public Properties(int initialCapacity) {
    52 		super(initialCapacity);
    53 	}
    54 
    55 	@XmlTransient
    56 	public Properties getDefaults() {
    57 		return defaults;
    58 	}
    59 
    60 	public void setDefaults(Properties defaults) {
    61 		this.defaults = defaults;
    62 	}
    63 
    64 	/**
    65 	 * @param defaults the last/deepest defaults
    66 	 */
    67 	public void setLastDefaults(Properties defaults) {
    68 		if (this.defaults == null) {
    69 			this.defaults = defaults;
    70 		} else {
    71 			this.defaults.setLastDefaults(defaults);
    72 		}
    73 	}
    74 
    75 	private Property findProperty(String name) {
    76 		Property p = findByName(this, name);
    77 		if (p == null && defaults != null) {
    78 			p = defaults.findProperty(name);
    79 		}
    80 		return p;
    81 	}
    82 
    83 	public String getString(String name, String defaultValue) {
    84 		Property p = findProperty(name);
    85 		return p == null ? defaultValue : p.getValue();
    86 	}
    87 
    88 	public boolean getBoolean(String name, boolean defaultValue) {
    89 		Property p = findProperty(name);
    90 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    91 	}
    92 
    93 	public int getInteger(String name, int defaultValue) {
    94 		Property p = findProperty(name);
    95 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    96 	}
    97 
    98 	public Pattern getPattern(String name, Pattern defaultValue) {
    99 		Property p = findProperty(name);
   100 		return p == null ? defaultValue : Pattern.compile(p.getValue());
   101 	}
   102 
   103 	public boolean hasProperty(String name) {
   104 		return findByName(this, name) != null;
   105 	}
   106 
   107 	@Override
   108 	public Properties clone() {
   109 		Properties clone = new Properties(size());
   110 		Collections.copy(clone, this);
   111 		return clone;
   112 	}
   113 
   114 	/**
   115 	 * @return merged this and backing defaults as Java Properties
   116 	 */
   117 	public java.util.Properties getJavaProperties() {
   118 		java.util.Properties javaProperties = new java.util.Properties();
   119 		duplicateTo(javaProperties);
   120 		return javaProperties;
   121 	}
   122 
   123 	private void duplicateTo(java.util.Properties javaProperties) {
   124 		if (defaults != null) {
   125 			defaults.duplicateTo(javaProperties);
   126 		}
   127 		for (Property p : this) {
   128 			String value = p.getValue();
   129 			if (value != null) {
   130 				javaProperties.setProperty(p.getName(), value);
   131 			}
   132 		}
   133 	}
   134 }