Class ValueSignal<T>

java.lang.Object
com.vaadin.signals.local.AbstractLocalSignal<T>
com.vaadin.signals.local.ValueSignal<T>
Type Parameters:
T - the signal value type
All Implemented Interfaces:
Signal<T>, WritableSignal<T>, Serializable

public class ValueSignal<T> extends AbstractLocalSignal<T> implements WritableSignal<T>
A local writable signal that holds a reference to an object.

Local signals are non-serializable and intended for UI-local state only. They do not participate in clustering and are simpler than shared signals.

Changing the signal to reference another immutable value is an atomic operation. It is safe to concurrently read and write the signal value from multiple threads.

The signal can also be used with mutable values in which case no thread safety is provided. Mutations must be done through modify(ValueModifier) to ensure dependents are informed after the modification is applied.

Local value signals can't be used inside signal transactions.

All operation objects returned from methods on this class are resolved immediately.

See Also:
  • Constructor Details

    • ValueSignal

      public ValueSignal(T initialValue)
      Creates a new value signal with the given initial value.
      Parameters:
      initialValue - the initial value, may be null
    • ValueSignal

      public ValueSignal()
      Creates a new value signal with an initial value of null.
  • Method Details

    • checkPreconditions

      protected void checkPreconditions()
      Description copied from class: AbstractLocalSignal
      Hook for subclasses to perform precondition checks before accessing the value. Called while holding the lock. Default implementation does nothing.
      Overrides:
      checkPreconditions in class AbstractLocalSignal<T>
    • value

      public SignalOperation<T> value(T value)
      Description copied from interface: WritableSignal
      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.
      Specified by:
      value in interface WritableSignal<T>
      Parameters:
      value - the value to set
      Returns:
      an operation containing the eventual result
    • 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.

      Comparison between the expected value and the new value is performed using Object.equals(Object).

      Specified by:
      replace in interface WritableSignal<T>
      Parameters:
      expectedValue - the expected value
      newValue - the new value
      Returns:
      an operation containing the eventual result
    • update

      public CancelableOperation<T> update(SignalUpdater<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. This implementation acquires a lock while running the updater which means that it's never necessary to run the callback again. This also means that canceling the returned operation will never have any effect.

      The result of the returned operation is resolved with the same value that was passed to the updater callback.

      Specified by:
      update in interface WritableSignal<T>
      Parameters:
      updater - the value update callback, not null
      Returns:
      an operation containing the result
    • modify

      public void modify(ValueModifier<T> modifier)
      Runs the given callback to apply changes to a mutable referenced value and then notifies dependents.

      This method is only intended for cases where concurrency is limited through other means, such as Vaadin's session lock. Using this method concurrently with any other methods on the same instance may, but is not guaranteed to, cause an ConcurrentModificationException. The exception can be thrown either from this method or from the other invoked method. This can happen even if the other method is safe for concurrent use.

      Parameters:
      modifier - a callback that receives the current value to modify, not null
    • mapMutable

      public <C> WritableSignal<C> mapMutable(SignalMapper<T,C> getter, SignalModifier<T,C> modifier)
      Creates a two-way mapped signal that uses in-place modification for writing. Reading the mapped signal applies the getter function to extract a child value. Writing to the mapped signal uses the modifier function to update this signal's value in place.

      This method is named differently from WritableSignal.map(SignalMapper, SignalSetter) to avoid ambiguity in method overload resolution when using method references or lambdas.

      This is useful for mutable bean patterns where the parent object's properties are modified directly using setters. For example:

       class Todo {
           private String text;
           private boolean done;
      
           public boolean isDone() {
               return done;
           }
      
           public void setDone(boolean done) {
               this.done = done;
           }
       }
      
       ValueSignal<Todo> todoSignal = new ValueSignal<>(new Todo());
       WritableSignal<Boolean> doneSignal = todoSignal.mapMutable(Todo::isDone,
               Todo::setDone);
      
       checkbox.bindValue(doneSignal); // Two-way binding
       
      Type Parameters:
      C - the child (mapped) signal type
      Parameters:
      getter - the function to extract the child value from this signal's value, not null
      modifier - the function to modify this signal's value in place with the new child value, not null
      Returns:
      a two-way mapped signal using in-place modification, not null