diff --git a/src/main/java/com/xxl/tool/pipeline/Pipeline.java b/src/main/java/com/xxl/tool/pipeline/Pipeline.java index dd3c54b..31fbbf5 100644 --- a/src/main/java/com/xxl/tool/pipeline/Pipeline.java +++ b/src/main/java/com/xxl/tool/pipeline/Pipeline.java @@ -122,14 +122,14 @@ public Response process(Object request, ConcurrentMap co handlerChain.handle(pipelineContext); } catch (Exception e) { logger.error("pipeline process error, name:{}, pipelineContext:{}", name, pipelineContext, e); - pipelineContext.breakToFail(ResponseBuilder.newFailBuilder().msg(e.getMessage()).build()); + pipelineContext.breakToFail(new ResponseBuilder<>().fail(e.getMessage()).build()); //throw new RuntimeException(e); } } // valid response if (pipelineContext.getResponse() == null) { - pipelineContext.breakToFail(ResponseBuilder.newFailBuilder().msg("pipeline response not found.").build()); + pipelineContext.breakToFail(new ResponseBuilder<>().fail("pipeline response not found.").build()); } logger.error("pipeline process end, name:{}, pipelineContext:{}", name, pipelineContext); diff --git a/src/main/java/com/xxl/tool/pipeline/PipelineContext.java b/src/main/java/com/xxl/tool/pipeline/PipelineContext.java index 8b62bbb..1b83f9e 100644 --- a/src/main/java/com/xxl/tool/pipeline/PipelineContext.java +++ b/src/main/java/com/xxl/tool/pipeline/PipelineContext.java @@ -20,7 +20,7 @@ public class PipelineContext { /** * pipelint response */ - private Response response = ResponseBuilder.newSuccessBuilder().build(); + private Response response = new ResponseBuilder<>().success().build(); /** * is break or not */ @@ -82,7 +82,7 @@ public String toString() { * brank pipeline and response fail */ public void breakToFail(){ - breakToFail(ResponseBuilder.newFailBuilder().build()); + breakToFail(new ResponseBuilder<>().fail().build()); } /** diff --git a/src/main/java/com/xxl/tool/response/ResponseBuilder.java b/src/main/java/com/xxl/tool/response/ResponseBuilder.java index 94bf09f..f7abb0b 100644 --- a/src/main/java/com/xxl/tool/response/ResponseBuilder.java +++ b/src/main/java/com/xxl/tool/response/ResponseBuilder.java @@ -5,50 +5,54 @@ * * @author xuxueli 2015-12-4 */ -public class ResponseBuilder { +public class ResponseBuilder { - private Response response; + private final Response response; public ResponseBuilder() { - this.response = new Response(); + this.response = new Response(); } - public ResponseBuilder code(int code) { + // fill data + public ResponseBuilder code(int code) { response.setCode(code); return this; } - public ResponseBuilder msg(String msg) { + public ResponseBuilder msg(String msg) { response.setMsg(msg); return this; } - public ResponseBuilder data(Object data) { + public ResponseBuilder data(T data) { response.setData(data); return this; } - public Response build() { - return response; + // fast fill data + public ResponseBuilder success() { + response.setCode(ResponseCode.CODE_200.getCode()); + response.setMsg(ResponseCode.CODE_200.getMsg()); + return this; + } + public ResponseBuilder success(T data) { + response.setCode(ResponseCode.CODE_200.getCode()); + response.setMsg(ResponseCode.CODE_200.getMsg()); + response.setData(data); + return this; + } + public ResponseBuilder fail() { + response.setCode(ResponseCode.CODE_203.getCode()); + response.setMsg(ResponseCode.CODE_203.getMsg()); + return this; + } + public ResponseBuilder fail(String msg) { + response.setCode(ResponseCode.CODE_203.getCode()); + response.setMsg(msg); + return this; } - - public static ResponseBuilder newBuilder(){ - return new ResponseBuilder(); - } - public static ResponseBuilder newSuccessBuilder(){ - return new ResponseBuilder() - .code(ResponseCode.CODE_200.getCode()) - .msg(ResponseCode.CODE_200.getMsg()); - } - public static ResponseBuilder newSuccessBuilder(Object data){ - return new ResponseBuilder() - .code(ResponseCode.CODE_200.getCode()) - .msg(ResponseCode.CODE_200.getMsg()) - .data(data); - } - public static ResponseBuilder newFailBuilder(){ - return new ResponseBuilder() - .code(ResponseCode.CODE_203.getCode()) - .msg(ResponseCode.CODE_203.getMsg()); + // build + public Response build() { + return response; } } diff --git a/src/test/java/com/xxl/tool/test/response/ResponseBuilderTest.java b/src/test/java/com/xxl/tool/test/response/ResponseBuilderTest.java index 8d4432a..caf20f5 100644 --- a/src/test/java/com/xxl/tool/test/response/ResponseBuilderTest.java +++ b/src/test/java/com/xxl/tool/test/response/ResponseBuilderTest.java @@ -13,7 +13,7 @@ public class ResponseBuilderTest { @Test public void testResponseBuilder() { - Response response = new ResponseBuilder() + Response response = new ResponseBuilder() .code(ResponseCode.CODE_200.getCode()) .msg("Sucess") .data("Hello World")