修改 EnumTools 中方法的参数名称

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