public static final class OD.BindingBuilder<T> extends Object
A BindingBuilder is started by
OD.bind(ClassType)
or OD.Local.bind(ClassType)
,
finished by one of the to(...)
methods, for example,
OD.bind(Foo.class).to(foo); OD.Local.bind(Bar.class).to(BarImpl::new);
The to(...)
method creates a Binding, adds it to
the global or local binding list, depending how the builder was started.
Tags -
By default, the binding exact-matches empty tags.
You can specify exact-matching of tags by tags(Object...)
OD.bind(Foo.class).tags(tag1, tag2).to(foo); ... OD.get(Foo.class, tag1, tag2); // sees `foo`
See also tagsMatch(OD.Predicate)
for arbitrary tag matching algorithm.
Instance Methods | |
---|---|
OD.BindingBuilder<T> |
tags(Object... tags)
Specify exact-matching tags.
|
OD.BindingBuilder<T> |
tagsMatch(OD.Predicate<Object[]> predicate)
Specify how tags will be matched.
|
OD.Binding |
to(T instance)
Create a Binding to a single instance.
|
OD.Binding |
to(OD.Supplier<? extends T> supplier)
Create a Binding to a supplier of T.
|
OD.Binding |
to(Class<? extends T> implClass)
Create a Binding to an implementation class of T.
|
public OD.BindingBuilder<T> tags(Object... tags)
This method is equivalent to tagsMatch(OD.Predicate)
with a predicate that matches exactly the given `tags`.
Exact-match of two objects are defined by Object.equals(Object)
;
also, `null` exact-matches `null`.
public OD.BindingBuilder<T> tagsMatch(OD.Predicate<Object[]> predicate)
The tags in `OD.get(type,tags)` will be fed to `predicate` to check whether they match this binding. For example, to specify that the binding matches any lookup tags
OD.bind(Foo.class).tagsMatch(tags->true).to(foo);
public OD.Binding to(T instance)
This method is equivalent to to(OD.Supplier)
with a supplier that always returns `instance`.
public OD.Binding to(OD.Supplier<? extends T> supplier)
The supplier will be invoke for every `OD.get()` call that matches this binding. For example,
OD.bind(Foo.class).to(FooImpl::new);
public OD.Binding to(Class<? extends T> implClass)
In simple cases, the zero-arg constructor of `implClass` will be invoked for each `OD.get()`. For example
OD.bind(Foo.class).to(FooImpl.class); OD.get(Foo.class); // returns `new FooImpl()`
Type Argument
If `implClass` is generic, it might want to know the exact type arguments upon instantiation. This can be done by a constructor that accepts type arguments. For example
public class FooImpl<T> implements Foo<T> { public FooImpl(Class<T> classT) { ...
If we `bind(Foo.class).to(FooImpl.class)`, the binding will match a lookup for `Foo<Bar>`, and the constructor `FooImpl(Class)` will be invoked with `Bar` class.
More generally,
suppose `implClass` has n type parameters <T1, ..., Tn>,
the constructor can have any number of arguments; each argument type must be one of
Class<X>
,
ClassType<X>
,
or
ReferenceType<X>
,
where `X` must be one of T1...Tn. For example,
public class BarImpl<K, V> implements Bar<Map<K, List<V>>> { public BarImpl(ClassType<V> typeV, Class<K> clazzK)