Migrate Changes#query and Projects#list to a more fluent interface

This syntax allows more compact userland code.
Thanks to Dave Borowitz for this suggestion.

Example:
  changes.query("is:open")
    .withLimit(5)
    .get();

Change-Id: I676e58c5d50c5599908df19f9384cc1e6d2aed8e
This commit is contained in:
Urs Wolfer
2014-05-20 20:05:29 +02:00
committed by Dave Borowitz
parent fa06730419
commit ebf130e5a2
2 changed files with 21 additions and 37 deletions

View File

@@ -30,49 +30,43 @@ public interface Changes {
throws RestApiException;
ChangeApi create(ChangeInfo in) throws RestApiException;
/**
* Shorthand for {@link #query(QueryParameter)} without any conditions (i.e. lists all changes).
*/
List<ChangeInfo> query() throws RestApiException;
List<ChangeInfo> query(QueryParameter queryParameter) throws RestApiException;
QueryRequest query();
QueryRequest query(String query);
public class QueryParameter {
public abstract class QueryRequest {
private String query;
private int limit;
private int start;
private EnumSet<ListChangesOption> options = EnumSet.noneOf(ListChangesOption.class);
public QueryParameter() {}
public abstract List<ChangeInfo> get() throws RestApiException;
public QueryParameter(String query) {
this.query = query;
}
public QueryParameter withQuery(String query) {
public QueryRequest withQuery(String query) {
this.query = query;
return this;
}
public QueryParameter withLimit(int limit) {
public QueryRequest withLimit(int limit) {
this.limit = limit;
return this;
}
public QueryParameter withStart(int start) {
public QueryRequest withStart(int start) {
this.start = start;
return this;
}
public QueryParameter withOption(ListChangesOption options) {
public QueryRequest withOption(ListChangesOption options) {
this.options.add(options);
return this;
}
public QueryParameter withOptions(ListChangesOption... options) {
public QueryRequest withOptions(ListChangesOption... options) {
this.options.addAll(Arrays.asList(options));
return this;
}
public QueryParameter withOptions(EnumSet<ListChangesOption> options) {
public QueryRequest withOptions(EnumSet<ListChangesOption> options) {
this.options = options;
return this;
}
@@ -120,12 +114,12 @@ public interface Changes {
}
@Override
public List<ChangeInfo> query() throws RestApiException {
public QueryRequest query() {
throw new NotImplementedException();
}
@Override
public List<ChangeInfo> query(QueryParameter queryParameter) throws RestApiException {
public QueryRequest query(String query) {
throw new NotImplementedException();
}
}

View File

@@ -23,37 +23,32 @@ import java.util.List;
public interface Projects {
ProjectApi name(String name) throws RestApiException;
List<ProjectInfo> list() throws RestApiException;
List<ProjectInfo> list(ListParameter listParameter) throws RestApiException;
ListRequest list();
public class ListParameter {
public abstract class ListRequest {
private boolean description;
private String prefix;
private int limit;
private int start;
public ListParameter() {}
public abstract List<ProjectInfo> get() throws RestApiException;
public ListParameter(String prefix) {
this.prefix = prefix;
}
public ListParameter withDescription(boolean description) {
public ListRequest withDescription(boolean description) {
this.description = description;
return this;
}
public ListParameter withPrefix(String prefix) {
public ListRequest withPrefix(String prefix) {
this.prefix = prefix;
return this;
}
public ListParameter withLimit(int limit) {
public ListRequest withLimit(int limit) {
this.limit = limit;
return this;
}
public ListParameter withStart(int start) {
public ListRequest withStart(int start) {
this.start = start;
return this;
}
@@ -86,12 +81,7 @@ public interface Projects {
}
@Override
public List<ProjectInfo> list() throws RestApiException {
throw new NotImplementedException();
}
@Override
public List<ProjectInfo> list(ListParameter listParameter) throws RestApiException {
public ListRequest list() {
throw new NotImplementedException();
}
}