public class JProperties
extends java.util.Hashtable<K,V>
Properties
manage properties file in UTF-8 encoding.
Its save updateAndSave(File)
methods keep layout of previous properties file.Modifier and Type | Field and Description |
---|---|
protected java.util.Set<JPropertiesListener> |
listenerSet |
protected static int |
PREFIX_PROPERTIES_CACHE_MAX_ENTRIES |
protected java.util.Map<java.lang.String,JProperties> |
prefixPropertiesCache |
protected boolean |
propertyNameAsDefaultValue |
static java.lang.String |
REVISION |
protected java.util.Set<java.lang.String> |
SENSITIVE_KEYWORD |
Constructor and Description |
---|
JProperties() |
Modifier and Type | Method and Description |
---|---|
void |
addProperties(JProperties prop)
Add every property of the given
JProperties to
this JProperties . |
void |
addProperties(java.util.Properties prop)
Add every property of the given
Properties to
this JProperties . |
void |
addPropertiesListener(JPropertiesListener l)
Adds a listener for any change of this JProperties.
|
protected static java.lang.String[] |
buildArrayFromString(java.lang.String value,
java.lang.String[] defaultValue)
Convert a string to a String[] using the following formats:
"@{delim}value1{delim}value2...", where
{delim} is one character delimiter of your choice.
"value1{delim}value2{delim}...", where {delim} is a character from \t\n\r\f .
|
protected JProperties |
checkCacheGetProperties(java.lang.String prefix) |
void |
clearProperties()
Clear all properties.
|
boolean |
containsProperty(java.lang.String name)
Tests if this JProperties contains the given property key/name.
|
void |
firePropertiesChange()
Triggers a propertie change to notify all listener of this JProperties.
|
boolean |
getBooleanProperty(java.lang.String name,
boolean defaultValue)
Gets the value of a property as a boolean given its key/name.
|
double |
getDoubleProperty(java.lang.String name,
double defaultValue)
Gets the value of a property as a double value given its key/name.
|
float |
getFloatProperty(java.lang.String name,
float defaultValue)
Gets the value of a property as a float value given its key/name.
|
int[] |
getIntArrayProperty(java.lang.String name,
int[] defaultValue)
Gets the value of a property as a int array.
|
int |
getIntegerProperty(java.lang.String name,
int defaultValue)
Gets the value of a property as an int value given its key/name.
|
protected java.util.Set<JPropertiesListener> |
getListenerSet()
Retrieve set of all listeners for this JProperties.
|
long |
getLongProperty(java.lang.String name,
long defaultValue)
Gets the value of a property as a long value given its key/name.
|
JProperties |
getProperties(java.lang.String prefix)
Returns a new property set of properties with key matching a
given prefix.
|
java.lang.String |
getProperty(java.lang.String name)
Gets value of a property.
|
java.lang.String |
getProperty(java.lang.String name,
java.lang.String defaultValue)
Gets the value of a property given its key/name.
|
java.lang.String[] |
getStringArrayProperty(java.lang.String name,
java.lang.String[] defaultValue)
Gets the value of a property as a String array.
|
java.util.Map<java.lang.String,java.lang.Object> |
getSubProperties(java.lang.String prefix)
Returns a map of properties built from those properties and having a given prefix.
Example: prop: foo.bar.case1.x: value of x for case1 foo.bar.case1.y: value of y for case1 foo.bar.case2.x: value of x for case2 foo.bar.case2.y: value of y for case2 foo.bar.case2.z.t: value of z for case2 |
void |
invalidateCacheGetProperties()
Invalidate the cache used for
getProperties(String) method. |
void |
load(java.io.File file)
Reads a property file (key and element pairs) from the given File.
|
void |
load(java.io.File file,
boolean unprotectSensitiveData)
Reads a property file (key and element pairs) from the given File.
|
void |
load(java.io.File file,
java.lang.String prefix)
Reads a property file (key and element pairs) from the given File.
|
void |
load(java.io.File file,
java.lang.String prefix,
boolean unprotectSensitiveData)
Reads a property file (key and element pairs) from the given File.
|
V |
put(K key,
V value) |
protected void |
putCacheGetProperties(java.lang.String prefix,
JProperties prop) |
void |
removePropertiesListener(JPropertiesListener l)
Removes a listener for any change of this JProperties.
|
java.lang.Object |
setProperty(java.lang.String name,
java.lang.String value)
Sets value of a property.
|
void |
updateAndSave(java.io.File file)
Updates and save properties.
|
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, size, toString, values
public static final java.lang.String REVISION
protected static final int PREFIX_PROPERTIES_CACHE_MAX_ENTRIES
protected java.util.Map<java.lang.String,JProperties> prefixPropertiesCache
protected boolean propertyNameAsDefaultValue
protected java.util.Set<JPropertiesListener> listenerSet
protected java.util.Set<java.lang.String> SENSITIVE_KEYWORD
protected java.util.Set<JPropertiesListener> getListenerSet()
addPropertiesListener(JPropertiesListener)
,
removePropertiesListener(JPropertiesListener)
public void addPropertiesListener(JPropertiesListener l)
l
- the listener to addremovePropertiesListener(JPropertiesListener)
public void removePropertiesListener(JPropertiesListener l)
l
- the listener to removeaddPropertiesListener(JPropertiesListener)
public void firePropertiesChange()
public boolean containsProperty(java.lang.String name)
name
- the key/name of the property to checkpublic java.lang.String getProperty(java.lang.String name)
name
- the key/name of the property to retrievepublic java.lang.String getProperty(java.lang.String name, java.lang.String defaultValue)
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value is null.public java.lang.Object setProperty(java.lang.String name, java.lang.String value)
name
- the key/name of the property to retrievevalue
- the new value to assign.public boolean getBooleanProperty(java.lang.String name, boolean defaultValue)
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or is neither "false" nor "true" (case sensitive).public int getIntegerProperty(java.lang.String name, int defaultValue)
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or if Integer.parseInt() throws exception during String
to int conversion.public long getLongProperty(java.lang.String name, long defaultValue)
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or if Long.parseLong() throws exception during String
to long conversion.public float getFloatProperty(java.lang.String name, float defaultValue)
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or if Float.parseFloat() throws exception during String
to float conversion.public double getDoubleProperty(java.lang.String name, double defaultValue)
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or if Double.parseDouble() throws exception during String
to double conversion.public java.lang.String[] getStringArrayProperty(java.lang.String name, java.lang.String[] defaultValue)
The property value must be in one of the following forms:
{delim}
is one character delimiter of your choice.{delim}
is a character from \t\n\r\f
.String[] array = prop.getStringArrayProperty("missingexample", new String[] { "default", "value"}); assertEquals(2, array.length); assertEquals("default", array[0]); assertEquals("value", array[1]); prop.setProperty("example1", "@|abc|def"); String[] array1 = prop.getStringArrayProperty("example1", new String[] { }); assertEquals(2, array1.length); assertEquals("abc", array1[0]); assertEquals("def", array1[1]); prop.setProperty("example2", "foo bar\tzig"); String[] array2 = prop.getStringArrayProperty("example2", new String[] { }); assertEquals(3, array2.length); assertEquals("foo", array2[0]); assertEquals("bar", array2[1]); assertEquals("zig", array2[2]);
name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or no values could be found in the conversion.protected static java.lang.String[] buildArrayFromString(java.lang.String value, java.lang.String[] defaultValue)
{delim}
is one character delimiter of your choice.{delim}
is a character from \t\n\r\f
.value
- the valuedefaultValue
- the default valuepublic int[] getIntArrayProperty(java.lang.String name, int[] defaultValue)
getStringArrayProperty(String, String[])
.name
- the key/name of the property to retrievedefaultValue
- the default value to use if the existing value
is null or no values could be found in the conversion.getStringArrayProperty(String, String[])
public void addProperties(java.util.Properties prop)
Properties
to
this JProperties
.prop
- the properties to be addedpublic void addProperties(JProperties prop)
JProperties
to
this JProperties
.prop
- the jproperties to be addedprotected JProperties checkCacheGetProperties(java.lang.String prefix)
protected void putCacheGetProperties(java.lang.String prefix, JProperties prop)
public void invalidateCacheGetProperties()
getProperties(String)
method.public JProperties getProperties(java.lang.String prefix)
prefix
- the prefix the properties must match to be added to the
returned JProperties instance, if null, all properties are added.public java.util.Map<java.lang.String,java.lang.Object> getSubProperties(java.lang.String prefix)
foo.bar.case1.x: value of x for case1
foo.bar.case1.y: value of y for case1
foo.bar.case2.x: value of x for case2
foo.bar.case2.y: value of y for case2
foo.bar.case2.z.t: value of z for case2
prop.getSubProperties("foo.bar.")
returns a Map with the keys case1
and case2
.
A JProperties is bound to case1
and it contains the properties:
x: value of x for case1
y: value of y for case1
A JProperties is bound to case2
and it contains the properties:
x: value of x for case2
y: value of y for case2
z.t: value of z for case2
prefix
- the prefixpublic void clearProperties()
public void load(java.io.File file) throws java.io.IOException
file
- the file from which to read properties.java.io.IOException
- if an error occurred when reading from the input
stream.java.lang.IllegalArgumentException
- if the input stream contains a malformed
Unicode escape sequence.Properties.load(java.io.InputStream)
public void load(java.io.File file, boolean unprotectSensitiveData) throws java.io.IOException
file
- the file from which to read properties.unprotectSensitiveData
- true if sensitive data should be unprotected after readjava.io.IOException
- if an error occurred when reading from the input
stream.java.lang.IllegalArgumentException
- if the input stream contains a malformed
Unicode escape sequence.Properties.load(java.io.InputStream)
public void load(java.io.File file, java.lang.String prefix) throws java.io.IOException
file
- the file from which to read properties.prefix
- a prefix to use add to all properties' name.
if null, no prefix is used.java.io.IOException
- if an error occurred when reading from the input
stream.java.lang.IllegalArgumentException
- if the input stream contains a malformed
Unicode escape sequence.Properties.load(java.io.InputStream)
public void load(java.io.File file, java.lang.String prefix, boolean unprotectSensitiveData) throws java.io.IOException
file
- the file from which to read properties.prefix
- a prefix to use add to all properties' name.
if null, no prefix is used.unprotectSensitiveData
- true if sensitive data should be unprotected after readjava.io.IOException
- if an error occurred when reading from the input
stream.java.lang.IllegalArgumentException
- if the input stream contains a malformed
Unicode escape sequence.Properties.load(java.io.InputStream)
public void updateAndSave(java.io.File file) throws java.io.IOException
file
- the properties File to read and update.java.io.IOException
- IOException thrown in methodpublic V put(K key, V value)
put
in interface java.util.Map<K,V>
put
in class java.util.Hashtable<K,V>
Copyright © 2001-2017 Jalios SA. All Rights Reserved.