Class ScopedValue.Carrier
- Enclosing class:
ScopedValue<T>
A Carrier is used to accumulate mappings so that an operation (a Runnable or ScopedValue.CallableOp) can be executed with all scoped values in the
mapping bound to values. The following example runs an operation with k1
bound (or rebound) to v1, and k2 bound (or rebound) to v2.
A Carrier is immutable and thread-safe. The where method returns a new Carrier object,
it does not mutate an existing mapping.
Unless otherwise specified, passing a null argument to a method in
this class will cause a NullPointerException to be thrown.
- Since:
- 25
-
Method Summary
Modifier and TypeMethodDescription<R, X extends Throwable>
Rcall(ScopedValue.CallableOp<? extends R, X> op) Calls a value-returning operation with each scoped value in this mapping bound to its value in the current thread.<T> Tget(ScopedValue<T> key) Returns the value of aScopedValuein this mapping.voidRuns an operation with each scoped value in this mapping bound to its value in the current thread.where(ScopedValue<T> key, T value) Returns a newCarrierwith the mappings from this carrier plus a new mapping fromkeytovalue.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Method Details
-
where
Returns a newCarrierwith the mappings from this carrier plus a new mapping fromkeytovalue. If this carrier already has a mapping for the scoped valuekeythen it will map to the newvalue. The current carrier is immutable, so it is not changed by this method.- Type Parameters:
T- the type of the value- Parameters:
key- theScopedValuekeyvalue- the value, can benull- Returns:
- a new
Carrierwith the mappings from this carrier plus the new mapping
-
get
Returns the value of aScopedValuein this mapping.- Type Parameters:
T- the type of the value- Parameters:
key- theScopedValuekey- Returns:
- the value
- Throws:
NoSuchElementException- if the key is not present in this mapping
-
call
Calls a value-returning operation with each scoped value in this mapping bound to its value in the current thread. When the operation completes (normally or with an exception), each scoped value in the mapping will revert to being unbound, or revert to its previous value when previously bound, in the current thread. Ifopcompletes with an exception then it propagated by this method.Scoped values are intended to be used in a structured manner. If code invoked directly or indirectly by the operation creates a
StructuredTaskScopePREVIEW but does not closePREVIEW it, then it is detected as a structure violation when the operation completes (normally or with an exception). In that case, the underlying construct of theStructuredTaskScopeis closed andStructureViolationExceptionPREVIEW is thrown.- Type Parameters:
R- the type of the result of the operationX- type of the exception thrown by the operation- Parameters:
op- the operation to run- Returns:
- the result
- Throws:
StructureViolationExceptionPREVIEW- if a structure violation is detectedX- ifopcompletes with an exception
-
run
Runs an operation with each scoped value in this mapping bound to its value in the current thread. When the operation completes (normally or with an exception), each scoped value in the mapping will revert to being unbound, or revert to its previous value when previously bound, in the current thread. Ifopcompletes with an exception then it propagated by this method.Scoped values are intended to be used in a structured manner. If code invoked directly or indirectly by the operation creates a
StructuredTaskScopePREVIEW but does not closePREVIEW it, then it is detected as a structure violation when the operation completes (normally or with an exception). In that case, the underlying construct of theStructuredTaskScopeis closed andStructureViolationExceptionPREVIEW is thrown.- Parameters:
op- the operation to run- Throws:
StructureViolationExceptionPREVIEW- if a structure violation is detected
-