-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
256 additions
and
66 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
tang-commons/src/main/java/com/tang/commons/exception/AssertException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.tang.commons.exception; | ||
|
||
/** | ||
* 断言异常 | ||
* | ||
* @author Tang | ||
*/ | ||
public class AssertException extends RuntimeException { | ||
|
||
@java.io.Serial | ||
private static final long serialVersionUID = 1953209867997857028L; | ||
|
||
public AssertException() { | ||
} | ||
|
||
public AssertException(String message) { | ||
super(message); | ||
} | ||
|
||
public AssertException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public AssertException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
182 changes: 182 additions & 0 deletions
182
tang-commons/src/main/java/com/tang/commons/utils/Assert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package com.tang.commons.utils; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
import com.tang.commons.exception.AssertException; | ||
|
||
/** | ||
* 断言工具类,符合条件时抛出异常 | ||
* | ||
* @author Tang | ||
*/ | ||
public class Assert { | ||
|
||
private Assert() { | ||
} | ||
|
||
/** | ||
* 表达式为 True 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
*/ | ||
public static void isTrue(boolean expression) { | ||
isTrue(expression, "表达式为 True"); | ||
} | ||
|
||
/** | ||
* 表达式为 True 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
* @param message 异常信息 | ||
*/ | ||
public static void isTrue(boolean expression, String message) { | ||
isTrue(expression, new AssertException(message)); | ||
} | ||
|
||
/** | ||
* 表达式为 True 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
* @param cause 异常 | ||
*/ | ||
public static void isTrue(boolean expression, RuntimeException cause) { | ||
isTrue(expression, () -> cause); | ||
} | ||
|
||
/** | ||
* 表达式为 True 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
* @param cause 异常 | ||
*/ | ||
public static void isTrue(boolean expression, Supplier<? extends RuntimeException> cause) { | ||
if (expression) { | ||
throw cause.get(); | ||
} | ||
} | ||
|
||
/** | ||
* 表达式为 False 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
*/ | ||
public static void isFalse(boolean expression) { | ||
isFalse(expression, "表达式为 False"); | ||
} | ||
|
||
/** | ||
* 表达式为 False 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
* @param message 异常信息 | ||
*/ | ||
public static void isFalse(boolean expression, String message) { | ||
isFalse(expression, new AssertException(message)); | ||
} | ||
|
||
/** | ||
* 表达式为 False 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
* @param cause 异常 | ||
*/ | ||
public static void isFalse(boolean expression, RuntimeException cause) { | ||
isFalse(expression, () -> cause); | ||
} | ||
|
||
/** | ||
* 表达式为 False 时抛出异常 | ||
* | ||
* @param expression 表达式 | ||
* @param cause 异常 | ||
*/ | ||
public static void isFalse(boolean expression, Supplier<? extends RuntimeException> cause) { | ||
if (!expression) { | ||
throw cause.get(); | ||
} | ||
} | ||
|
||
/** | ||
* 对象为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
*/ | ||
public static void isNull(Object object) { | ||
isNull(object, "对象为 Null"); | ||
} | ||
|
||
/** | ||
* 对象为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
* @param message 异常信息 | ||
*/ | ||
public static void isNull(Object object, String message) { | ||
isNull(object, new AssertException(message)); | ||
} | ||
|
||
/** | ||
* 对象为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
* @param cause 异常 | ||
*/ | ||
public static void isNull(Object object, RuntimeException cause) { | ||
isNull(object, () -> cause); | ||
} | ||
|
||
/** | ||
* 对象为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
* @param cause 异常 | ||
*/ | ||
public static void isNull(Object object, Supplier<? extends RuntimeException> cause) { | ||
if (Objects.isNull(object)) { | ||
throw cause.get(); | ||
} | ||
} | ||
|
||
/** | ||
* 对象不为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
*/ | ||
public static void nonNull(Object object) { | ||
nonNull(object, "对象不为 Null"); | ||
} | ||
|
||
/** | ||
* 对象不为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
* @param message 异常信息 | ||
*/ | ||
public static void nonNull(Object object, String message) { | ||
nonNull(object, new AssertException(message)); | ||
} | ||
|
||
/** | ||
* 对象不为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
* @param cause 异常 | ||
*/ | ||
public static void nonNull(Object object, RuntimeException cause) { | ||
nonNull(object, () -> cause); | ||
} | ||
|
||
/** | ||
* 对象不为 Null 时抛出异常 | ||
* | ||
* @param object 对象 | ||
* @param cause 异常 | ||
*/ | ||
public static void nonNull(Object object, Supplier<? extends RuntimeException> cause) { | ||
if (Objects.nonNull(object)) { | ||
throw cause.get(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.