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