refactor: 重构 MultiTypesException.ExceptionType 接口

- 将 `ExceptionType` 中的工厂方法抽取到 `ExceptionFactory` 中
- 更新相关文档注释
This commit is contained in:
zhouxy108 2025-06-09 11:10:03 +08:00
parent 8828b12c78
commit 577b73874b
3 changed files with 70 additions and 40 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.exception;
import javax.annotation.Nonnull;
/**
* 异常工厂
*
* @author ZhouXY
*/
public interface ExceptionFactory<X extends Exception> {
/**
* 创建异常
*
* @return 异常对象
*/
@Nonnull
X create();
/**
* 使用指定 {@code message} 创建异常
*
* @param message 异常信息
* @return 异常对象
*/
@Nonnull
X create(String message);
/**
* 使用指定 {@code cause} 创建异常
*
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
X create(Throwable cause);
/**
* 使用指定 {@code message} {@code cause} 创建异常
*
* @param message 异常信息
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
X create(String message, Throwable cause);
}

View File

@ -29,7 +29,8 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型 * 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型
* *
* <p> * <p>
* 表示场景类型的枚举实现 {@link ExceptionType}其中的工厂方法用于创建对应类型的异常 * 表示场景类型的枚举实现 {@link ExceptionType}各个枚举值本身就是该场景的异常的工厂实例
* 使用其中的工厂方法用于创建对应类型的异常
* *
* <pre> * <pre>
* public final class LoginException * public final class LoginException
@ -120,7 +121,9 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0 * @since 1.0.0
*/ */
public interface MultiTypesException<E extends Exception, T extends MultiTypesException.ExceptionType<E>> { public interface MultiTypesException<
X extends Exception,
T extends MultiTypesException.ExceptionType<X>> {
/** /**
* 异常类型 * 异常类型
@ -142,48 +145,13 @@ public interface MultiTypesException<E extends Exception, T extends MultiTypesEx
/** /**
* 异常类型 * 异常类型
*/ */
public static interface ExceptionType<E extends Exception> extends IWithCode<String> { public static interface ExceptionType<X extends Exception>
extends IWithCode<String>, ExceptionFactory<X> {
/** /**
* 默认异常信息 * 默认异常信息
*/ */
String getDefaultMessage(); String getDefaultMessage();
/**
* 创建异常
*
* @return 异常对象
*/
@Nonnull
E create();
/**
* 使用指定 {@code message} 创建异常
*
* @param message 异常信息
* @return 异常对象
*/
@Nonnull
E create(String message);
/**
* 使用指定 {@code cause} 创建异常
*
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
E create(Throwable cause);
/**
* 使用指定 {@code message} {@code cause} 创建异常
*
* @param message 异常信息
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
E create(String message, Throwable cause);
} }
} }

View File

@ -25,7 +25,8 @@
* 异常实现 {@link MultiTypesException} {@link MultiTypesException#getType} 方法返回对应的场景类型 * 异常实现 {@link MultiTypesException} {@link MultiTypesException#getType} 方法返回对应的场景类型
* *
* <p> * <p>
* 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType}其中的工厂方法用于创建对应类型的异常 * 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType}各个枚举值本身就是该场景的异常的工厂实例
* 使用其中的工厂方法用于创建对应类型的异常
* *
* <pre> * <pre>
* public final class LoginException * public final class LoginException