Class AbstractSignal<T>
- Type Parameters:
T- the signal value type
- All Implemented Interfaces:
Signal<T>
- Direct Known Subclasses:
ComputedSignal,ListSignal,MapSignal,NodeSignal,ValueSignal
This signal may be synchronized across a cluster. In that case, changes to
the signal value are only confirmed asynchronously. The regular signal
value() returns the assumed value based on local modifications
whereas peekConfirmed() gives access to the confirmed value.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Predicate<SignalCommand> Signal validator that accepts anything. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractSignal(SignalTree tree, Id id, Predicate<SignalCommand> validator) Creates a new signal instance with the given id and validator for the given signal tree. -
Method Summary
Modifier and TypeMethodDescriptionprotected NodeSignalasNode()Converts this signal into a node signal.protected SignalOperation<Void> clear()Helper to submit a clear command.protected UsageTracker.UsagecreateUsage(Transaction transaction) Creates a usage instance based on the current state of this signal.protected Node.Datadata(Transaction transaction) Gets the data node for this signal in the given transaction.protected Node.Datadata(TreeRevision revision) Gets the data node for this signal in the given tree revision.protected abstract TextractValue(Node.Data data) Extracts the value for this signal from the given signal data node.protected static <T> THelper to convert the given JSON to a Java instance of the given type using the global signal object mapper.id()Gets the unique id of this signal instance.protected Predicate<SignalCommand> mergeValidators(Predicate<SignalCommand> validator) Merges the validator used by this signal with the given validator.protected static <T> THelper to convert the value of the given node into Java object of the given type.peek()Reads the value without setting up any dependencies.Reads the confirmed value without setting up any dependencies.protected SignalOperation<Void> remove(AbstractSignal<?> child) Helper to submit a remove command.protected SignalOperation<Void> submit(SignalCommand command) Submits a command for this signal and updates the created operation without a value once the command result is confirmed.protected <R> SignalOperation<R> submit(SignalCommand command, Function<CommandResult.Accept, R> resultConverter) Submits a command for this signal and uses the provided result converter to updates the created operation once the command result is confirmed.protected <R,O extends SignalOperation<R>>
Osubmit(SignalCommand command, Function<CommandResult.Accept, R> resultConverter, O operation) Submits a command for this signal and updates the given operation using the given result converter once the command result is confirmed.protected <I extends AbstractSignal<?>>
InsertOperation<I> submitInsert(SignalCommand command, Function<Id, I> childFactory) Submits a command for this signal and creates and insert operation that is updated once the command result is confirmed.protected <O extends SignalOperation<Void>>
OsubmitVoidOperation(SignalCommand command, O operation) Submits a command for this signal and updates the given operation without a value once the command result is confirmed.protected static tools.jackson.databind.JsonNodeHelper to convert the given object to JSON using the global signal object mapper.protected SignalTreetree()Gets the signal tree that stores the value for this signal.protected abstract ObjectusageChangeValue(Node.Data data) Gets a reference value that will be used to determine whether a dependency based on previous usage should be invalidated.protected Predicate<SignalCommand> Gets the validator used by this signal instance.value()Gets the current value of this signal.
-
Field Details
-
ANYTHING_GOES
Signal validator that accepts anything. This is defined as a constant to enable using==to detect and optimize cases where no validation is applied.
-
-
Constructor Details
-
AbstractSignal
Creates a new signal instance with the given id and validator for the given signal tree.- Parameters:
tree- the signal tree that contains the value for this signal, notnullid- the id of the signal node within the signal tree, notnullvalidator- the validator to check operations submitted to this singal, notnull
-
-
Method Details
-
data
Gets the data node for this signal in the given tree revision.- Parameters:
revision- the tree revision to read from, notnull- Returns:
- the data node, or
nullif there is no node for this signal in the revision
-
data
Gets the data node for this signal in the given transaction.- Parameters:
transaction- the transaction to read from, notnull- Returns:
- the data node, or
nullif there is no node for this signal in the transaction
-
value
Description copied from interface:SignalGets 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)orSignal.computed(Supplier)callback sets up that effect or computed signal to depend on the signal. -
peek
Reads the value without setting up any dependencies. This method returns the same value asvalue()but without creating a dependency when used inside a transaction, effect or computed signal.- Returns:
- the signal value
-
peekConfirmed
Reads the confirmed value without setting up any dependencies. The confirmed value doesn't consider changes in the current transaction or changes that have been submitted but not yet confirmed in a cluster.- Returns:
- the confirmed signal value
-
validator
Gets the validator used by this signal instance.- Returns:
- the used validator, not
null
-
mergeValidators
Merges the validator used by this signal with the given validator. This chains the two validators so that both must accept any change but it additionally avoids redundant chaining in case either validator isANYTHING_GOES.- Parameters:
validator- the validator to merge, notnull- Returns:
- a combined validator, not
null
-
extractValue
Extracts the value for this signal from the given signal data node.- Parameters:
data- the data node to extract the value from, ornullif the node doesn't exist in the tree- Returns:
- the signal value
-
usageChangeValue
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
value()of this signal changes.- Parameters:
data- the data node to read from, notnull- Returns:
- a reference value to use for validity checks, may be
null
-
submit
protected <R,O extends SignalOperation<R>> O submit(SignalCommand command, Function<CommandResult.Accept, R> resultConverter, O operation) Submits a command for this signal and updates the given operation using the given result converter once the command result is confirmed. The command is submitted through the currentTransactionand it usesSignalEnvironment.getCurrentResultNotifier()for delivering the result update.- Type Parameters:
R- the result typeO- the operation type- Parameters:
command- the command to submit, notnullresultConverter- a callback for creating an operation result value based on the command result, notnulloperation- the operation to update with the eventual result, notnull- Returns:
- the provided operation, for chaining
-
submitVoidOperation
protected <O extends SignalOperation<Void>> O submitVoidOperation(SignalCommand command, O operation) Submits a command for this signal and updates the given operation without a value once the command result is confirmed. This is a shorthand forsubmit(SignalCommand, Function, SignalOperation)in the case of operations that don't have a result value.- Type Parameters:
O- the operation type- Parameters:
command- the command to submit, notnulloperation- the operation to update with the eventual result, notnull- Returns:
- the provided operation, for chaining
-
submitInsert
protected <I extends AbstractSignal<?>> InsertOperation<I> submitInsert(SignalCommand command, Function<Id, I> childFactory) Submits a command for this signal and creates and insert operation that is updated once the command result is confirmed. This is a shorthand forsubmit(SignalCommand, Function, SignalOperation)in the case of insert operations.- Type Parameters:
I- the insert operation type- Parameters:
command- the command to submit, notnullchildFactory- callback used to create a signal instance in the insert operation, notnull- Returns:
- the created insert operation, not
null
-
submit
protected <R> SignalOperation<R> submit(SignalCommand command, Function<CommandResult.Accept, R> resultConverter) Submits a command for this signal and uses the provided result converter to updates the created operation once the command result is confirmed. This is a shorthand forsubmit(SignalCommand, Function, SignalOperation)in the case of using the default operation type.- Type Parameters:
R- the operation result value- Parameters:
command- the command to submit, notnullresultConverter- a callback for creating an operation result value based on the command result, notnull- Returns:
- the created operation instance, not
null
-
submit
Submits a command for this signal and updates the created operation without a value once the command result is confirmed. This is a shorthand forsubmit(SignalCommand, Function, SignalOperation)in the case of using the default operation type and no result value.- Parameters:
command- the command to submit, notnull- Returns:
- the created operation instance, not
null
-
id
Gets the unique id of this signal instance. The id will be the same for other signal instances backed by the same data, e.g. in the case of usingasNode()to create a signal of different type.- Returns:
- the signal id, not null
-
tree
Gets the signal tree that stores the value for this signal.- Returns:
- the signal tree, not
null
-
createUsage
Creates a usage instance based on the current state of this signal.- Parameters:
transaction- the transaction for which the usage occurs, notnull- Returns:
- a usage instance, not
null
-
asNode
Converts this signal into a node signal. This allows further conversion into any specific signal type through the methods inNodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.- Returns:
- this signal as a node signal, not
null
-
clear
Helper to submit a clear command. This is a helper is re-defined as public in the signal types where a clear operation makes sense.- Returns:
- the created signal operation instance, not
null
-
remove
Helper to submit a remove command. This is a helper is re-defined as public in the signal types where a remove operation makes sense.- Parameters:
child- the child signal to remove, notnull- Returns:
- the created signal operation instance, not
null
-
toJson
Helper to convert the given object to JSON using the global signal object mapper.- Parameters:
value- the object to convert to JSON- Returns:
- the converted JSON node, not
null - See Also:
-
fromJson
Helper to convert the given JSON to a Java instance of the given type using the global signal object mapper.- Type Parameters:
T- the target type- Parameters:
value- the JSON value to converttargetType- the target type, notnull- Returns:
- the converted Java instance
- See Also:
-
nodeValue
Helper to convert the value of the given node into Java object of the given type.- Type Parameters:
T- the Java object type- Parameters:
node- the signal node to read the value from, notnullvalueType- the type to convert to, notnull- Returns:
- the converted Java instance
-