|
13 | 13 |
|
14 | 14 | import io.netty.channel.EventLoop; |
15 | 15 | import io.vertx.core.*; |
| 16 | +import io.vertx.core.Future; |
16 | 17 | import io.vertx.core.impl.future.FailedFuture; |
17 | 18 | import io.vertx.core.impl.future.PromiseImpl; |
18 | 19 | import io.vertx.core.impl.future.PromiseInternal; |
19 | 20 | import io.vertx.core.impl.future.SucceededFuture; |
| 21 | +import io.vertx.core.spi.context.storage.AccessMode; |
| 22 | +import io.vertx.core.spi.context.storage.ContextLocal; |
20 | 23 | import io.vertx.core.spi.tracing.VertxTracer; |
21 | 24 |
|
22 | | -import java.util.concurrent.Callable; |
23 | | -import java.util.concurrent.ConcurrentMap; |
24 | | -import java.util.concurrent.Executor; |
25 | | -import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.*; |
| 26 | +import java.util.function.Supplier; |
26 | 27 |
|
27 | 28 | /** |
28 | 29 | * This interface provides an api for vert.x core internal use only |
|
33 | 34 | */ |
34 | 35 | public interface ContextInternal extends Context { |
35 | 36 |
|
| 37 | + ContextLocal<ConcurrentMap<Object, Object>> LOCAL_MAP = new ContextLocalImpl<>(0); |
| 38 | + |
36 | 39 | /** |
37 | 40 | * @return the current context |
38 | 41 | */ |
@@ -304,19 +307,76 @@ default boolean remove(Object key) { |
304 | 307 | /** |
305 | 308 | * @return the {@link ConcurrentMap} used to store local context data |
306 | 309 | */ |
307 | | - ConcurrentMap<Object, Object> localContextData(); |
| 310 | + default ConcurrentMap<Object, Object> localContextData() { |
| 311 | + return LOCAL_MAP.get(this, ConcurrentHashMap::new); |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * Get some local data from the context. |
| 316 | + * |
| 317 | + * @param key the key of the data |
| 318 | + * @param <T> the type of the data |
| 319 | + * @return the local data |
| 320 | + */ |
| 321 | + default <T> T getLocal(ContextLocal<T> key) { |
| 322 | + return getLocal(key, AccessMode.CONCURRENT); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Get some local data from the context. |
| 327 | + * |
| 328 | + * @param key the key of the data |
| 329 | + * @param <T> the type of the data |
| 330 | + * @return the local data |
| 331 | + */ |
| 332 | + <T> T getLocal(ContextLocal<T> key, AccessMode accessMode); |
308 | 333 |
|
| 334 | + /** |
| 335 | + * Get some local data from the context, when it does not exist the {@code initialValueSupplier} is called to obtain |
| 336 | + * the initial value. |
| 337 | + * |
| 338 | + * <p> The {@code initialValueSupplier} might be called multiple times when multiple threads call this method concurrently. |
| 339 | + * |
| 340 | + * @param key the key of the data |
| 341 | + * @param initialValueSupplier the supplier of the initial value optionally called |
| 342 | + * @param <T> the type of the data |
| 343 | + * @return the local data |
| 344 | + */ |
| 345 | + <T> T getLocal(ContextLocal<T> key, AccessMode accessMode, Supplier<? extends T> initialValueSupplier); |
| 346 | + |
| 347 | + /** |
| 348 | + * Put some local data in the context. |
| 349 | + * <p> |
| 350 | + * This can be used to share data between different handlers that share a context |
| 351 | + * |
| 352 | + * @param key the key of the data |
| 353 | + * @param value the data |
| 354 | + */ |
| 355 | + <T> void putLocal(ContextLocal<T> key, AccessMode accessMode, T value); |
| 356 | + |
| 357 | + /** |
| 358 | + * Remove some local data from the context. |
| 359 | + * |
| 360 | + * @param key the key to remove |
| 361 | + */ |
| 362 | + default <T> void removeLocal(ContextLocal<T> key, AccessMode accessMode) { |
| 363 | + putLocal(key, accessMode, null); |
| 364 | + } |
| 365 | + |
| 366 | + @Deprecated |
309 | 367 | @SuppressWarnings("unchecked") |
310 | 368 | @Override |
311 | 369 | default <T> T getLocal(Object key) { |
312 | 370 | return (T) localContextData().get(key); |
313 | 371 | } |
314 | 372 |
|
| 373 | + @Deprecated |
315 | 374 | @Override |
316 | 375 | default void putLocal(Object key, Object value) { |
317 | 376 | localContextData().put(key, value); |
318 | 377 | } |
319 | 378 |
|
| 379 | + @Deprecated |
320 | 380 | @Override |
321 | 381 | default boolean removeLocal(Object key) { |
322 | 382 | return localContextData().remove(key) != null; |
@@ -445,4 +505,5 @@ default ContextInternal unwrap() { |
445 | 505 | default boolean isDuplicate() { |
446 | 506 | return false; |
447 | 507 | } |
| 508 | + |
448 | 509 | } |
0 commit comments