Skip to content

Instantly share code, notes, and snippets.

@mstruk
Created November 14, 2012 20:24
Show Gist options
  • Save mstruk/4074544 to your computer and use it in GitHub Desktop.
Save mstruk/4074544 to your computer and use it in GitHub Desktop.
PipeConfig Ergonomics

Currently, when creating a new PipeConfig we use a constructor and specify a rootUrl:

    URL rootUrl = new URL("http://todo-aerogear.rhcloud.com/todo-server");

    Pipeline pipeline = new Pipeline(rootUrl);
    PipeConfig config = new PipeConfig(rootUrl, Project.class);

    Pipe<Project> projects = pipeline.pipe(Project.class, config);

We are specifying rootUrl twice which feels weird, as Pipeline is supposed to be a factory object, and we set rootUrl on it already. We need a way for PipeConfig to inherit that information.

One way would be like this:

    URL rootUrl = new URL("http://todo-aerogear.rhcloud.com/todo-server");

    Pipeline pipeline = new Pipeline(rootUrl);
    PipeConfig config = new PipeConfig(pipeline);
    config.setEntityClass(Project.class);

    Pipe<Project> projects = pipeline.pipe(Project.class, config);

But if we take a look at what the passed Class object really represents:

PipeConfig.java:

    public PipeConfig(URL baseURL, Class klass) {
        this.baseURL = baseURL;
        this.name = klass.getSimpleName().toLowerCase();
        this.endpoint = name;
        this.type = PipeTypes.REST;
    }

We see it's only used for setting default value for name and endpoint. That however can be determined by Pipe itself. So we can move that to the Pipe, and we now only have to do:

    URL rootUrl = new URL("http://todo-aerogear.rhcloud.com/todo-server");

    Pipeline pipeline = new Pipeline(rootUrl);
    PipeConfig config = new PipeConfig(pipeline);
    
    Pipe<Project> projects = pipeline.pipe(Project.class, config);

That's nicer.

Actually, pipe will be registered with the Pipeline, and at that point all this information will be available, so there's no need to pass pipeline either:

    URL rootUrl = new URL("http://todo-aerogear.rhcloud.com/todo-server");

    Pipeline pipeline = new Pipeline(rootUrl);
    PipeConfig config = new PipeConfig();

    Pipe<Project> projects = pipeline.pipe(Project.class, config);

That's even nicer for getting the same result. But if this works, why not just go a step further, and just do this:

    URL rootUrl = new URL("http://todo-aerogear.rhcloud.com/todo-server");

    Pipeline pipeline = new Pipeline(rootUrl);

    Pipe<Project> projects = pipeline.pipe(Project.class);

We get a nice API for most common use case.

WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment