Skip to content

Instantly share code, notes, and snippets.

@secondsun
Forked from mstruk/gist:4074544
Created November 15, 2012 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secondsun/4078711 to your computer and use it in GitHub Desktop.
Save secondsun/4078711 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. That information should be on PipeConfig.

One way would be like this:

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

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

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

But now, because of type safetype, we are passing in Project.class and PipeConfig together. How about we make PipeConfig a generic type? And add a new field. PipeConfig.java:

public class PipeConfig<T> { 

    //old fields
    private final Class<T> entityClass;

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

Now creating a Pipe looks like:

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

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

That's nicer.

And we can also have, as a convenience method:

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

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

WDYT?

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