org.apache.tamaya.inject.internal

## Class DefaultDynamicValue<T>

- java.lang.Object
- - org.apache.tamaya.inject.internal.DefaultDynamicValue<T>

- Type Parameters:
    `T` - The type of the value.
  All Implemented Interfaces:
    Serializable, DynamicValue<T>

      public final class DefaultDynamicValue<T>
      extends Object
      implements DynamicValue<T>, Serializable

  A accessor for a single configured value. This can be used to support values that may change during runtime, reconfigured or final. Hereby external code (could be Tamaya configuration listners or client code), can set a new value. Depending on the `UpdatePolicy` the new value is immedeately active or it requires an active commit by client code. Similarly an instance also can ignore all later changes to the value.

  ### Implementation Details

  This class is
  - Serializable, when also the item stored is serializable
  - Thread safe
  See Also:
    Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface org.apache.tamaya.inject.DynamicValue

      `DynamicValue.UpdatePolicy`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addListener(PropertyChangeListener l)` Add a listener to be called as weak reference, when this value has been changed.
    `void` | `commit()` Commits a new value that has not been committed yet, make it the new value of the instance.
    `T` | `commitAndGet()` Performs a commit, if necessary, and returns the current value.
    `void` | `discard()` Discards a new value that was published.
    `T` | `evaluateValue()` Evaluates the current value dynamically from the underlying configuration.
    `T` | `get()` If a value is present in this `DynamicValue`, returns the value, otherwise throws `ConfigException`.
    `T` | `getNewValue()` Access a new value that has not yet been committed.
    `DynamicValue.UpdatePolicy` | `getUpdatePolicy()` Access the `UpdatePolicy` used for updating this value.
    `boolean` | `isPresent()` Return `true` if there is a value present, otherwise `false`.
    `static DynamicValue` | `of(Field annotatedField, Configuration configuration)`
    `static DynamicValue` | `of(Method method, Configuration configuration)`
    `T` | `orElse(T other)` Return the value if present, otherwise return `other`.
    `T` | `orElseGet(ConfiguredItemSupplier<? extends T> other)` Return the value if present, otherwise invoke `other` and return the result of that invocation.
    `<X extends Throwable> T` | `orElseThrow(ConfiguredItemSupplier<? extends X> exceptionSupplier)` Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
    `void` | `removeListener(PropertyChangeListener l)` Removes a listener to be called, when this value has been changed.
    `void` | `setUpdatePolicy(DynamicValue.UpdatePolicy updatePolicy)` Sets a new `UpdatePolicy`.
    `boolean` | `updateValue()` Method to check for and apply a new value.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### of

          public static DynamicValue of(Field annotatedField,
                                        Configuration configuration)

    - #### of

          public static DynamicValue of(Method method,
                                        Configuration configuration)

    - #### commitAndGet

          public T commitAndGet()

      Performs a commit, if necessary, and returns the current value.
      Specified by:
        `commitAndGet` in interface `DynamicValue<T>`
      Returns:
        the non-null value held by this `DynamicValue`
      Throws:
        `ConfigException` - if there is no value present
      See Also:
        `isPresent()`

    - #### commit

          public void commit()

      Commits a new value that has not been committed yet, make it the new value of the instance. On change any registered listeners will be triggered.
      Specified by:
        `commit` in interface `DynamicValue<T>`

    - #### discard

          public void discard()

      Discards a new value that was published. No listeners will be informed.
      Specified by:
        `discard` in interface `DynamicValue<T>`

    - #### getUpdatePolicy

          public DynamicValue.UpdatePolicy getUpdatePolicy()

      Access the `UpdatePolicy` used for updating this value.
      Specified by:
        `getUpdatePolicy` in interface `DynamicValue<T>`
      Returns:
        the update policy, never null.

    - #### setUpdatePolicy

          public void setUpdatePolicy(DynamicValue.UpdatePolicy updatePolicy)

      Sets a new `UpdatePolicy`.
      Specified by:
        `setUpdatePolicy` in interface `DynamicValue<T>`
      Parameters:
        `updatePolicy` - the new policy, not null.

    - #### addListener

          public void addListener(PropertyChangeListener l)

      Add a listener to be called as weak reference, when this value has been changed.
      Specified by:
        `addListener` in interface `DynamicValue<T>`
      Parameters:
        `l` - the listener, not null

    - #### removeListener

          public void removeListener(PropertyChangeListener l)

      Removes a listener to be called, when this value has been changed.
      Specified by:
        `removeListener` in interface `DynamicValue<T>`
      Parameters:
        `l` - the listner to be removed, not null

    - #### get

          public T get()

      If a value is present in this `DynamicValue`, returns the value, otherwise throws `ConfigException`.
      Specified by:
        `get` in interface `DynamicValue<T>`
      Returns:
        the non-null value held by this `Optional`
      Throws:
        `ConfigException` - if there is no value present
      See Also:
        `isPresent()`

    - #### updateValue

          public boolean updateValue()

      Method to check for and apply a new value. Depending on the `UpdatePolicy` the value is immediately or deferred visible (or it may even be ignored completely).
      Specified by:
        `updateValue` in interface `DynamicValue<T>`
      Returns:
        true, if a new value has been detected. The value may not be visible depending on the current `DefaultDynamicValue.UpdatePolicy` in place.

    - #### evaluateValue

          public T evaluateValue()

      Evaluates the current value dynamically from the underlying configuration.
      Specified by:
        `evaluateValue` in interface `DynamicValue<T>`
      Returns:
        the current actual value, or null.

    - #### getNewValue

          public T getNewValue()

      Access a new value that has not yet been committed.
      Specified by:
        `getNewValue` in interface `DynamicValue<T>`
      Returns:
        the uncommitted new value, or null.

    - #### isPresent

          public boolean isPresent()

      Return `true` if there is a value present, otherwise `false`.
      Specified by:
        `isPresent` in interface `DynamicValue<T>`
      Returns:
        `true` if there is a value present, otherwise `false`

    - #### orElse

          public T orElse(T other)

      Return the value if present, otherwise return `other`.
      Specified by:
        `orElse` in interface `DynamicValue<T>`
      Parameters:
        `other` - the value to be returned if there is no value present, may be null
      Returns:
        the value, if present, otherwise `other`

    - #### orElseGet

          public T orElseGet(ConfiguredItemSupplier<? extends T> other)

      Return the value if present, otherwise invoke `other` and return the result of that invocation.
      Specified by:
        `orElseGet` in interface `DynamicValue<T>`
      Parameters:
        `other` - a `ConfiguredItemSupplier` whose result is returned if no value is present
      Returns:
        the value if present otherwise the result of `other.get()`
      Throws:
        `NullPointerException` - if value is not present and `other` is null

    - #### orElseThrow

          public <X extends Throwable> T orElseThrow(ConfiguredItemSupplier<? extends X> exceptionSupplier)
                                              throws X extends Throwable

      Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

      NOTE A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, `IllegalStateException::new`

      Specified by:
        `orElseThrow` in interface `DynamicValue<T>`
      Type Parameters:
        `X` - Type of the exception to be thrown
      Parameters:
        `exceptionSupplier` - The supplier which will return the exception to be thrown
      Returns:
        the present value
      Throws:
        `X` - if there is no value present
        `NullPointerException` - if no value is present and `exceptionSupplier` is null
        `X extends Throwable`

Copyright © 2014–2015 Apache Software Foundation. All rights reserved.