public interface Registry
Registries are primarily used for inter Handler
communication in request processing.
The Context
object that handlers operate on implements the Registry
interface.
import ratpack.handling.Handler; import ratpack.handling.Context; import ratpack.registry.Registry; import static ratpack.registry.Registries.just; public class Thing { private final String name public Thing(String name) { this.name = name; } public String getName() { return name; } } public class UpstreamHandler implements Handler { public void handle(Context context) { context.next(just(new Thing("foo"))); } } public class DownstreamHandler implements Handler { public void handle(Context context) { assert context instanceof Registry; Thing thing = context.get(Thing.class); context.render(thing.getName()); } } import ratpack.test.handling.HandlingResult; import ratpack.test.handling.RequestFixture; import static ratpack.test.UnitTest.handle; import static ratpack.handling.Handlers.chain; import static ratpack.func.Actions.noop; Handler chain = chain(new UpstreamHandler(), new DownstreamHandler()); HandlingResult result = handle(chain, noop()); assert result.rendered(String.class).equals("foo");
Registry objects are assumed to be thread safe. No external synchronization is performed around registry access. As registry objects may be used across multiple requests, they should be thread safe.
Registries that are created per request however do not need to be thread safe.
Modifier and Type | Method and Description |
---|---|
<T> Iterable<? extends T> |
all(TypeToken<T> type,
Predicate<? super T> predicate)
Returns all of the objects whose declared type is assignment compatible with the given type and who satisfy the given predicate.
|
<T> boolean |
each(TypeToken<T> type,
Predicate<? super T> predicate,
Action<? super T> action)
Calls the given action with each object whose declared type is assignment compatible with the given type and who satisfies the given predicate.
|
<T> T |
first(TypeToken<T> type,
Predicate<? super T> predicate)
Returns the first object whose declared type is assignment compatible with the given type and who satisfies the given predicate.
|
<O> O |
get(Class<O> type)
Provides an object of the specified type, or throws an exception if no object of that type is available.
|
<O> O |
get(TypeToken<O> type)
Provides an object of the specified type, or throws an exception if no object of that type is available.
|
<O> Iterable<? extends O> |
getAll(Class<O> type)
Returns all of the objects whose declared type is assignment compatible with the given type.
|
<O> Iterable<? extends O> |
getAll(TypeToken<O> type)
Returns all of the objects whose declared type is assignment compatible with the given type.
|
<O> O |
maybeGet(Class<O> type)
Does the same thing as
get(Class) , except returns null instead of throwing an exception. |
<O> O |
maybeGet(TypeToken<O> type)
Does the same thing as
get(Class) , except returns null instead of throwing an exception. |
<O> O get(Class<O> type) throws NotInRegistryException
O
- The type of the object to providetype
- The type of the object to provideNotInRegistryException
- If no object of this type can be returned<O> O get(TypeToken<O> type) throws NotInRegistryException
O
- The type of the object to providetype
- The type of the object to provideNotInRegistryException
- If no object of this type can be returned@Nullable <O> O maybeGet(Class<O> type)
get(Class)
, except returns null instead of throwing an exception.O
- The type of the object to providetype
- The type of the object to provide@Nullable <O> O maybeGet(TypeToken<O> type)
get(Class)
, except returns null instead of throwing an exception.O
- The type of the object to providetype
- The type of the object to provide<O> Iterable<? extends O> getAll(Class<O> type)
O
- the type of objects to search fortype
- the type of objects to search for<O> Iterable<? extends O> getAll(TypeToken<O> type)
O
- the type of objects to search fortype
- the type of objects to search for@Nullable <T> T first(TypeToken<T> type, Predicate<? super T> predicate)
T
- the type of the object to search fortype
- the type of object to search forpredicate
- a predicate to check objects against<T> Iterable<? extends T> all(TypeToken<T> type, Predicate<? super T> predicate)
T
- the type of objects to search fortype
- the type of objects to search forpredicate
- a predicate to check objects against<T> boolean each(TypeToken<T> type, Predicate<? super T> predicate, Action<? super T> action) throws Exception
T
- the type of object to search fortype
- the type of object to search forpredicate
- a predicate to check objects againstaction
- an action to call with each matching objectException
- any thrown by action