ValueHolder.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.boman.system.common;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.Data;
  4. import java.io.Serializable;
  5. /**
  6. * @author shiqian
  7. * @description
  8. * @date 2021年03月22日 09:26
  9. **/
  10. @Data
  11. public class ValueHolder<T> implements Serializable {
  12. public static final int OK = 0;
  13. public static final int FAIL = -1;
  14. private T data;
  15. private int code;
  16. private String message;
  17. public ValueHolder() {
  18. }
  19. public ValueHolder(int code, String message, T data) {
  20. this.data = data;
  21. this.code = code;
  22. this.message = message;
  23. }
  24. public ValueHolder(int code, String message) {
  25. this.code = code;
  26. this.message = message;
  27. }
  28. public JSONObject toJSONObject() {
  29. JSONObject obj = new JSONObject();
  30. obj.put("code", this.code);
  31. obj.put("message", this.message);
  32. obj.put("data", this.data);
  33. return obj;
  34. }
  35. public static <T> ValueHolder<T> ok(String message){
  36. return new ValueHolder<>(OK, message);
  37. }
  38. public static <T> ValueHolder<T> ok(String message, T data){
  39. return new ValueHolder<>(OK, message, data);
  40. }
  41. public static <T> ValueHolder<T> ok(T data){
  42. return new ValueHolder<>(OK, "成功", data);
  43. }
  44. public static <T> ValueHolder<T> fail(String message, T data){
  45. return new ValueHolder<>(FAIL, message, data);
  46. }
  47. public static <T> ValueHolder<T> fail(String message){
  48. return new ValueHolder<>(FAIL, message);
  49. }
  50. public boolean isOK() {
  51. return this.code == 0;
  52. }
  53. public String toDebugString() {
  54. return this.toJSONObject().toString();
  55. }
  56. public T getData() {
  57. return this.data;
  58. }
  59. public int getCode() {
  60. return this.code;
  61. }
  62. public String getMessage() {
  63. return this.message;
  64. }
  65. public void setData(final T data) {
  66. this.data = data;
  67. }
  68. public void setCode(final int code) {
  69. this.code = code;
  70. }
  71. public void setMessage(final String message) {
  72. this.message = message;
  73. }
  74. @Override
  75. public boolean equals(final Object o) {
  76. if (o == this) {
  77. return true;
  78. } else if (!(o instanceof ValueHolder)) {
  79. return false;
  80. } else {
  81. ValueHolder<?> other = (ValueHolder)o;
  82. if (!other.canEqual(this)) {
  83. return false;
  84. } else {
  85. label39: {
  86. Object this$data = this.getData();
  87. Object other$data = other.getData();
  88. if (this$data == null) {
  89. if (other$data == null) {
  90. break label39;
  91. }
  92. } else if (this$data.equals(other$data)) {
  93. break label39;
  94. }
  95. return false;
  96. }
  97. if (this.getCode() != other.getCode()) {
  98. return false;
  99. } else {
  100. Object this$message = this.getMessage();
  101. Object other$message = other.getMessage();
  102. if (this$message == null) {
  103. return other$message == null;
  104. } else return this$message.equals(other$message);
  105. }
  106. }
  107. }
  108. }
  109. protected boolean canEqual(final Object other) {
  110. return other instanceof ValueHolder;
  111. }
  112. @Override
  113. public int hashCode() {
  114. int result = 1;
  115. Object $data = this.getData();
  116. result = result * 59 + ($data == null ? 43 : $data.hashCode());
  117. result = result * 59 + this.getCode();
  118. Object $message = this.getMessage();
  119. result = result * 59 + ($message == null ? 43 : $message.hashCode());
  120. return result;
  121. }
  122. @Override
  123. public String toString() {
  124. return "ValueHolder(data=" + this.getData() + ", code=" + this.getCode() + ", message=" + this.getMessage() + ")";
  125. }
  126. }