Package org.scijava

Interface Gateway

  • All Superinterfaces:
    Comparable<Prioritized>, Contextual, Disposable, HasPluginInfo, Identifiable, Locatable, Logged, Prioritized, RichPlugin, SciJavaPlugin, Versioned
    All Known Implementing Classes:
    AbstractGateway, SciJava

    public interface Gateway
    extends RichPlugin, Disposable
    Interface for convenience classes that wrap a Context to provide one-line access to a suite of Services.

    The get(java.lang.Class<S>) methods provide consistent Service instantiation, while throwing NoSuchServiceException if the requested Service is not found.

    Sample implementation

    Let's say we have a Kraken service and a Cow service. Using the Context directly, the code would look like:

     Context context = new Context();
     context.getService(Cow.class).feedToKraken();
     context.getService(Kraken.class).burp();

    To perform these actions, you have to know a priori to ask for a Cow and a Kraken; i.e., your IDE's code completion will not give you a hint. Further, if either service is unavailable, a NullPointerException is thrown.

    But if we create a Gateway class called Animals with the following signatures:

     public Cow cow() { return get(Cow.class); }
     public Kraken kraken() { return get(Kraken.class); }

    We can now access our services through the new Animals gateway:

     Animals animals = new Animals();
     animals.cow().feedToKraken();
     animals.kraken().burp();

    This provides succinct yet explicit access to the Cow and Kraken services; it is a simple two-layer access to functionality, which an IDE can auto-complete. And if one of the services is not available, a NoSuchServiceException is thrown, which facilitates appropriate (but optional) handling of missing services.

    Gateways discoverable at runtime must implement this interface and be annotated with @Gateway with attribute Plugin.type() = Gateway.class. While it possible to create a gateway merely by implementing this interface, it is encouraged to instead extend AbstractGateway, for convenience.

    Author:
    Mark Hiner, Curtis Rueden
    See Also:
    Context