001package com.mybatisflex.core.query; 002 003import com.mybatisflex.core.util.StringUtil; 004 005public class Assert { 006 007 008 private Assert() { 009 } 010 011 012 /** 013 * 断言表达式为 true 014 * 015 * @param <T> 要返回的值类型 016 * @param value 要返回的值 017 * @param expression 断言表达式 018 * @return 原始值 019 * @throws IllegalArgumentException 当表达式为 false 时抛出 020 */ 021 public static <T> T that(T value, boolean expression) { 022 if (!expression) { 023 throw new IllegalArgumentException("Assertion failed"); 024 } 025 return value; 026 } 027 028 /** 029 * 断言表达式为 true,成功返回指定的值 030 * 031 * @param <T> 要返回的值类型 032 * @param value 要返回的值 033 * @param expression 断言表达式 034 * @param message 异常消息 035 * @return 原始值 036 * @throws IllegalArgumentException 当表达式为 false 时抛出 037 */ 038 public static <T> T that(T value, boolean expression, String message) { 039 if (!expression) { 040 throw new IllegalArgumentException(message != null ? message : "Assertion failed"); 041 } 042 return value; 043 } 044 045 046 /** 047 * 断言字符串不为空白(延迟计算异常消息) 048 * 049 * @param text 要验证的字符串 050 * @param message 异常消息 051 * @return 原始字符串 052 * @throws IllegalArgumentException 当字符串为空白时抛出 053 */ 054 public static String hasText(String text, String message) { 055 if (StringUtil.noText(text)) { 056 throw new IllegalArgumentException( 057 StringUtil.hasText(message) ? message : "value must have text" 058 ); 059 } 060 return text; 061 } 062 063 064 /** 065 * 断言字符串不为空白 066 * 067 * @param text 要验证的参数 068 * @return 原始字符串 069 * @throws IllegalArgumentException 当字符串为空白时抛出 070 */ 071 public static boolean hasText(String text) { 072 if (StringUtil.noText(text)) { 073 throw new IllegalArgumentException("value must have text"); 074 } 075 return true; 076 } 077 078 079 /** 080 * 断言对象不为空 081 * 082 * @param obj 要验证的参数 083 * @return 原始对象 084 * @throws IllegalArgumentException 当对象为空时抛出 085 */ 086 public static boolean notNull(Object obj) { 087 if (obj == null) { 088 throw new IllegalArgumentException("value must not be null"); 089 } 090 return true; 091 } 092 093 /** 094 * 断言对象不为空 095 * 096 * @param obj 要验证的参数 097 * @param message 异常消息 098 * @return 原始对象 099 * @throws IllegalArgumentException 当对象为空时抛出 100 */ 101 public static boolean notNull(Object obj, String message) { 102 if (obj == null) { 103 throw new IllegalArgumentException(StringUtil.hasText(message) ? message : "value must not be null"); 104 } 105 return true; 106 } 107}