com.vaadin.hilla.signals.

Class NumberSignal

public class NumberSignal extends ValueSignal<Double>

A signal that holds a number value.

  • Constructor Details

    • NumberSignal

      public NumberSignal(Double defaultValue)

      Creates a new NumberSignal with the provided default value.

      Parameters:

      defaultValue - the default value

      Throws:

      NullPointerException - if the default value is null

    • NumberSignal

      public NumberSignal()

      Creates a new NumberSignal with the default value of 0.

    • NumberSignal

      protected NumberSignal(NumberSignal delegate)
  • Method Details

    • getDelegate

      protected NumberSignal getDelegate()

      Overrides:

      getDelegate in class ValueSignal<Double>

    • processEvent

      protected com.fasterxml.jackson.databind.node.ObjectNode processEvent(com.fasterxml.jackson.databind.node.ObjectNode event)

      Processes the event and updates the signal value if needed. Note that this method is not thread-safe and should be called from a synchronized context.

      Overrides:

      processEvent in class ValueSignal<Double>

      Parameters:

      event - the event to process

      Returns:

      true if the event was successfully processed and the signal value was updated, false otherwise.

    • handleIncrement

      protected com.fasterxml.jackson.databind.node.ObjectNode handleIncrement(StateEvent<Double> stateEvent)
    • withOperationValidator

      public NumberSignal withOperationValidator(OperationValidator<Double> validator)

      Returns a new signal that validates the operations with the provided validator. As the same validator is for all operations, the validator should be able to handle all operations that the signal supports.

      For example, the following code creates a signal that only allows increment by 1:

      
       NumberSignal number = new NumberSignal(42.0);
       NumberSignal limitedNumber = number.withOperationValidator(operation -> {
           if (op instanceof IncrementOperation increment
                   && increment.value() != 1) {
               return ValidationResult
                       .reject("Only increment by 1 is allowed");
           }
           return ValidationResult.allow();
       });
       
      Note that the above allows other operations without any validations. If more concise restrictions are needed, specialized operation type should be used:
      
       NumberSignal number = new NumberSignal(42.0);
       NumberSignal limitedNumber = number.withOperationValidator(operation -> {
           return switch (operation) {
               case IncrementOperation increment -> {
                   if (increment.value() != 1) {
                       yield ValidationResult
                           .reject("Only increment by 1 is allowed");
                   }
                   yield ValidationResult.allow();
               }
               case ReplaceValueOperation<Double> ignored ->
                           ValidationResult.reject("No setting is allowed");
               case SetValueOperation<Double> ignored ->
                           ValidationResult.reject("No replacing is allowed");
               default -> ValidationResult.reject("Unknown operation is not allowed");
           };
       });
       

      Overrides:

      withOperationValidator in class ValueSignal<Double>

      Parameters:

      validator - the operation validator, not null

      Returns:

      a new signal that validates the operations with the provided validator.

      Throws:

      NullPointerException - if the validator is null

    • asReadonly

      public NumberSignal asReadonly()

      Description copied from class: Signal

      Returns a read-only instance of the signal that rejects any attempt to modify the signal value. The read-only signal, however, receives the same updates as the original signal does.

      Overrides:

      asReadonly in class ValueSignal<Double>

      Returns:

      the read-only signal