public static class OD.Local extends Object
This class contains static methods for managing thread-local bindings.
Each thread contains a local binding list; the local bindings are always looked up before the global bindings.
Examples:
OD.Local.bind(Foo.class).to(foo1); ... OD.Local.bind(Foo.class).to(foo2); // later in the same thread OD.get(Foo.class) // sees `foo2`
Static Methods | |
---|---|
<T> OD.BindingBuilder<T> |
bind(Class<T> clazz)
To add a local binding.
|
<T> OD.BindingBuilder<T> |
bind(ClassType<T> type)
To add a local binding.
|
void |
bind(OD.Binding binding)
Add the binding to the local binding list of the current thread.
|
List<OD.Binding> |
getBindings()
Get the local binding list of the current thread.
|
void |
setBindings(List<OD.Binding> bindings)
Set the local binding list for the current thread.
|
public static <T> OD.BindingBuilder<T> bind(Class<T> clazz)
This method is equivalent to bind(ClassType)
by wrapping the `Class` as `ClassType`; see ClassType.of(Class)
.
public static <T> OD.BindingBuilder<T> bind(ClassType<T> type)
This method return a builder; when the builder finishes, a binding is created and added to the local binding list of the current thread.
Example usage:
OD.Local.bind(Foo.class).to(foo);
bind(Class)
public static void bind(OD.Binding binding)
public static List<OD.Binding> getBindings()
The returned list is immutable. Typically it will be used later in
setBindings(List)
. For example
List<Binding> b0 = OD.Local.getBindings(); // save local bindings try { ... OD.Local.bind(Foo.class).to(foo); // change local bindings ... } finally { OD.Local.setBindings(b0); // restore local bindings }
It is ok to get the binding list from one thread and set it to another thread; this is useful for migrating a task and its context between threads.
public static void setBindings(List<OD.Binding> bindings)
The list is typically from a previous getBindings()
call;
but it can also be any list of bindings.
If `bindings` is null or empty, the local bindings of the current thread will be cleared.