public class DynamicProperty extends 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(Runnable r)
Adds a callback to the DynamicProperty to run
when the value of the propety is updated.
|
Boolean |
getBoolean()
Gets the current value of the property as an Boolean.
|
Boolean |
getBoolean(Boolean defaultValue)
Gets the current value of the property as an Boolean.
|
long |
getChangedTimestamp()
Gets the time (in milliseconds since the epoch)
when the property value was last set/changed.
|
Double |
getDouble()
Gets the current value of the property as a Long.
|
Double |
getDouble(Double defaultValue)
Gets the current value of the property as a Long.
|
Float |
getFloat()
Gets the current value of the property as a Float.
|
Float |
getFloat(Float defaultValue)
Gets the current value of the property as a Float.
|
static DynamicProperty |
getInstance(String propName)
Gets the DynamicProperty for a given property name.
|
Integer |
getInteger()
Gets the current value of the property as an Integer.
|
Integer |
getInteger(Integer defaultValue)
Gets the current value of the property as an Integer.
|
Long |
getLong()
Gets the current value of the property as a Long.
|
Long |
getLong(Long defaultValue)
Gets the current value of the property as a Long.
|
String |
getName()
Gets the name of the property.
|
Class |
getNamedClass()
Gets the current value of the property as a Class.
|
Class<?> |
getNamedClass(Class<?> defaultValue)
Gets the current value of the property as a Class.
|
String |
getString()
Gets the current value of the property as a String.
|
String |
getString(String defaultValue)
Gets the current value of the property as a String.
|
boolean |
removeCallback(Runnable r)
Removes a callback to the DynamicProperty so that it will
no longer be run when the value of the propety is updated.
|
String |
toString() |
public static DynamicProperty getInstance(String propName)
propName
- the name of the propertypropName
public String getName()
public long getChangedTimestamp()
public String getString()
public String getString(String defaultValue)
defaultValue
- the value to return if the property is not definedpublic Boolean getBoolean() throws 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.IllegalArgumentException
- if the property is defined but
is not an Booleanpublic Boolean getBoolean(Boolean defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic Integer getInteger() throws IllegalArgumentException
IllegalArgumentException
- if the property is defined but
is not an Integerpublic Integer getInteger(Integer defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic Float getFloat() throws IllegalArgumentException
IllegalArgumentException
- if the property is defined but is
not a Floatpublic Float getFloat(Float defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic Long getLong() throws IllegalArgumentException
IllegalArgumentException
- if the property is defined but is
not a Longpublic Long getLong(Long defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic Double getDouble() throws IllegalArgumentException
IllegalArgumentException
- if the property is defined but is
not a Longpublic Double getDouble(Double defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not of the proper formatpublic Class getNamedClass() throws IllegalArgumentException
IllegalArgumentException
- zif the property is defined but is
not the name of a Classpublic Class<?> getNamedClass(Class<?> defaultValue)
defaultValue
- the value to return if the property is not defined,
or is not the name of a Classpublic void addCallback(Runnable r)
public boolean removeCallback(Runnable r)
Copyright © 2016. All Rights Reserved.