Builder for Pipe creation
// http://myServer/myProject/foo
Pipe<Foo> fooPipe = pipeline.pipe(Foo.class).buildAndAdd();
// http://myServer/myProject/my-crazy-endpoint
Pipe<Bar> barPipe = pipeline.pipe(Bar.class)
.endpoint("my-crazy-endpoint")
.name("bad name")
.buildAndAdd();
public class Pipeline {
private URL baseURL;
public Pipeline(URL baseURL) {
this.baseURL = baseURL;
}
public PipeBuilder pipe(Class klass) {
return new BuilderImpl(klass, baseURL);
}
private final class BuilderImpl implements PipeBuilder {
private final Class klass;
private URL url;
private String name;
private String endpoint;
private Type type = REST;
public BuilderImpl(Class klass, URL baseURL) {
this.klass = klass;
this.url = baseURL;
this.name = klass.getSimpleName().toLowerCase();
this.endpoint = name;
}
@Override
public PipeBuilder name(String name) {
this.name = name;
return this;
}
@Override
public PipeBuilder endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
@Override
public PipeBuilder type(Type type) {
this.type = type;
return this;
}
@Override
public PipeBuilder url(URL url) {
this.url = url;
return this;
}
@Override
public Pipe buildAndAdd() {
Pipe pipe = AdapterFactory.createPipe(type, klass, appendEndpoint(url, endpoint));
pipes.put(name, pipe);
return pipe;
}
private URL appendEndpoint(URL baseURL, String endpoint) {
try {
if( !baseURL.toString().endsWith("/")) {
endpoint = "/" + endpoint;
}
return new URL(baseURL + endpoint + "/");
} catch (MalformedURLException e) {
Log.e("AeroGear", e.getMessage());
return null;
}
}
}
public static interface PipeBuilder {
public PipeBuilder name(String name);
public PipeBuilder endpoint(String endpoint);
public PipeBuilder type(Type type);
public PipeBuilder url(URL url);
public Pipe buildAndAdd();
}
}