Interface ThreadContext
- All Known Implementing Classes:
DefaultThreadContext
public interface ThreadContext
This class represents the context of a thread, allowing to access and share information transparently across threads.
A typical use case for a thread context is to store information about the current request in a servlet container.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
containsProperty
(String name) Check whether a property with the given name is present and readable.default ThreadContext
Create a newThreadContext
containing all properties of this context.<T> io.vavr.control.Try<T>
getPropertyValue
(String name) Retrieves aTry
of the property's value for the given name.<T> io.vavr.control.Option<Property<T>>
removeProperty
(String name) Removes the property with the given name.default void
setProperty
(String name, Property<?> value) Set a value for the property for the given name, independent whether it has been set before.void
setPropertyIfAbsent
(String name, Property<?> value) Sets a value for the property for the given name, if it has not been set before.default void
setPropertyIfAbsent
(String name, Callable<Property<?>> valueGenerator) Sets a value for the property for the given name, if it has not been set before.
-
Method Details
-
getPropertyValue
Retrieves aTry
of the property's value for the given name. TheTry
is considered to fail in case of exceptions or if the property does not exist. Implementations have to ensure that this method is thread-safe.- Type Parameters:
T
- The generic value type.- Parameters:
name
- The name of the property.- Returns:
- The value wrapped in a
Try
. - Throws:
ClassCastException
- If the property cannot be cast to the desired type.
-
setPropertyIfAbsent
void setPropertyIfAbsent(@Nonnull String name, @Nonnull Property<?> value) throws ThreadContextPropertyException Sets a value for the property for the given name, if it has not been set before. Implementations have to ensure that this method is thread-safe.- Parameters:
name
- The name of the property.value
- AProperty
.- Throws:
ThreadContextPropertyException
- If there is an issue while setting the property.
-
setPropertyIfAbsent
default void setPropertyIfAbsent(@Nonnull String name, @Nonnull Callable<Property<?>> valueGenerator) throws ThreadContextPropertyException Sets a value for the property for the given name, if it has not been set before. Implementations have to ensure that this method is thread-safe.- Parameters:
name
- The name of the property.valueGenerator
- ACallable
that returns theProperty
to set- Throws:
ThreadContextPropertyException
- If there is an issue while setting the property or executing the valueGenerator.
-
setProperty
default void setProperty(@Nonnull String name, @Nonnull Property<?> value) throws ThreadContextPropertyException Set a value for the property for the given name, independent whether it has been set before. Implementations have to ensure that this method is thread-safe.- Parameters:
name
- The name of the property.value
- AProperty
.- Throws:
ThreadContextPropertyException
- If there is an issue while setting the property.
-
removeProperty
@Nonnull <T> io.vavr.control.Option<Property<T>> removeProperty(@Nonnull String name) throws ClassCastException Removes the property with the given name.Caution: Implementations may not be thread-safe!
- Type Parameters:
T
- The generic value type.- Parameters:
name
- The name of the property.- Returns:
- A
Option
holding the removed value, orOption.none()
if the property did not exist before. - Throws:
ClassCastException
- If the property cannot be cast to the desired type.
-
containsProperty
Check whether a property with the given name is present and readable.Caution: Implementations may not be thread-safe!
- Parameters:
name
- The name of the property.- Returns:
- A boolean indicating whether the property does exist and is readable.
-
duplicate
Create a newThreadContext
containing all properties of this context. Both the new and the existing context will point to the same property objects. This is useful for passing on a context to a new thread, without the two contexts interfering with each other.Caution: Implementations may not be thread-safe!
- Returns:
- A new instance of
ThreadContext
. - Throws:
ThreadContextAccessException
- If the current implementation does not support the copy operation.
-