public class DynamicProperty
extends java.lang.Object
This class is intended for those situations where the value of a property is fetched many times, and the value may be changed on-the-fly. If the property is being read only once, "normal" access methods should be used. If the property value is fixed, consider just caching the value in a variable.
Fetching the cached value is synchronized only on this property, so contention should be negligible. If even that level of overhead is too much for you, you should (a) think real hard about what you are doing, and (b) just cache the property value in a variable and be done with it.
IMPORTANT NOTE
DynamicProperty objects are not subject to normal garbage collection.
They should be used only as a static value that lives for the
lifetime of the program.
Modifier | Constructor and Description |
---|---|
protected |
DynamicProperty() |
Modifier and Type | Method and Description |
---|---|
void |
addCallback(java.lang.Runnable r)
Adds a callback to the DynamicProperty to run
when the value of the propety is updated.
|
void |
addValidator(PropertyChangeValidator validator) |
java.lang.Boolean |
getBoolean()
Gets the current value of the property as an Boolean.
|
java.lang.Boolean |
getBoolean(java.lang.Boolean defaultValue)
Gets the current value of the property as an Boolean.
|
<T> <any> |
getCachedValue(java.lang.Class<T> objectType) |
long |
getChangedTimestamp()
Gets the time (in milliseconds since the epoch)
when the property value was last set/changed.
|
java.lang.Double |
getDouble()
Gets the current value of the property as a Long.
|
java.lang.Double |
getDouble(java.lang.Double defaultValue)
Gets the current value of the property as a Long.
|
java.lang.Float |
getFloat()
Gets the current value of the property as a Float.
|
java.lang.Float |
getFloat(java.lang.Float defaultValue)
Gets the current value of the property as a Float.
|
static DynamicProperty |
getInstance(java.lang.String propName)
Gets the DynamicProperty for a given property name.
|
java.lang.Integer |
getInteger()
Gets the current value of the property as an Integer.
|
java.lang.Integer |
getInteger(java.lang.Integer defaultValue)
Gets the current value of the property as an Integer.
|
java.lang.Long |
getLong()
Gets the current value of the property as a Long.
|
java.lang.Long |
getLong(java.lang.Long defaultValue)
Gets the current value of the property as a Long.
|
java.lang.String |
getName()
Gets the name of the property.
|
java.lang.Class |
getNamedClass()
Gets the current value of the property as a Class.
|
java.lang.Class<?> |
getNamedClass(java.lang.Class<?> defaultValue)
Gets the current value of the property as a Class.
|
java.lang.String |
getString()
Gets the current value of the property as a String.
|
java.lang.String |
getString(java.lang.String defaultValue)
Gets the current value of the property as a String.
|
boolean |
removeCallback(java.lang.Runnable r)
Removes a callback to the DynamicProperty so that it will
no longer be run when the value of the propety is updated.
|
java.lang.String |
toString() |
public static DynamicProperty getInstance(java.lang.String propName)
propName
- the name of the propertypropName
public java.lang.String getName()
public long getChangedTimestamp()
public java.lang.String getString()
public java.lang.String getString(java.lang.String defaultValue)
defaultValue
- the value to return if the property is not definedpublic java.lang.Boolean getBoolean() throws java.lang.IllegalArgumentException
Boolean.TRUE
.
A property string value of "false", "no", "off", "f" or "b"
produces Boolean.FALSE
.
(The value comparison ignores case.)
Any other value will result in an exception.java.lang.IllegalArgumentException
- if the property is defined but
is not an Booleanpublic java.lang.Boolean getBoolean(java.lang.Boolean defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic java.lang.Integer getInteger() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the property is defined but
is not an Integerpublic java.lang.Integer getInteger(java.lang.Integer defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic java.lang.Float getFloat() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the property is defined but is
not a Floatpublic java.lang.Float getFloat(java.lang.Float defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic java.lang.Long getLong() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the property is defined but is
not a Longpublic java.lang.Long getLong(java.lang.Long defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic java.lang.Double getDouble() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the property is defined but is
not a Longpublic java.lang.Double getDouble(java.lang.Double defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic java.lang.Class getNamedClass() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- zif the property is defined but is
not the name of a Classpublic java.lang.Class<?> getNamedClass(java.lang.Class<?> defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not the name of a Classpublic <T> <any> getCachedValue(java.lang.Class<T> objectType)
public void addCallback(java.lang.Runnable r)
public void addValidator(PropertyChangeValidator validator)
public boolean removeCallback(java.lang.Runnable r)
public java.lang.String toString()
toString
in class java.lang.Object