package com.vtence.molecule;

public interface Middleware extends Application {

    void connectTo(Application successor);
}

@@ -82,45 +82,1832 @@
     
-void connectTo(Application successor);
+// Define a functional interface ...%0A    interface MiddlewareFunction %7B%0A        Application then(Application application);%0A    %7D%0A%0A    // ... so that we can use method references with new our style %60Middleware%60s ...%0A    static Middleware from(MiddlewareFunction middleware) %7B%0A        return new Middleware() %7B%0A            public void handle(Request request, Response response) throws Exception %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A%0A            public void connectTo(Application successor) %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A%0A            public Application then(Application application) %7B%0A                return middleware.then(application);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    // .. until eventually this becomes our new functional interface ...%0A    default Application then(Application next) %7B%0A        connectTo(next);%0A        return this;%0A    %7D%0A%0A    // ... and this is gone.%0A    void connectTo(Application successor);%0A%0A    // Until then support application as functions with this trick%0A    default Application then(ApplicationFunction application) %7B%0A        return then(Application.of(application));%0A    %7D%0A%0A    /**%0A     * Returns a composed middleware that chains this middleware%0A     * with the next.%0A     */%0A    default Middleware then(Middleware next) %7B%0A        return new Middleware() %7B%0A            public void handle(Request request, Response response) throws Exception %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A%0A            public void connectTo(Application successor) %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A%0A            public Application then(Application application) %7B%0A                return Middleware.this.then(next.then(application));%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

Define `Middleware` as a function of `Application` -> `Application`