- Type Parameters:
-
T- type of the result carried by thisReactiveValue
ReactiveValue can be split into its constituent components by calling result(), exception(), and blocking(). It can be recreated from these components by calling ReactiveValue(Object, Throwable, boolean) or some other constructor. ReactiveValue is immutable.
Reactive code usually takes the form of a method and communicates its output like a method, i.e. via return value or an exception. Reactive code may additionally signal reactive blocking by calling CurrentReactiveScope.block(). Return value, exception, and signaling of reactive blocking constitutes implicit output of reactive computation. ReactiveValue offers an explicit representation of the same. Conversion between explicit and implicit representations is performed by methods get() and capture(Supplier).
ReactiveValue does not carry reactive dependencies. Use ReactiveScope for that.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs newReactiveValue.ReactiveValue(Throwable exception) Constructs newReactiveValuefrom exception.ReactiveValue(Throwable exception, boolean blocking) Constructs newReactiveValuefrom exception and blocking flag.ReactiveValue(T result) Constructs newReactiveValuefrom return value.ReactiveValue(T result, boolean blocking) Constructs newReactiveValuefrom return value and blocking flag.ReactiveValue(T result, Throwable exception, boolean blocking) Constructs newReactiveValuefrom return value, exception, and reactive blocking flag. -
Method Summary
Modifier and TypeMethodDescriptionbooleanblocking()Gets the reactive blocking flag from thisReactiveValue.static <T> ReactiveValue<T>Captures implicit reactive output of providedSupplierand returns it encapsulated in newReactiveValue.booleanCompares thisReactiveValueto another object for equality.Gets the exception component of thisReactiveValue.get()Unpacks explicit reactive output represented by thisReactiveValueinto implicit reactive output.inthashCode()Computes hash code of thisReactiveValue.result()Gets the return value component of thisReactiveValue.booleansame(ReactiveValue<?> other) Checks reference equality between twoReactiveValueinstances.toString()Returns a string representation of thisReactiveValue.
-
Constructor Details
-
ReactiveValue
Constructs newReactiveValuefrom return value, exception, and reactive blocking flag. Only one ofresultandexceptioncan be non-null. The parameters can be later retrieved viaresult(),exception(), andblocking().- Parameters:
-
result- component representing return value of reactive computation that can be later retrieved viaresult() -
exception- component representing exception thrown by reactive computation that can be later retrieved viaexception() -
blocking-trueif the constructedReactiveValueshould represent blocking reactive computation,falseotherwise - Throws:
-
IllegalArgumentException- if bothresultandexceptionare non-null - See Also:
-
ReactiveValue
public ReactiveValue()Constructs newReactiveValue. The newReactiveValuerepresents reactive computation that successfully completed withnullresult and without blocking. TheReactiveValuewill havenullresult()andexception()andfalseblocking()flag. -
ReactiveValue
Constructs newReactiveValuefrom return value. The newReactiveValuerepresents reactive computation that successfully completed without blocking. TheReactiveValuewill havenullexception()andfalseblocking()flag.- Parameters:
-
result- return value of the reactive computation the newReactiveValuerepresents - See Also:
-
ReactiveValue
Constructs newReactiveValuefrom exception. The newReactiveValuerepresents reactive computation that threw an exception without blocking. TheReactiveValuewill havenullresult()andfalseblocking()flag.- Parameters:
-
exception- exception thrown by the reactive computation the newReactiveValuerepresents - See Also:
-
ReactiveValue
Constructs newReactiveValuefrom return value and blocking flag. The newReactiveValuerepresents reactive computation that successfully completed and possibly signaled blocking. TheReactiveValuewill havenullexception().- Parameters:
-
result- return value of the reactive computation the newReactiveValuerepresents -
blocking-trueif the constructedReactiveValueshould represent blocking reactive computation,falseotherwise - See Also:
-
ReactiveValue
Constructs newReactiveValuefrom exception and blocking flag. The newReactiveValuerepresents reactive computation that threw an exception and possibly signaled blocking. TheReactiveValuewill havenullresult().- Parameters:
-
exception- exception thrown by the reactive computation the newReactiveValuerepresents -
blocking-trueif the constructedReactiveValueshould represent blocking reactive computation,falseotherwise - See Also:
-
-
Method Details
-
result
Gets the return value component of thisReactiveValue. Only one ofresult()andexception()can be non-null.- Returns:
-
return value component of
ReactiveValue - See Also:
-
exception
Gets the exception component of thisReactiveValue. Only one ofresult()andexception()can be non-null. -
blocking
public boolean blocking()Gets the reactive blocking flag from thisReactiveValue. Blocking flag is set if thisReactiveValuerepresents output of reactive computation that signaled blocking during its execution by callingCurrentReactiveScope.block().- Returns:
-
trueif thisReactiveValuerepresents output of blocking reactive computation,falseotherwise - See Also:
-
get
Unpacks explicit reactive output represented by thisReactiveValueinto implicit reactive output. Ifexception()is notnull, it is thrown wrapped inCompletionException. Otherwiseresult()is returned. In either case, ifblocking()istrue,CurrentReactiveScope.block()is called.- Returns:
-
value of
result() - Throws:
-
CompletionException- ifexception()is notnull - See Also:
-
capture
Captures implicit reactive output of providedSupplierand returns it encapsulated in newReactiveValue. If thesupplierthrows, returnedReactiveValuewill haveexception()set to the caught exception. Otherwise theReactiveValuewill haveresult()set to value returned from thesupplier.If the
supplierreactively blocks by callingCurrentReactiveScope.block(),blocking()flag will be set on the returnedReactiveValue. This method obtains blocking flag by callingCurrentReactiveScope.blocked()after calling thesupplier, which means the returnedReactiveValuewill haveblocking()flag set also if the currentReactiveScopewas already blocked by the time this method was called. This is reasonable behavior, because thesuppliermight be itself derived from information produced by blocking operations executed earlier during the current reactive computation, which means thatsupplier's output cannot be trusted to be non-blocking.If there is no current
ReactiveScope, i.e.ReactiveScope.current()returnsnull, this method creates temporaryReactiveScopeand executes thesupplierin it, so that blocking flag can be captured. This is particularly useful in unit tests.- Type Parameters:
-
T- type of value returned by thesupplier - Parameters:
-
supplier- reactive code to execute - Returns:
-
ReactiveValueencapsulating implicit reactive output of thesupplier - See Also:
-
equals
Compares thisReactiveValueto another object for equality.ReactiveValuecan only equal anotherReactiveValue. TwoReactiveValueinstances are equal if theirresult(),exception(), andblocking()flags are equal. Value equality is used for bothresult()andexception(). Two exceptions are equal when their stringified form (including stack trace and causes) compares equal.Full value equality checking may be expensive or even undesirable. Use
same(ReactiveValue)to compute shallow reference equality. -
hashCode
public int hashCode()Computes hash code of thisReactiveValue. Hash code is calculated in such a way that if twoReactiveValueinstances are equal as checked byequals(Object), then their hash codes are equal too. This makesReactiveValueusable as a key in aMap.Both
result()andexception()are included in hash code calculation. Exceptions are hashed in such a way that two exceptions with the same stringified form (including stack traces and causes) will have the same hash code. -
same
Checks reference equality between twoReactiveValueinstances. AnotherReactiveValueis reference-equal to this instance according to this method if it is notnulland itsresult(),exception(), andblocking()components are all reference-equal to corresponding components of thisReactiveValue.This method is useful when
equals(Object)would be too expensive or where reference equality is desirable.- Parameters:
-
other-ReactiveValueto compare this instance to ornull - Returns:
-
trueif the twoReactiveValueinstances are reference-equal,falseotherwise - See Also:
-
toString
Returns a string representation of thisReactiveValue. The returned string is suitable for debug output and includes string representation ofresult()orexception()as well as indication whether theReactiveValueisblocking().
-