Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Created October 7, 2016 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3coder/511089573b355dd5d646a07b37de54dc to your computer and use it in GitHub Desktop.
Save dmi3coder/511089573b355dd5d646a07b37de54dc to your computer and use it in GitHub Desktop.
Abstract Rest service response factory
package com.dmi3coder.scorsero.rest;
import java.util.ArrayList;
import java.util.List;
public class ResponseFactory<T> {
private List<T> data;
private ResponseInfo info;
public enum ResponseInfo{
OK(200,"OK");
private final int code;
private final String description;
ResponseInfo(int code, String description) {
this.code = code;
this.description = description;
}
}
public static <G> ResponseFactory<G> create(List<G> data){
ResponseFactory<G> factory = new ResponseFactory<G>();
factory.data = data;
return factory;
}
public static <G> ResponseFactory<G> create(G data){
List<G> list = new ArrayList<G>(1);
list.add(data);
return create(list);
}
public ResponseFactory<T> withResponse(ResponseInfo info){
this.info = info;
return this;
}
public ResponseItem<T> build(){
return new ResponseItem<T>(info.code,info.description,data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment