com.vaadin.hilla.signals.
Class NumberSignal
A signal that holds a number value.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new NumberSignal with the default value of 0.
protected
NumberSignal
(NumberSignal delegate) NumberSignal
(Double defaultValue) Creates a new NumberSignal with the provided default value.
-
Method Summary
Modifier and TypeMethodDescriptionReturns a read-only instance of the signal that rejects any attempt to modify the signal value.
protected NumberSignal
protected com.fasterxml.jackson.databind.node.ObjectNode
handleIncrement
(StateEvent<Double> stateEvent) 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.
withOperationValidator
(OperationValidator<Double> validator) Returns a new signal that validates the operations with the provided validator.
Methods inherited from class com.vaadin.hilla.signals.ValueSignal
compareAndSet, createSnapshotEvent, getValue, handleReplaceEvent, handleSetEvent, handleValidationResult, subscribe, subscribe
-
Constructor Details
-
NumberSignal
Creates a new NumberSignal with the provided default value.
Parameters:
defaultValue
- the default valueThrows:
NullPointerException
- if the default value is null -
NumberSignal
public NumberSignal()Creates a new NumberSignal with the default value of 0.
-
NumberSignal
-
-
Method Details
-
getDelegate
Overrides:
getDelegate
in classValueSignal<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 classValueSignal<Double>
Parameters:
event
- the event to processReturns:
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
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:
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 -> { if (op instanceof IncrementOperation increment && increment.value() != 1) { return ValidationResult .reject("Only increment by 1 is allowed"); } return ValidationResult.allow(); });
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 classValueSignal<Double>
Parameters:
validator
- the operation validator, notnull
Returns:
a new signal that validates the operations with the provided validator.
Throws:
NullPointerException
- if the validator isnull
-
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 classValueSignal<Double>
Returns:
the read-only signal
-