Package com.atlassian.bitbucket.request
Interface RequestLocal<T>
- Type Parameters:
T
- the value type
public interface RequestLocal<T>
Value that is resolved in the current
RequestContext
, if one is active. Conceptually similar to a
ThreadLocal
, but tied to the current RequestContext
instead of the current thread.
Note that the request context can be propagated to background threads such as event listener threads and repository
hook handler threads. As a result, the object stored in a RequestLocal
could be accessed from different
threads and care should be taken to ensure the stored object is thread safe when interacting with it from multiple
threads (e.g. the request thread, an asynchronous event listener and/or a repository hook).
- Since:
- 7.9
-
Method Summary
Modifier and TypeMethodDescriptionAttempts to compute the value given the current value.computeIfAbsent
(Supplier<? extends T> missingValueSupplier) Returns the value, if present.get()
void
If a value is present, invoke the specified consumer with the value, otherwise do nothing.boolean
isActive()
boolean
remove()
Removes the value from the currentRequestContext
, if present.Removes the value from the currentRequestContext
, if a non-null value is present and thepredicate
matches the value.boolean
Sets the value.
-
Method Details
-
compute
Attempts to compute the value given the current value. The value returned by themappingFunction
is stored as the value for currentRequestContext
.If there is no current
RequestContext
, themappingFunction
is still invoked but the computed value is not stored.- Parameters:
mappingFunction
- function that computes the value given the current value which may benull
if no value is present.- Returns:
- the computed value
-
computeIfAbsent
Returns the value, if present. If anRequestContext
is active, but no value is set, the value supplied by themissingValueSupplier
is stored and returned.If no
RequestContext
is active,missingValueSupplier
is still invoked, but the computed value is not stored.- Parameters:
missingValueSupplier
- supplier of the value when- Returns:
- the value, possibly supplied by
missingValueSupplier
-
get
- Returns:
- the value, or
null
if not set or noRequestContext
is active
-
ifPresent
If a value is present, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer
- block to be executed if a value is present
-
isActive
boolean isActive()- Returns:
true
is aRequestContext
is currently active, otherwisefalse
-
isPresent
boolean isPresent()- Returns:
true
when a value other thannull
is defined in the currentRequestContext
.
-
remove
Removes the value from the currentRequestContext
, if present.- Returns:
- the removed value, if the value was removed; otherwise
null
-
removeIf
Removes the value from the currentRequestContext
, if a non-null value is present and thepredicate
matches the value. If no value is present, the predicate will not be called. The Predicate will never be called with anull
value.- Parameters:
predicate
- the predicate- Returns:
- the removed value, if the value was removed; otherwise
null
-
set
Sets the value. Setting anull
value is equivalent to callingremove()
.- Parameters:
value
- the value- Returns:
true
if the value was set, orfalse
if noRequestContext
is active
-