Created
October 7, 2016 21:55
-
-
Save dmi3coder/511089573b355dd5d646a07b37de54dc to your computer and use it in GitHub Desktop.
Abstract Rest service response factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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