com.netflix.config
Class DynamicProperty

java.lang.Object
  extended by com.netflix.config.DynamicProperty

public class DynamicProperty
extends java.lang.Object

A cached configuration property value that is automatically updated when the config is changed. The object is fully thread safe, and access is very fast. (In fact, testing indicates that using a DynamicProperty is faster than fetching a System Property.)

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.


Constructor Summary
protected DynamicProperty()
           
 
Method Summary
 void addCallback(java.lang.Runnable r)
          Adds a callback to the DynamicProperty to run when the value of the propety is updated.
 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.
 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()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DynamicProperty

protected DynamicProperty()
Method Detail

getInstance

public static DynamicProperty getInstance(java.lang.String propName)
Gets the DynamicProperty for a given property name. This may be a previously constructed object, or an object constructed on-demand to satisfy the request.

Parameters:
propName - the name of the property
Returns:
a DynamicProperty object that holds the cached value of the configuration property named propName

getName

public java.lang.String getName()
Gets the name of the property.


getChangedTimestamp

public long getChangedTimestamp()
Gets the time (in milliseconds since the epoch) when the property value was last set/changed.


getString

public java.lang.String getString()
Gets the current value of the property as a String.

Returns:
the current property value, or null if there is none

getString

public java.lang.String getString(java.lang.String defaultValue)
Gets the current value of the property as a String.

Parameters:
defaultValue - the value to return if the property is not defined
Returns:
the current property value, or the default value there is none

getBoolean

public java.lang.Boolean getBoolean()
                             throws java.lang.IllegalArgumentException
Gets the current value of the property as an Boolean. A property string value of "true", "yes", "on", "t" or "y" produces 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.

Returns:
the current property value, or null if there is none.
Throws:
java.lang.IllegalArgumentException - if the property is defined but is not an Boolean

getBoolean

public java.lang.Boolean getBoolean(java.lang.Boolean defaultValue)
Gets the current value of the property as an Boolean.

Parameters:
defaultValue - the value to return if the property is not defined, or is not of the proper format
Returns:
the current property value, or the default value if there is none or the property is not of the proper format

getInteger

public java.lang.Integer getInteger()
                             throws java.lang.IllegalArgumentException
Gets the current value of the property as an Integer.

Returns:
the current property value, or null if there is none.
Throws:
java.lang.IllegalArgumentException - if the property is defined but is not an Integer

getInteger

public java.lang.Integer getInteger(java.lang.Integer defaultValue)
Gets the current value of the property as an Integer.

Parameters:
defaultValue - the value to return if the property is not defined, or is not of the proper format
Returns:
the current property value, or the default value if there is none or the property is not of the proper format

getFloat

public java.lang.Float getFloat()
                         throws java.lang.IllegalArgumentException
Gets the current value of the property as a Float.

Returns:
the current property value, or null if there is none.
Throws:
java.lang.IllegalArgumentException - if the property is defined but is not a Float

getFloat

public java.lang.Float getFloat(java.lang.Float defaultValue)
Gets the current value of the property as a Float.

Parameters:
defaultValue - the value to return if the property is not defined, or is not of the proper format
Returns:
the current property value, or the default value if there is none or the property is not of the proper format

getLong

public java.lang.Long getLong()
                       throws java.lang.IllegalArgumentException
Gets the current value of the property as a Long.

Returns:
the current property value, or null if there is none.
Throws:
java.lang.IllegalArgumentException - if the property is defined but is not a Long

getLong

public java.lang.Long getLong(java.lang.Long defaultValue)
Gets the current value of the property as a Long.

Parameters:
defaultValue - the value to return if the property is not defined, or is not of the proper format
Returns:
the current property value, or the default value if there is none or the property is not of the proper format

getDouble

public java.lang.Double getDouble()
                           throws java.lang.IllegalArgumentException
Gets the current value of the property as a Long.

Returns:
the current property value, or null if there is none.
Throws:
java.lang.IllegalArgumentException - if the property is defined but is not a Long

getDouble

public java.lang.Double getDouble(java.lang.Double defaultValue)
Gets the current value of the property as a Long.

Parameters:
defaultValue - the value to return if the property is not defined, or is not of the proper format
Returns:
the current property value, or the default value if there is none or the property is not of the proper format

getNamedClass

public java.lang.Class getNamedClass()
                              throws java.lang.IllegalArgumentException
Gets the current value of the property as a Class.

Returns:
the current property value, or null if there is none.
Throws:
java.lang.IllegalArgumentException - zif the property is defined but is not the name of a Class

getNamedClass

public java.lang.Class<?> getNamedClass(java.lang.Class<?> defaultValue)
Gets the current value of the property as a Class.

Parameters:
defaultValue - the value to return if the property is not defined, or is not the name of a Class
Returns:
the current property value, or the default value if there is none or the property is not of the proper format

addCallback

public void addCallback(java.lang.Runnable r)
Adds a callback to the DynamicProperty to run when the value of the propety is updated.


removeCallback

public 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.

Returns:
true iff the callback was previously registered

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object