Assert.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.punchsettle.server.common.utils;
  2. import java.util.Collection;
  3. import java.util.Objects;
  4. import com.punchsettle.server.common.exception.BusinessException;
  5. /**
  6. * @author tyuio
  7. * @version 1.0.0
  8. * @description 断言工具类
  9. * @date 2024/11/26 19:43
  10. */
  11. public class Assert {
  12. /**
  13. * 判断对象是否为空
  14. * @param object 待判断对象
  15. * @param errorMsg 异常提示信息
  16. * @throws IllegalArgumentException
  17. */
  18. public static void isNull(Object object, String errorMsg) throws IllegalArgumentException {
  19. if (Objects.isNull(object)) {
  20. throw new IllegalArgumentException(errorMsg);
  21. }
  22. if (object instanceof String && ((String) object).isBlank()) {
  23. throw new IllegalArgumentException(errorMsg);
  24. }
  25. }
  26. /**
  27. * 判断对象是否为空
  28. * @param object 待判断对象
  29. * @throws IllegalArgumentException
  30. */
  31. public static void isNull(Object object) throws IllegalArgumentException {
  32. isNull(object, "传入的对象参数不能为空");
  33. }
  34. /**
  35. * 判断对象是否为空
  36. * @param object 待判断对象
  37. * @param errorMsg 异常提示信息
  38. * @throws BusinessException
  39. */
  40. public static void isNullInBusiness(Object object, String errorMsg) throws BusinessException {
  41. if (Objects.isNull(object)) {
  42. throw new BusinessException(errorMsg);
  43. }
  44. if (object instanceof String && ((String) object).isBlank()) {
  45. throw new BusinessException(errorMsg);
  46. }
  47. }
  48. /**
  49. * 判断集合是否为空
  50. * @param collection 待判断对象
  51. * @param errorMsg 异常提示信息
  52. * @return
  53. * @param <T>
  54. * @throws IllegalArgumentException
  55. */
  56. public static <T> void notEmpty(Collection<T> collection, String errorMsg) throws IllegalArgumentException {
  57. if (collection == null || collection.isEmpty()) {
  58. throw new IllegalArgumentException(errorMsg);
  59. }
  60. }
  61. /**
  62. * 判断集合是否为空
  63. * @param collection 待判断对象
  64. * @param <T>
  65. * @throws IllegalArgumentException
  66. */
  67. public static <T> void notEmpty(Collection<T> collection) throws IllegalArgumentException {
  68. notEmpty(collection, "传入的集合参数不能为空");
  69. }
  70. }