public final class GlobalProperties
extends java.lang.Object
This class offers a global store for different string properties.
If some class MyClass needs a global flag or control parameter that
acts on the class behaviour, we offer to use the following idiom:
public static final MY_CONTROL_PARAMETER = GlobalProperties.getClassProperty(
MyClass.class,
"MY_CONTROL_PARAMETER",
(default value));
(or analogous). Then the behaviour of your class, controlled by
this parameter, will be guaranteed unchanged since the moment of the class
initialization. Simultaneously, you are able to initialize this
control parameter in your application or applet before the first access
to that class by GlobalProperties.setProperty(String, String)
method.
GlobalProperties class does not depend, directly or
indirectly, on any other classes excepting standard Java API.
Unlike System.getProperty call, the methods of this class
never throw SecurityException. It makes GlobalProperties
class more suitable for creating service Java libraries.
By default, the global store supported by GlobalProperties
contains the following 3 properties:
"line.separator" (also stored in LINE_SEPARATOR class constant),
"file.separator" (also stored in FILE_SEPARATOR class constant),
"path.separator" (also stored in PATH_SEPARATOR class constant).
GlobalProperties
on the base of standard Java Runtime libraries, without any risk to
meet with SecurityException.
For Java applications, initializing GlobalProperties
by standard system properties can be a good idea:
GlobalProperties.setProperties(System.getProperties());
(In applets, System.getProperties() throws
SecurityException usually.)
| Modifier and Type | Field and Description |
|---|---|
static char |
FILE_SEPARATOR
Equivalent of the standard
File.separatorChar constant:
the system-dependent default name-separator character. |
static java.lang.String |
JAVA_NATIVE_ENABLED_PROPERTY_NAME
The name of the global boolean property that is recommended to be used for
"disabling native code", i.e.
|
static java.lang.String |
LINE_SEPARATOR
The standard sequence of characters that separates lines in text files and
console output.
|
static char |
PATH_SEPARATOR
Equivalent of the standard
File.pathSeparatorChar constant:
the system-dependent path-separator character. |
| Modifier and Type | Method and Description |
|---|---|
static void |
fix()
Fixes the global store: any modifications become impossible after the first call of this method.
|
static java.lang.Boolean |
getBooleanProperty(java.lang.String key)
Returns the global boolean property with the given name
key. |
static boolean |
getBooleanProperty(java.lang.String key,
boolean defaultValue)
Returns the global boolean property with the given name
key. |
static java.lang.Boolean |
getClassBooleanProperty(java.lang.Class clazz,
java.lang.String key)
Equivalent to
call. |
static boolean |
getClassBooleanProperty(java.lang.Class clazz,
java.lang.String key,
boolean defaultValue)
Equivalent to
call. |
static int |
getClassDebugLevel(java.lang.Class clazz)
Equivalent to
. |
static java.lang.Integer |
getClassIntProperty(java.lang.Class clazz,
java.lang.String key)
Equivalent to
call. |
static int |
getClassIntProperty(java.lang.Class clazz,
java.lang.String key,
int defaultValue)
Equivalent to
call. |
static java.lang.Long |
getClassLongProperty(java.lang.Class clazz,
java.lang.String key)
Equivalent to
call. |
static long |
getClassLongProperty(java.lang.Class clazz,
java.lang.String key,
long defaultValue)
Equivalent to
call. |
static java.lang.String |
getClassProperty(java.lang.Class clazz,
java.lang.String key)
Equivalent to
call. |
static java.lang.String |
getClassProperty(java.lang.Class clazz,
java.lang.String key,
java.lang.String defaultValue)
Equivalent to
call. |
static java.lang.String |
getClassPropertyName(java.lang.Class clazz,
java.lang.String key)
Returns the name for a property oriented to the given class.
|
static java.lang.Integer |
getIntProperty(java.lang.String key)
Returns the global integer property with the given name
key. |
static int |
getIntProperty(java.lang.String key,
int defaultValue)
Returns the global integer property with the given name
key. |
static java.lang.Long |
getLongProperty(java.lang.String key)
Returns the global long integer property with the given name
key. |
static long |
getLongProperty(java.lang.String key,
long defaultValue)
Returns the global long integer property with the given name
key. |
static java.util.Properties |
getProperties()
Returns all properties stored in the global store represented by
GlobalProperties class. |
static java.lang.String |
getProperty(java.lang.String key)
Returns the global property with the given name
key. |
static java.lang.String |
getProperty(java.lang.String key,
java.lang.String defaultValue)
Returns the global property with the given name
key,
or defaultValue if there is no such a property. |
static java.lang.String |
removeProperty(java.lang.String key)
Removes the global property with the given name
key. |
static void |
setBooleanProperty(java.lang.String key,
boolean value)
Equivalent to
setProperty(key, String.valueOf(value)) call. |
static void |
setIntProperty(java.lang.String key,
int value)
Equivalent to
setProperty(key, String.valueOf(value)) call. |
static void |
setLongProperty(java.lang.String key,
long value)
Equivalent to
setProperty(key, String.valueOf(value)) call. |
static void |
setProperties(java.util.Properties properties)
Adds a collection of string properties to the global store.
|
static java.lang.String |
setProperty(java.lang.String key,
java.lang.String value)
Sets the global string property with the given name
key. |
public static final java.lang.String LINE_SEPARATOR
"\n" on Unix-like systems,
"\r\n" on DOS/Windows family, "\r" on Macintosh.
Standard System.out.println() call is equivalent to
System.out.print(LINE_SEPARATOR).
If you want to print several lines on the console by a single call
of System.out.println, you must always use
LINE_SEPARATOR to separate lines instead more simple
"\n". For example, the call
System.out.println("First line\nSecond line");
is incorrect (unlike an analogous call in C/C++): it will not properly
work under Macintosh. The following call should be called instead:
System.out.println("First line" + GlobalProperties.LINE_SEPARATOR + "Second line");
You can also use the following more simple call that is also correct:
Out.println("First line\nSecond line")
LINE_SEPARATOR is also recommended to be used while
creating text files that should be then used by other software installed
on the same operating system.
Out.print(String),
Out.println(String)public static final char FILE_SEPARATOR
File.separatorChar constant:
the system-dependent default name-separator character. On UNIX systems
the value of this constant is '/'; on Microsoft Windows systems
it is '\'.public static final char PATH_SEPARATOR
File.pathSeparatorChar constant:
the system-dependent path-separator character. This character is used to
separate filenames in a sequence of files given as a path list.
On UNIX systems, this character is ':'; on Microsoft Windows systems
it is ';'.public static final java.lang.String JAVA_NATIVE_ENABLED_PROPERTY_NAME
loadLibrary
or load methods of System and Runtime
standard classes.
More precisely, all classes in net.algart.* package, which use
native-code for optimization or other goals, never try to load external native
libraries if
GlobalProperties.getBooleanProperty(
GlobalProperties.JAVA_NATIVE_ENABLED_PROPERTY_NAME,
true)
returns false. If other case, and if some exceptions occur
while attempt to call System.load(...), these classes
don't use native methods and work using only methods implemented in Java.
If you implements your own class, which need native code but can be
implemented without native also, we recommend you to follow the same logic.
In Java applications, you usually don't need to set this property.
In Java applets, we recommend to disable native libraries by the call
GlobalProperties.setBooleanProperty(JAVA_NATIVE_ENABLED_PROPERTY_NAME,false)
before the first access to any other classes excepting
standard Java API classes. Without such a call, Microsoft Internet Explorer
with Microsoft JVM will print an error message while initializing
classes that try to load native library, despite catching
and ignoring all possible exceptions while such an attempt.public static java.util.Properties getProperties()
GlobalProperties class. The returned object is a
clone of the internal store. All keys and values in the returned
java.util.Properties object are instances of String
class.getProperty(String),
getProperty(String, String)public static java.lang.String getProperty(java.lang.String key)
key.
Returns null if there is no property with the given name.
key - the property namejava.lang.NullPointerException - if key argument is nullgetProperty(String, String),
getBooleanProperty(String),
getIntProperty(String),
getLongProperty(String),
getClassProperty(Class, String),
setProperty(String, String)public static java.lang.String getProperty(java.lang.String key,
java.lang.String defaultValue)
key,
or defaultValue if there is no such a property.key - the property namedefaultValue - the default value for this propertydefaultValue for it doesn't existjava.lang.NullPointerException - if key argument is nullgetProperty(String),
getBooleanProperty(String, boolean),
getIntProperty(String, int),
getLongProperty(String, long),
getClassProperty(Class, String, String),
setProperty(String, String)public static java.lang.Boolean getBooleanProperty(java.lang.String key)
key.
It is calculated as a result of Boolean.valueOf(value) call,
where value is a result of getProperty(key) call.
Returns null if there is no property with the given name.
key - the property nameBoolean wrapper for an existing boolean property,
null for non-existing propertyjava.lang.NullPointerException - if key argument is nullgetProperty(String, String)public static boolean getBooleanProperty(java.lang.String key,
boolean defaultValue)
key.
It is calculated as a result of Boolean.valueOf(value).booleanValue() call,
where value is a result of getProperty(key) call.
Returns defaultValue if there is no property with the given name.
key - the property namedefaultValue for it doesn't existjava.lang.NullPointerException - if key argument is nullgetProperty(String, String)public static java.lang.Integer getIntProperty(java.lang.String key)
key.
It is calculated as a result of Integer.valueOf(value) call,
where value is a result of getProperty(key) call.
Returns null if there is no property with the given name.
key - the property nameInteger wrapper for an existing integer property,
null for non-existing propertyjava.lang.NullPointerException - if key argument is nullgetProperty(String, String)public static int getIntProperty(java.lang.String key,
int defaultValue)
key.
It is calculated as a result of Integer.valueOf(value).intValue() call,
where value is a result of getProperty(key) call.
Returns defaultValue if there is no property with the given name.
key - the property namedefaultValue for it doesn't existjava.lang.NullPointerException - if key argument is nullgetProperty(String, String)public static java.lang.Long getLongProperty(java.lang.String key)
key.
It is calculated as a result of Long.valueOf(value) call,
where value is a result of getProperty(key) call.
Returns null if there is no property with the given name.
key - the property nameLong wrapper for an existing long integer property,
null for non-existing propertyjava.lang.NullPointerException - if key argument is nullgetProperty(String, String)public static long getLongProperty(java.lang.String key,
long defaultValue)
key.
It is calculated as a result of Long.valueOf(value).longValue() call,
where value is a result of getProperty(key) call.
Returns defaultValue if there is no property with the given name.
key - the property namedefaultValue for it doesn't existjava.lang.NullPointerException - if key argument is nullgetProperty(String, String)public static java.lang.String getClassPropertyName(java.lang.Class clazz,
java.lang.String key)
GlobalProperties.getClassProperty
and GlobalProperties.getClassXxxProperty.clazz - the given classkey - the property name inside the classclazz.getName() + "." + keyjava.lang.NullPointerException - if clazz or key argument is nullpublic static java.lang.String getClassProperty(java.lang.Class clazz,
java.lang.String key)
getProperty(getClassPropertyName(clazz,key)) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)null for it doesn't existjava.lang.NullPointerException - if clazz or key argument is nullgetProperty(String),
getClassBooleanProperty(Class, String),
getClassIntProperty(Class, String),
getClassLongProperty(Class, String),
getClassProperty(Class, String, String)public static java.lang.String getClassProperty(java.lang.Class clazz,
java.lang.String key,
java.lang.String defaultValue)
getProperty(getClassPropertyName(clazz,key),defaultValue) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)defaultValue - the default value for this propertydefaultValue for it doesn't existjava.lang.NullPointerException - if clazz or key argument is nullgetProperty(String, String),
getClassBooleanProperty(Class, String, boolean),
getClassIntProperty(Class, String, int),
getClassLongProperty(Class, String, long),
getClassProperty(Class, String)public static java.lang.Boolean getClassBooleanProperty(java.lang.Class clazz,
java.lang.String key)
getBooleanProperty(getClassPropertyName(clazz,key)) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)Boolean wrapper for an existing boolean property,
null for non-existing propertyjava.lang.NullPointerException - if clazz or key argument is nullgetBooleanProperty(String),
getClassBooleanProperty(Class, String, boolean)public static boolean getClassBooleanProperty(java.lang.Class clazz,
java.lang.String key,
boolean defaultValue)
getBooleanProperty(getClassPropertyName(clazz,key),defaultValue) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)defaultValue - the default value for this propertydefaultValue for it doesn't existjava.lang.NullPointerException - if clazz or key argument is nullgetBooleanProperty(String, boolean),
getClassBooleanProperty(Class, String)public static java.lang.Integer getClassIntProperty(java.lang.Class clazz,
java.lang.String key)
getIntProperty(getClassPropertyName(clazz,key)) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)Integer wrapper for an existing integer property,
null for non-existing propertyjava.lang.NullPointerException - if clazz or key argument is nullgetIntProperty(String),
getClassIntProperty(Class, String, int)public static int getClassIntProperty(java.lang.Class clazz,
java.lang.String key,
int defaultValue)
getIntProperty(getClassPropertyName(clazz,key),defaultValue) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)defaultValue - the default value for this propertydefaultValue for it doesn't existjava.lang.NullPointerException - if clazz or key argument is nullgetIntProperty(String, int),
getClassIntProperty(Class, String)public static java.lang.Long getClassLongProperty(java.lang.Class clazz,
java.lang.String key)
getLongProperty(getClassPropertyName(clazz,key)) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)Long wrapper for an existing long integer property,
null for non-existing propertyjava.lang.NullPointerException - if clazz or key argument is nullgetLongProperty(String),
getClassLongProperty(Class, String, long)public static long getClassLongProperty(java.lang.Class clazz,
java.lang.String key,
long defaultValue)
getLongProperty(getClassPropertyName(clazz,key),defaultValue) call.clazz - the class the property corresponds to whichkey - the property name inside the class (full property name will be clazz.getName() + "." + key)defaultValue - the default value for this propertydefaultValue for it doesn't existjava.lang.NullPointerException - if clazz or key argument is nullgetLongProperty(String, long),
getClassLongProperty(Class, String)public static int getClassDebugLevel(java.lang.Class clazz)
getClassIntProperty(clazz, "DEBUG_LEVEL", 0).
We recommend to use such properties to control whether the class should print additional debug information or no. For example:
public class MyClass {
public static final int DEBUG_LEVEL = GlobalProperties.getClassDebugLevel(MyClass.class);
. . .
}clazz - the class the property corresponds to whichclazz.getName() + ".DEBUG_LEVEL"
or 0 if there is no such a propertyjava.lang.NullPointerException - if clazz argument is nullgetClassIntProperty(Class, String, int)public static java.lang.String setProperty(java.lang.String key,
java.lang.String value)
key.key - the property namevalue - the new value for this property, must not be nullnull if there was no such a propertyjava.lang.NullPointerException - if key or value argument is nulljava.lang.IllegalAccessError - if fix() method has been called beforegetProperty(String),
setBooleanProperty(String, boolean),
setIntProperty(String, int),
setLongProperty(String, long),
setProperties(Properties),
removeProperty(String),
fix()public static void setBooleanProperty(java.lang.String key,
boolean value)
setProperty(key, String.valueOf(value)) call.key - the property namevalue - the new value for this propertyjava.lang.NullPointerException - if key argument is nullsetProperty(String, String)public static void setIntProperty(java.lang.String key,
int value)
setProperty(key, String.valueOf(value)) call.key - the property namevalue - the new value for this propertyjava.lang.NullPointerException - if key argument is nullsetProperty(String, String)public static void setLongProperty(java.lang.String key,
long value)
setProperty(key, String.valueOf(value)) call.key - the property namevalue - the new value for this propertyjava.lang.NullPointerException - if key argument is nullsetProperty(String, String)public static java.lang.String removeProperty(java.lang.String key)
key.key - the property namenull if there was no such a propertyjava.lang.NullPointerException - if key argument is nulljava.lang.IllegalAccessError - if fix() method has been called beforegetProperty(String),
setProperty(String, String),
setProperties(Properties),
removeProperty(String),
fix()public static void setProperties(java.util.Properties properties)
setProperty(key,value} calls for
all (key, value) pairs contained in
properties argument, excepting all pairs where
key or value is not a non-null instance of the
String class.
A good usage example for Java applications:
GlobalProperties.setProperties(System.getProperties());
Please check that such a call is placed in the very beginning
of your application, before any possible usage of these properties.
In particular, place this call before any access to classes
that probably use these global properties for initializing
there statis class variables, such as DEBUG_LEVEL (see
getClassDebugLevel(Class)).properties - the collection of string properties that will be added to the current global storejava.lang.NullPointerException - if properties argument is nulljava.lang.IllegalAccessError - if fix() method has been called beforegetProperty(String),
setProperty(String, String),
removeProperty(String),
fix()public static void fix()
setProperty(String, String), removeProperty(String),
setProperties(Properties) will lead to IllegalAccessError.
The call of fix() method never throws exceptions. The second and further
its calls do nothing.