2023-03-13 14:26:03 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2022-2023 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-24 11:10:27 +08:00
|
|
|
|
package xyz.zhouxy.plusone.commons.util;
|
2022-11-07 17:49:27 +08:00
|
|
|
|
|
2023-04-21 01:47:18 +08:00
|
|
|
|
import java.util.function.Supplier;
|
2022-11-07 17:49:27 +08:00
|
|
|
|
|
2023-04-15 20:16:21 +08:00
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
2023-07-19 00:32:08 +08:00
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
|
2022-11-07 17:49:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* 枚举工具类
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
|
|
|
|
*/
|
|
|
|
|
public final class EnumUtil {
|
|
|
|
|
|
|
|
|
|
private EnumUtil() {
|
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过 ordinal 获取枚举实例
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举的类型
|
|
|
|
|
* @param clazz 枚举的类型信息
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @param ordinal 序号
|
2022-11-07 17:49:27 +08:00
|
|
|
|
* @return 枚举对象
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @deprecated 不推荐使用枚举的 ordinal。
|
2022-11-07 17:49:27 +08:00
|
|
|
|
*/
|
2023-05-13 12:52:47 +08:00
|
|
|
|
@Deprecated
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) {
|
2023-07-19 00:32:08 +08:00
|
|
|
|
Preconditions.checkNotNull(clazz, "Clazz must not be null.");
|
2022-11-07 17:49:27 +08:00
|
|
|
|
E[] values = clazz.getEnumConstants();
|
2023-07-19 00:44:42 +08:00
|
|
|
|
PreconditionsExt.check((ordinal >= 0 && ordinal < values.length),
|
2023-04-21 01:47:18 +08:00
|
|
|
|
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
|
|
|
|
|
return values[ordinal];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过 ordinal 获取枚举实例
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举的类型
|
|
|
|
|
* @param clazz 枚举的类型信息
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @param ordinal 序号
|
2023-04-21 01:47:18 +08:00
|
|
|
|
* @param defaultValue 默认值
|
|
|
|
|
* @return 枚举对象
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @deprecated 不推荐使用枚举的 ordinal。
|
2023-04-21 01:47:18 +08:00
|
|
|
|
*/
|
2023-05-13 12:52:47 +08:00
|
|
|
|
@Deprecated
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> E valueOf(Class<E> clazz, @Nullable Integer ordinal, E defaultValue) {
|
|
|
|
|
if (null == ordinal) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
return valueOf(clazz, ordinal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过 ordinal 获取枚举实例
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举的类型
|
|
|
|
|
* @param clazz 枚举的类型信息
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @param ordinal 序号
|
2023-04-21 01:47:18 +08:00
|
|
|
|
* @param defaultValue 默认值
|
|
|
|
|
* @return 枚举对象
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @deprecated 不推荐使用枚举的 ordinal。
|
2023-04-21 01:47:18 +08:00
|
|
|
|
*/
|
2023-05-13 12:52:47 +08:00
|
|
|
|
@Deprecated
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> E getValueOrDefault(
|
|
|
|
|
Class<E> clazz,
|
|
|
|
|
@Nullable Integer ordinal,
|
|
|
|
|
Supplier<E> defaultValue) {
|
|
|
|
|
if (null == ordinal) {
|
|
|
|
|
return defaultValue.get();
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
2023-04-21 01:47:18 +08:00
|
|
|
|
return valueOf(clazz, ordinal);
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过 ordinal 获取枚举实例
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举的类型
|
|
|
|
|
* @param clazz 枚举的类型信息
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @param ordinal 序号
|
2022-11-07 17:49:27 +08:00
|
|
|
|
* @return 枚举对象
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @deprecated 不推荐使用枚举的 ordinal。
|
2022-11-07 17:49:27 +08:00
|
|
|
|
*/
|
2023-05-13 12:52:47 +08:00
|
|
|
|
@Deprecated
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
|
|
|
|
|
return getValueOrDefault(clazz, ordinal, () -> {
|
2023-07-19 00:32:08 +08:00
|
|
|
|
Preconditions.checkNotNull(clazz, "Clazz must not be null.");
|
2023-04-21 01:47:18 +08:00
|
|
|
|
E[] values = clazz.getEnumConstants();
|
|
|
|
|
return values[0];
|
|
|
|
|
});
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过 ordinal 获取枚举实例
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举的类型
|
|
|
|
|
* @param clazz 枚举的类型信息
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @param ordinal 序号
|
2022-11-07 17:49:27 +08:00
|
|
|
|
* @return 枚举对象
|
2023-05-13 12:52:47 +08:00
|
|
|
|
* @deprecated 不推荐使用枚举的 ordinal。
|
2022-11-07 17:49:27 +08:00
|
|
|
|
*/
|
2023-05-13 12:52:47 +08:00
|
|
|
|
@Deprecated
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, @Nullable Integer ordinal) {
|
|
|
|
|
return valueOf(clazz, ordinal, null);
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
|
2023-07-19 00:32:08 +08:00
|
|
|
|
Preconditions.checkNotNull(clazz, "Clazz must not be null.");
|
|
|
|
|
Preconditions.checkNotNull(ordinal, "Ordinal must not be null.");
|
2022-11-07 17:49:27 +08:00
|
|
|
|
E[] values = clazz.getEnumConstants();
|
|
|
|
|
if (ordinal >= 0 && ordinal < values.length) {
|
|
|
|
|
return ordinal;
|
|
|
|
|
}
|
|
|
|
|
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 11:17:46 +08:00
|
|
|
|
/**
|
|
|
|
|
* 校验枚举的 ordinal。
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举类型
|
|
|
|
|
* @param clazz 枚举类型
|
|
|
|
|
* @param ordinal The ordinal
|
|
|
|
|
* @return The ordinal
|
|
|
|
|
*/
|
|
|
|
|
@Nullable
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, @Nullable Integer ordinal) {
|
|
|
|
|
return checkOrdinalOrDefault(clazz, ordinal, null);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 11:17:46 +08:00
|
|
|
|
/**
|
|
|
|
|
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code 0}。
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举类型
|
|
|
|
|
* @param clazz 枚举类型
|
|
|
|
|
* @param ordinal The ordinal
|
|
|
|
|
* @return The ordinal
|
|
|
|
|
*/
|
|
|
|
|
@Nullable
|
2023-04-21 01:47:18 +08:00
|
|
|
|
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
|
|
|
|
|
return checkOrdinalOrDefault(clazz, ordinal, 0);
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 11:17:46 +08:00
|
|
|
|
/**
|
|
|
|
|
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code defaultValue}。
|
|
|
|
|
*
|
|
|
|
|
* @param <E> 枚举类型
|
|
|
|
|
* @param clazz 枚举类型
|
|
|
|
|
* @param ordinal The ordinal
|
|
|
|
|
* @return The ordinal
|
|
|
|
|
*/
|
2023-04-21 01:47:18 +08:00
|
|
|
|
@Nullable
|
|
|
|
|
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(
|
|
|
|
|
Class<E> clazz,
|
|
|
|
|
@Nullable Integer ordinal,
|
|
|
|
|
@Nullable Integer defaultValue) {
|
|
|
|
|
if (ordinal != null) {
|
|
|
|
|
return checkOrdinal(clazz, ordinal);
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
2023-04-21 01:47:18 +08:00
|
|
|
|
return defaultValue;
|
2022-11-07 17:49:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|