com.vaadin.signals.

Class ValueSignal<T>

java.lang.Object
com.vaadin.signals.Signal<T>
com.vaadin.signals.ValueSignal<T>

Type Parameters:

T - the signal value type

Direct Known Subclasses:

NumberSignal

public class ValueSignal<T> extends Signal<T>

A signal containing a value. The value is updated as a single atomic change. It's recommended to use immutable values and this is partially enforced by the way a new instance is created from the underlying JSON data every time the value is read.

  • Constructor Details

    • ValueSignal

      public ValueSignal(T initialValue)

      Creates a new value signal with the given initial value. The type of the signal will be based on the type (Object.getClass()) of the initial value instance. The signal does not support clustering.

      Parameters:

      initialValue - the initial value to use, not null

    • ValueSignal

      public ValueSignal(Class<T> valueType)

      Creates a new value signal of the given type with no value. The signal does not support clustering.

      Parameters:

      valueType - the value type, not null

    • ValueSignal

      protected ValueSignal(SignalTree tree, Id id, Predicate<SignalCommand> validator, Class<T> valueType)

      Creates a new value signal instance with the given id and validator for the given signal tree with the given value type.

      Parameters:

      tree - the signal tree that contains the value for this signal, not null

      id - the id of the signal node within the signal tree, not null

      validator - the validator to check operations submitted to this singal, not null

      valueType - the value type, not null

  • Method Details

    • value

      public SignalOperation<T> value(T value)

      Sets the value of this signal. The result of the returned operation will be resolved with the previous value at the time when this operation was confirmed.

      Parameters:

      value - the value to set

      Returns:

      an operation containing the eventual result

    • extractValue

      protected T extractValue(Node.Data data)

      Description copied from class: Signal

      Extracts the value for this signal from the given signal data node.

      Specified by:

      extractValue in class Signal<T>

      Parameters:

      data - the data node to extract the value from, or null if the node doesn't exist in the tree

      Returns:

      the signal value

    • usageChangeValue

      protected Object usageChangeValue(Node.Data data)

      Description copied from class: Signal

      Gets a reference value that will be used to determine whether a dependency based on previous usage should be invalidated. This is done by getting one reference value when the dependency occurs and then comparing that to the current value to determine if the value has changed.

      The implementation should return an object that changes if and only if the Signal.value() of this signal changes.

      Specified by:

      usageChangeValue in class Signal<T>

      Parameters:

      data - the data node to read from, not null

      Returns:

      a reference value to use for validity checks, may be null

    • replace

      public SignalOperation<Void> replace(T expectedValue, T newValue)

      Sets the value of this signal if and only if the signal has the expected value at the time when the operation is confirmed. This is the signal counterpart to AtomicReference.compareAndSet(Object, Object). The result of the returned operation will be resolved as successful if the expected value was present and resolved as unsuccessful if any other value was present when the operation is processed.

      Parameters:

      expectedValue - the expected value

      newValue - the new value

      Returns:

      an operation containing the eventual result

    • update

      public CancelableOperation<T> update(UnaryOperator<T> updater)

      Updates the signal value based on the given callback. The callback receives the current signal value and returns the new value to use. If the original value has changed by the time this change is confirmed, then the returned value is ignored and the callback is run again with the new value as input. This process is repeated until cancelled or until the update succeeds without conflicting changes.

      The process can be cancelled through the returned operation instance. Note that canceling will only prevent further retries but the change will still be made if the currently running attempt succeeds.

      The result of the returned operation will be resolved with the previous value at the time when a successful update operation was confirmed.

      Update operations cannot participate in transactions since any retry would occur after the original transaction has already been committed. For this reason, the whole operation completely bypasses all transaction handling.

      Parameters:

      updater - the value update callback, not null

      Returns:

      an operation containing the eventual result

    • verifyValue

      public SignalOperation<Void> verifyValue(T expectedValue)

      Checks that this signal has the expected value. This operation is only meaningful to use as a condition in a transaction. The result of the returned operation will be resolved as successful if the expected value was present and resolved as unsuccessful if any other value was present when the operation is processed.

      Parameters:

      expectedValue - the expected value

      Returns:

      an operation containing the eventual result

    • withValidator

      public ValueSignal<T> withValidator(Predicate<SignalCommand> validator)

      Wraps this signal with a validator. The validator is used to check all value changing commands issued through the new signal instance. If this signal has a validator, then the new signal will use both validators. Note that due to the way validators are retained by asNode(), there's a possibility that the validator also receives commands that cannot be directly issued for a value signal.

      This signal will keep its current configuration and changes applied through this instance will be visible through the wrapped instance.

      Parameters:

      validator - the validator to use, not null

      Returns:

      a new value signal that uses the validator, not null

    • asReadonly

      public ValueSignal<T> asReadonly()

      Wraps this signal to not accept changes.

      This signal will keep its current configuration and changes applied through this instance will be visible through the wrapped instance.

      Returns:

      the new readonly value signal, not null

    • asNode

      public NodeSignal asNode()

      Description copied from class: Signal

      Converts this signal into a node signal. This allows further conversion into any specific signal type through the methods in NodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.

      Overrides:

      asNode in class Signal<T>

      Returns:

      this signal as a node signal, not null

    • equals

      public boolean equals(Object obj)

      Overrides:

      equals in class Object

    • hashCode

      public int hashCode()

      Overrides:

      hashCode in class Object

    • toString

      public String toString()

      Overrides:

      toString in class Object