ApiAssert.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.extension.api;
  17. import java.util.Collection;
  18. import java.util.Map;
  19. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  20. import com.baomidou.mybatisplus.extension.exceptions.ApiException;
  21. import com.baomidou.mybatisplus.extension.toolkit.ObjectUtils;
  22. /**
  23. * <p>
  24. * REST API 业务断言<br>
  25. * 参考:org.junit.Assert
  26. * </p>
  27. *
  28. * @author hubin
  29. * @since 2018-06-05
  30. */
  31. public class ApiAssert {
  32. protected ApiAssert() {
  33. // to do noting
  34. }
  35. /**
  36. * 大于O
  37. */
  38. public static void gtZero(Integer num, IErrorCode errorCode) {
  39. if (num == null || num <= 0) {
  40. fail(errorCode);
  41. }
  42. }
  43. /**
  44. * 大于等于O
  45. */
  46. public static void geZero(Integer num, IErrorCode errorCode) {
  47. if (num == null || num < 0) {
  48. fail(errorCode);
  49. }
  50. }
  51. /**
  52. * num1大于num2
  53. */
  54. public static void gt(Integer num1, Integer num2, IErrorCode errorCode) {
  55. if (num1 <= num2) {
  56. fail(errorCode);
  57. }
  58. }
  59. /**
  60. * num1大于等于num2
  61. */
  62. public static void ge(Integer num1, Integer num2, IErrorCode errorCode) {
  63. if (num1 < num2) {
  64. fail(errorCode);
  65. }
  66. }
  67. /**
  68. * obj1 eq obj2
  69. */
  70. public static void eq(Object obj1, Object obj2, IErrorCode errorCode) {
  71. if (!obj1.equals(obj2)) {
  72. fail(errorCode);
  73. }
  74. }
  75. public static void isTrue(boolean condition, IErrorCode errorCode) {
  76. if (!condition) {
  77. fail(errorCode);
  78. }
  79. }
  80. public static void isFalse(boolean condition, IErrorCode errorCode) {
  81. if (condition) {
  82. fail(errorCode);
  83. }
  84. }
  85. public static void isNull(IErrorCode errorCode, Object... conditions) {
  86. if (ObjectUtils.isNotNull(conditions)) {
  87. fail(errorCode);
  88. }
  89. }
  90. public static void notNull(IErrorCode errorCode, Object... conditions) {
  91. if (ObjectUtils.isNull(conditions)) {
  92. fail(errorCode);
  93. }
  94. }
  95. /**
  96. * <p>
  97. * 失败结果
  98. * </p>
  99. *
  100. * @param errorCode 异常错误码
  101. */
  102. public static void fail(IErrorCode errorCode) {
  103. throw new ApiException(errorCode);
  104. }
  105. public static void fail(boolean condition, IErrorCode errorCode) {
  106. if (condition) {
  107. fail(errorCode);
  108. }
  109. }
  110. public static void fail(String message) {
  111. throw new ApiException(message);
  112. }
  113. public static void fail(boolean condition, String message) {
  114. if (condition) {
  115. fail(message);
  116. }
  117. }
  118. public static void notEmpty(Object[] array, IErrorCode errorCode) {
  119. if (ObjectUtils.isEmpty(array)) {
  120. fail(errorCode);
  121. }
  122. }
  123. public static void noNullElements(Object[] array, IErrorCode errorCode) {
  124. if (array != null) {
  125. for (Object element : array) {
  126. if (element == null) {
  127. fail(errorCode);
  128. }
  129. }
  130. }
  131. }
  132. public static void notEmpty(Collection<?> collection, IErrorCode errorCode) {
  133. if (CollectionUtils.isNotEmpty(collection)) {
  134. fail(errorCode);
  135. }
  136. }
  137. public static void notEmpty(Map<?, ?> map, IErrorCode errorCode) {
  138. if (ObjectUtils.isEmpty(map)) {
  139. fail(errorCode);
  140. }
  141. }
  142. public static void isInstanceOf(Class<?> type, Object obj, IErrorCode errorCode) {
  143. notNull(errorCode, type);
  144. if (!type.isInstance(obj)) {
  145. fail(errorCode);
  146. }
  147. }
  148. public static void isAssignable(Class<?> superType, Class<?> subType, IErrorCode errorCode) {
  149. notNull(errorCode, superType);
  150. if (subType == null || !superType.isAssignableFrom(subType)) {
  151. fail(errorCode);
  152. }
  153. }
  154. }