Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jan 9, 2024
1 parent e7435cc commit 0abeaf3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/vertx/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,24 @@ default List<String> processArgs() {
*
* @param key the key of the data
* @param <T> the type of the data
* @return the data
* @return the local data
*/
@GenIgnore
<T> T getLocal(ContextKey<T> key);

/**
* Get some local data from the context.
* Get some local data from the context, when it does not exist the {@code initialValueSupplier} is called to obtain
* the initial value.
*
* <p> The {@code initialValueSupplier} might be called multiple times when multiple threads call this method concurrently.
*
* @param key the key of the data
* @param initialValueSupplier the supplier of the initial value optionally called
* @param <T> the type of the data
* @return the data
* @return the local data
*/
@GenIgnore
<T> T getLocal(ContextKey<T> key, Supplier<? extends T> supplier);
<T> T getLocal(ContextKey<T> key, Supplier<? extends T> initialValueSupplier);

/**
* Put some local data in the context.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/vertx/core/impl/ContextBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public final <T> T getLocal(ContextKey<T> key) {
return (T) res;
}

public final <T> T getLocal(ContextKey<T> key, Supplier<? extends T> supplier) {
public final <T> T getLocal(ContextKey<T> key, Supplier<? extends T> initialValueSupplier) {
ContextKeyImpl<T> internalKey = (ContextKeyImpl<T>) key;
int index = internalKey.index;
if (index >= locals.length) {
throw new IllegalArgumentException();
throw new IllegalArgumentException("Invalid key index: " + index);
}
Object res;
while (true) {
res = LOCALS_UPDATER.getVolatile(locals, index);
if (res != null) {
break;
}
Object initial = supplier.get();
Object initial = initialValueSupplier.get();
if (initial == null) {
throw new IllegalStateException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/core/FakeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public <T> void putLocal(ContextKey<T> key, T value) {
}

@Override
public <T> T getLocal(ContextKey<T> key, Supplier<? extends T> supplier) {
public <T> T getLocal(ContextKey<T> key, Supplier<? extends T> initialValueSupplier) {
return null;
}
}

0 comments on commit 0abeaf3

Please sign in to comment.