public final class PropertiesUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
PropertiesUtils.InvalidPropertyException |
Constructor and Description |
---|
PropertiesUtils() |
Modifier and Type | Method and Description |
---|---|
static Properties |
clone(Properties props)
Performs a correct deep clone of the properties object by capturing
all properties in the default(s) and placing them directly into the
new Properties object.
|
static Properties |
clone(Properties props,
Properties defaults,
boolean deepClone)
Performs a correct deep clone of the properties object by capturing
all properties in the default(s) and placing them directly into the
new Properties object.
|
static byte[] |
fromHex(String hex)
Return the bytes for a given hex string, or throw an
IllegalArgumentException |
static boolean |
getBooleanProperty(Properties props,
String propName,
boolean defaultValue) |
static Properties |
getDefaultProperties() |
static double |
getDoubleProperty(Properties props,
String propName,
double defaultValue) |
static float |
getFloatProperty(Properties props,
String propName,
float defaultValue) |
static String |
getHierarchicalProperty(String key,
String defaultValue)
Search for the property first in the environment, then in the system properties
|
static <T> T |
getHierarchicalProperty(String key,
T defaultValue,
Class<T> clazz)
Search for the property first in the environment, then in the system properties
|
static int |
getIntProperty(Properties props,
String propName,
int defaultValue) |
static long |
getLongProperty(Properties props,
String propName,
long defaultValue) |
static Properties |
load(String fileName) |
static void |
putAll(Properties addToThis,
Properties withThese)
This method is intended to replace the use of the
putAll
method of Properties inherited from java.util.Hashtable . |
static void |
setBeanProperties(Object bean,
Properties props,
String prefix) |
static void |
setBeanProperties(Object bean,
Properties props,
String prefix,
boolean includeEnv) |
static void |
setBeanProperty(Object bean,
String name,
Object value) |
static String |
toHex(byte[] bytes) |
static char |
toHex(int nibble)
Convert a nibble to a hex character
|
static void |
toHex(StringBuilder sb,
InputStream is) |
public static Properties clone(Properties props)
public static Properties clone(Properties props, Properties defaults, boolean deepClone)
public static void putAll(Properties addToThis, Properties withThese)
This method is intended to replace the use of the putAll
method of Properties
inherited from java.util.Hashtable
.
The problem with that method is that, since it is inherited from
Hashtable
, default properties are lost.
For example, the following code
Properties a;
Properties b;
//initialize ...
a.putAll(b);
will fail if b
had been constructed with a default
Properties
object. Those defaults would be lost and
not added to a
.
The above code could be correctly performed with this method, like this:
Properties a;
Properties b;
//initialize ...
PropertiesUtils.putAll(a,b);
In the above example, a
is modified - properties are added to
it (note that if a
has defaults they will remain unaffected.)
The properties from b
, including defaults, will be
added to a
using its setProperty
method -
these new properties will overwrite any pre-existing ones of the same
name.addToThis
- This Properties object is modified; the properties
of the other parameter are added to this. The added property values
will replace any current property values of the same names.withThese
- The properties (including defaults) of this
object are added to the "addToThis" parameter.public static int getIntProperty(Properties props, String propName, int defaultValue) throws PropertiesUtils.InvalidPropertyException
public static long getLongProperty(Properties props, String propName, long defaultValue)
public static float getFloatProperty(Properties props, String propName, float defaultValue)
public static double getDoubleProperty(Properties props, String propName, double defaultValue)
public static boolean getBooleanProperty(Properties props, String propName, boolean defaultValue)
public static Properties load(String fileName) throws IOException
IOException
public static char toHex(int nibble)
nibble
- the nibble to convert.public static String toHex(byte[] bytes)
public static byte[] fromHex(String hex)
IllegalArgumentException
hex
- public static void toHex(StringBuilder sb, InputStream is) throws IOException
IOException
public static void setBeanProperties(Object bean, Properties props, String prefix)
public static void setBeanProperties(Object bean, Properties props, String prefix, boolean includeEnv)
public static String getHierarchicalProperty(String key, String defaultValue)
key
- defaultValue
- public static <T> T getHierarchicalProperty(String key, T defaultValue, Class<T> clazz)
key
- defaultValue
- clazz
- public static Properties getDefaultProperties()
Copyright © 2020. All rights reserved.