Skip to content

Commit

Permalink
class type optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxueli committed Jun 9, 2024
1 parent 69277a6 commit 276f98a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/xxl/tool/pipeline/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ public Response<Object> process(Object request, ConcurrentMap<String, Object> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/xxl/tool/pipeline/PipelineContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PipelineContext {
/**
* pipelint response
*/
private Response<Object> response = ResponseBuilder.newSuccessBuilder().build();
private Response<Object> response = new ResponseBuilder<>().success().build();
/**
* is break or not
*/
Expand Down Expand Up @@ -82,7 +82,7 @@ public String toString() {
* brank pipeline and response fail
*/
public void breakToFail(){
breakToFail(ResponseBuilder.newFailBuilder().build());
breakToFail(new ResponseBuilder<>().fail().build());
}

/**
Expand Down
58 changes: 31 additions & 27 deletions src/main/java/com/xxl/tool/response/ResponseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,54 @@
*
* @author xuxueli 2015-12-4
*/
public class ResponseBuilder {
public class ResponseBuilder<T> {

private Response response;
private final Response<T> response;

public ResponseBuilder() {
this.response = new Response();
this.response = new Response<T>();
}

public ResponseBuilder code(int code) {
// fill data
public ResponseBuilder<T> code(int code) {
response.setCode(code);
return this;
}
public ResponseBuilder msg(String msg) {
public ResponseBuilder<T> msg(String msg) {
response.setMsg(msg);
return this;
}
public ResponseBuilder data(Object data) {
public ResponseBuilder<T> data(T data) {
response.setData(data);
return this;
}

public Response build() {
return response;
// fast fill data
public ResponseBuilder<T> success() {
response.setCode(ResponseCode.CODE_200.getCode());
response.setMsg(ResponseCode.CODE_200.getMsg());
return this;
}
public ResponseBuilder<T> success(T data) {
response.setCode(ResponseCode.CODE_200.getCode());
response.setMsg(ResponseCode.CODE_200.getMsg());
response.setData(data);
return this;
}
public ResponseBuilder<T> fail() {
response.setCode(ResponseCode.CODE_203.getCode());
response.setMsg(ResponseCode.CODE_203.getMsg());
return this;
}
public ResponseBuilder<T> 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<T> build() {
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ResponseBuilderTest {

@Test
public void testResponseBuilder() {
Response response = new ResponseBuilder()
Response<String> response = new ResponseBuilder<String>()
.code(ResponseCode.CODE_200.getCode())
.msg("Sucess")
.data("Hello World")
Expand Down

0 comments on commit 276f98a

Please sign in to comment.