ScopedValuePREVIEW<T>
public static final class ScopedValue.Carrier extends Object
Carrier
is a preview API of the Java platform. A Carrier
is used to accumulate mappings so that an operation (a Runnable
or Callable
) 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.
Modifier and Type | Method | Description |
---|---|---|
<R> R |
call |
Calls a value-returning operation with each scoped value in this mapping bound to its value in the current thread. |
<T> T |
get |
Returns the value of a ScopedValue PREVIEW in this mapping. |
<R> R |
get |
Invokes a supplier of results with each scoped value in this mapping bound to its value in the current thread. |
void |
run |
Runs an operation with each scoped value in this mapping bound to its value in the current thread. |
<T> ScopedValue.CarrierPREVIEW |
where |
Returns a new Carrier with the mappings from this carrier plus a new mapping from key to value . |
public <T> ScopedValue.CarrierPREVIEW where(ScopedValuePREVIEW<T> key, T value)
Carrier
with the mappings from this carrier plus a new mapping from key
to value
. If this carrier already has a mapping for the scoped value key
then it will map to the new value
. The current carrier is immutable, so it is not changed by this method.T
- the type of the valuekey
- the ScopedValue
keyvalue
- the value, can be null
Carrier
with the mappings from this carrier plus the new mappingpublic <T> T get(ScopedValuePREVIEW<T> key)
ScopedValue
PREVIEW in this mapping.T
- the type of the valuekey
- the ScopedValue
keyNoSuchElementException
- if the key is not present in this mappingpublic <R> R call(Callable<? extends R> op) throws Exception
op
completes 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 StructuredTaskScope
PREVIEW 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 the StructuredTaskScope
is closed and StructureViolationException
PREVIEW is thrown.
R
- the type of the result of the operationop
- the operation to runStructureViolationExceptionPREVIEW
- if a structure violation is detectedException
- if op
completes with an exceptionpublic <R> R get(Supplier<? extends R> op)
op
completes 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 StructuredTaskScope
PREVIEW 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 the StructuredTaskScope
is closed and StructureViolationException
PREVIEW is thrown.
R
- the type of the result of the operationop
- the operation to runStructureViolationExceptionPREVIEW
- if a structure violation is detectedpublic void run(Runnable op)
op
completes 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 StructuredTaskScope
PREVIEW 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 the StructuredTaskScope
is closed and StructureViolationException
PREVIEW is thrown.
op
- the operation to runStructureViolationExceptionPREVIEW
- if a structure violation is detected
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ScopedValue.Carrier.html
Carrier
when preview features are enabled.