All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.minlia.rocket.stateful.body.StatefulBody Maven / Gradle / Ivy

There is a newer version: 2.0.4.1.RELEASE
Show newest version
/*
 * Copyright © 2016 Minlia (http://oss.minlia.com/license/framework/2016)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.minlia.rocket.stateful.body;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.minlia.rocket.stateful.generator.SnowFlakeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
import lombok.Data;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * @author will
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "statefulBody", description = "状态化的返回体")
public class StatefulBody implements Body {

  public static final String SUCCESS_MESSAGE = "OK";
  public static final String FAILURE_MESSAGE = "Failure";
  public static final String UNKNOWN_MESSAGE = "Unknown";

  public static final Integer SUCCESS = 1;
  public static final Integer FAILURE = 0;
  public static final Integer UNKNOWN = -1;


  /**
   * 携带的负载返回对象
   */
  @JsonProperty
  @ApiModelProperty(value = "payload", notes = "携带的对象")
  protected T payload;

  /**
   * 当前操作时间
   */
  @JsonProperty
  @ApiModelProperty(value = "timestamp", notes = "当前操作完成的时间戳")
  protected Date timestamp;

  @JsonProperty
  @ApiModelProperty(value = "requestId", notes = "请求ID")
//  protected String requestId = RequestIdGenerator.generateRequestId();
  protected String requestId = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId());

  /**
   * 业务操作完成后的返回代码
   */
  @JsonProperty
  @ApiModelProperty(value = "code", notes = "业务操作完成后的返回代码")
  protected Integer code;

  /**
   * 用于兼容http状态码, 一般在业务上不使用
   */
  @JsonProperty
  @ApiModelProperty(value = "httpStatus", notes = "用于兼容http状态码, 一般在业务上不使用")
  protected Integer status;

  /**
   * 业务操作完成后的返回信息
   */
  @JsonProperty
  @ApiModelProperty(value = "message", notes = "业务操作完成后的返回信息")
  protected String message;

  public StatefulBody() {
    this.timestamp = new Date();
    this.code = UNKNOWN;
    this.message = UNKNOWN_MESSAGE;
    this.status = UNKNOWN;
    this.payload = null;
  }


  public StatefulBody(Integer code, Integer status, String message) {
    this();
    this.timestamp = new Date();
    this.code = code;
    this.status = status;
    this.message = message;
    this.payload = null;
  }


  /**
   * @param code
   * @param status
   * @param message
   * @param payload
   */
  public StatefulBody(Integer code, Integer status, String message, T payload) {
    this();
    this.timestamp = new Date();
    this.code = code;
    this.status = status;
    this.message = message;
    this.payload = payload;
  }


  public Date getTimestamp() {
    return timestamp;
  }

  public Integer getCode() {
    return code;
  }

  public void setCode(Integer code) {
    this.code = code;
  }

  public Integer getStatus() {
    return status;
  }

  public void setStatus(Integer status) {
    this.status = status;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }


  public T getPayload() {
    return payload;
  }

  public void setPayload(T payload) {
    this.payload = payload;
  }


  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this);
  }

  @Override
  public boolean equals(Object o) {
    return EqualsBuilder.reflectionEquals(this, o);
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
  }


  /**
   * 新增便利性方法
   */
  public boolean isSuccess() {
    return null != status && status.equals(SUCCESS);
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy