Sub topic 9
Binding several values together
This chapter assumes you can declare a ScopedValue and bind one with where(...).run(...). It also assumes you know what an unbound value is. Here you will bind several values in one scope.
Why bind more than one value
Real requests carry more than one piece of context, such as the current user, a request ID, and a tenant. You could nest one run inside another for each value, but that gets deep quickly. Chaining where calls binds all of them in a single step.
Chaining where calls
ScopedValue.where(key, value) returns a ScopedValue.Carrier. A carrier is an immutable holder of bindings that have not been applied yet. Calling where on a carrier returns a new carrier with one more binding. Nothing is bound until you call run on the carrier.
public class MultiBinding {
static final ScopedValue<String> USER = ScopedValue.newInstance();
static final ScopedValue<Integer> REQUEST_ID = ScopedValue.newInstance();
static final ScopedValue<String> TENANT = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice")
.where(REQUEST_ID, 42)
.where(TENANT, "acme")
.run(MultiBinding::handle);
}
static void handle() {
System.out.println(USER.get() + " #" + REQUEST_ID.get() + " @ " + TENANT.get());
}
}All three values are visible inside handle and everything it calls. When run returns, all three bindings end together.
Reusing a carrier
A carrier is immutable, so you can keep it in a variable and run several tasks with the same bindings.
public class ReusedCarrier {
static final ScopedValue<String> USER = ScopedValue.newInstance();
static final ScopedValue<Integer> REQUEST_ID = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.Carrier ctx = ScopedValue.where(USER, "alice")
.where(REQUEST_ID, 7);
ctx.run(() -> System.out.println("first: " + USER.get()));
ctx.run(() -> System.out.println("second: " + REQUEST_ID.get()));
}
}Each run starts a new scope with the same bindings. Building the carrier does not bind anything by itself.
Combining with nested scopes
Nested scopes still work with several values. An inner where rebinds only the value you name. The other values stay visible.
public class PartialRebind {
static final ScopedValue<String> USER = ScopedValue.newInstance();
static final ScopedValue<Integer> REQUEST_ID = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").where(REQUEST_ID, 1).run(() -> {
ScopedValue.where(USER, "admin").run(() ->
System.out.println(USER.get() + " #" + REQUEST_ID.get())); // admin #1
System.out.println(USER.get() + " #" + REQUEST_ID.get()); // alice #1
});
}
}Common errors
- Forgetting
run. A chain ofwherecalls with norunbinds nothing. Reading the value later throwsNoSuchElementException. - Binding only some of the values you read. If
handlecallsget()on a value that was never bound, that call fails. UseisBoundororElsefor values that are optional. - Expecting mutation. You cannot change a binding inside the scope. To change a value, open a nested scope.
Exercise
You write ScopedValue.where(USER, "alice").where(REQUEST_ID, 5); as a statement and then call USER.get() on the next line. What happens, and how do you fix it?
Show answer
USER.get() throws NoSuchElementException. The chain only builds a Carrier and binds nothing. Fix it by calling .run(...) on the carrier and reading USER.get() inside the lambda.