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; import static org.junit.Assert.assertTrue; 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) { assertTrue(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.handling.Handlers.chain; import static ratpack.func.Action.noop; import static org.junit.Assert.assertEquals; Handler chain = chain(new UpstreamHandler(), new DownstreamHandler()); HandlingResult result = RequestFixture.handle(chain, noop()); assertEquals("foo", result.rendered(String.class));
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> Optional<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.
|
default <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.
|
default <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.
|
default <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.
|
default Registry |
join(Registry child)
Creates a new registry by joining
this registry with the given registry |
default <O> Optional<O> |
maybeGet(Class<O> type)
Does the same thing as
get(Class) , except returns null instead of throwing an exception. |
<O> Optional<O> |
maybeGet(TypeToken<O> type)
Does the same thing as
get(Class) , except returns null instead of throwing an exception. |
default <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 returneddefault <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 returneddefault <O> Optional<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<O> Optional<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 providedefault <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<T> Optional<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
default Registry join(Registry child)
this
registry with the given registry
The returned registry is effectively the union of the two registries, with the child
registry taking precedence.
This means that child entries are effectively “returned first”.
import ratpack.registry.Registry;
import static ratpack.registry.Registries.registry;
import java.util.List;
import com.google.common.collect.Lists;
import static org.junit.Assert.assertEquals;
public class Example {
public static interface Thing {
String getName();
}
public static class ThingImpl implements Thing {
private final String name;
public ThingImpl(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
Registry child = registry().add(Thing.class, new ThingImpl("child-1")).add(Thing.class, new ThingImpl("child-2")).build();
Registry parent = registry().add(Thing.class, new ThingImpl("parent-1")).add(Thing.class, new ThingImpl("parent-2")).build();
Registry joined = parent.join(child);
assertEquals("child-2", joined.get(Thing.class).getName());
List<Thing> all = Lists.newArrayList(joined.getAll(Thing.class));
assertEquals("child-2", all.get(0).getName());
assertEquals("child-1", all.get(1).getName());
assertEquals("parent-2", all.get(2).getName());
assertEquals("parent-1", all.get(3).getName());
}
}
child
- the child registrythis
and the given child