修改 EnumTools 中方法的参数名称

1.x.x
ZhouXY108 2024-12-24 17:41:04 +08:00
parent 8ac446e228
commit 9f7eda47fe
1 changed files with 47 additions and 56 deletions

View File

@ -34,18 +34,18 @@ public final class EnumTools {
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal
* @param <E>
* @param enumType
* @param ordinal
* @return
* @deprecated 使 ordinal
*/
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉
AssertTools.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants();
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
E[] values = enumType.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return values[ordinal];
}
@ -53,25 +53,23 @@ public final class EnumTools {
* ordinal
*
* @param <E>
* @param clazz
* @param enumType
* @param ordinal
* @param defaultValue
* @return
* @deprecated 使 ordinal
*/
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> clazz, @Nullable Integer ordinal, E defaultValue) { // NOSONAR 该方法弃用,但不删掉
if (null == ordinal) {
return defaultValue;
}
return valueOf(clazz, ordinal);
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用,但不删掉
@Nullable Integer ordinal, E defaultValue) {
return null == ordinal ? defaultValue : valueOf(enumType, ordinal);
}
/**
* ordinal
*
* @param <E>
* @param clazz
* @param enumType
* @param ordinal
* @param defaultValue
* @return
@ -79,29 +77,26 @@ public final class EnumTools {
*/
@Deprecated
public static <E extends Enum<?>> E getValueOrDefault( // NOSONAR 该方法弃用,但不删掉
Class<E> clazz,
Class<E> enumType,
@Nullable Integer ordinal,
Supplier<E> defaultValue) {
if (null == ordinal) {
return defaultValue.get();
}
return valueOf(clazz, ordinal);
return null == ordinal ? defaultValue.get() : valueOf(enumType, ordinal);
}
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal
* @param <E>
* @param enumType
* @param ordinal
* @return
* @deprecated 使 ordinal
*/
@Deprecated
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
return getValueOrDefault(clazz, ordinal, () -> {
AssertTools.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants();
public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
return getValueOrDefault(enumType, ordinal, () -> {
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
E[] values = enumType.getEnumConstants();
return values[0];
});
}
@ -109,69 +104,65 @@ public final class EnumTools {
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal
* @param <E>
* @param enumType
* @param ordinal
* @return
* @deprecated 使 ordinal
*/
@Deprecated
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
return valueOf(clazz, ordinal, null);
public static <E extends Enum<?>> E getValueNullable(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
return valueOf(enumType, ordinal, null);
}
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
AssertTools.checkNotNull(clazz, "Clazz must not be null.");
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) {
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
E[] values = clazz.getEnumConstants();
if (ordinal >= 0 && ordinal < values.length) {
return ordinal;
}
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
E[] values = enumType.getEnumConstants();
AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length,
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return ordinal;
}
/**
* ordinal
*
* @param <E>
* @param clazz
* @param ordinal The ordinal
* @param <E>
* @param enumType
* @param ordinal The ordinal
* @return The ordinal
*/
@Nullable
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, @Nullable Integer ordinal) {
return checkOrdinalOrDefault(clazz, ordinal, null);
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> enumType, @Nullable Integer ordinal) {
return checkOrdinalOrDefault(enumType, ordinal, null);
}
/**
* ordinal ordinal {@code null} {@code 0}
*
* @param <E>
* @param clazz
* @param ordinal The ordinal
* @param <E>
* @param enumType
* @param ordinal The ordinal
* @return The ordinal
*/
@Nullable
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
return checkOrdinalOrDefault(clazz, ordinal, 0);
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> enumType, @Nullable Integer ordinal) {
return checkOrdinalOrDefault(enumType, ordinal, 0);
}
/**
* ordinal ordinal {@code null} {@code defaultValue}
*
* @param <E>
* @param clazz
* @param ordinal The ordinal
* @param <E>
* @param enumType
* @param ordinal The ordinal
* @return The ordinal
*/
@Nullable
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(
Class<E> clazz,
Class<E> enumType,
@Nullable Integer ordinal,
@Nullable Integer defaultValue) {
if (ordinal != null) {
return checkOrdinal(clazz, ordinal);
}
return defaultValue;
return ordinal != null ? checkOrdinal(enumType, ordinal) : defaultValue;
}
}