Class ReferenceSignal<T>

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

public class ReferenceSignal<T> extends Object implements WritableSignal<T>
A writable signal that holds a reference to an object.

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(Consumer) to ensure dependents are informed after the modification is applied.

Reference signals can't be used inside signal transactions.

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

  • Constructor Details

    • ReferenceSignal

      public ReferenceSignal(T initialValue)
    • ReferenceSignal

      public ReferenceSignal()
  • Method Details

    • value

      public T value()
      Description copied from interface: Signal
      Gets the current value of this signal. The value is read in a way that takes the current transaction into account and in the case of clustering also changes that have been submitted to the cluster but not yet confirmed.

      If the signal implementation supports transactions, then reading the value in a regular (i.e. Transaction.Type.STAGED) transaction makes the transaction depend on the value so that the transaction fails in case the signal value is changed concurrently.

      Reading the value inside an Signal.effect(Runnable) or Signal.computed(Supplier) callback sets up that effect or computed signal to depend on the signal.

      Specified by:
      value in interface Signal<T>
      Returns:
      the signal value
    • peek

      public T peek()
      Description copied from interface: Signal
      Reads the value without setting up any dependencies. This method returns the same value as Signal.value() but without creating a dependency when used inside a transaction, effect or computed signal.
      Specified by:
      peek in interface Signal<T>
      Returns:
      the signal value
    • 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(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. 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(Consumer<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