108 lines
3.5 KiB
Java
Raw Normal View History

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
import java.util.Objects;
/**
* 枚举工具类
*
* @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 枚举的类型信息
* @param ordinal 数据库中对应的数值
* @return 枚举对象
*/
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) {
E[] values = clazz.getEnumConstants();
try {
return values[ordinal];
} catch (IndexOutOfBoundsException e) {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
}
/**
* 通过 ordinal 获取枚举实例
*
* @param <E> 枚举的类型
* @param clazz 枚举的类型信息
* @param ordinal 数据库中对应的数值
* @return 枚举对象
*/
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, Integer ordinal) {
E[] values = clazz.getEnumConstants();
try {
return Objects.nonNull(ordinal) ? values[ordinal] : values[0];
} catch (IndexOutOfBoundsException e) {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
}
/**
* 通过 ordinal 获取枚举实例
*
* @param <E> 枚举的类型
* @param clazz 枚举的类型信息
* @param ordinal 数据库中对应的数值
* @return 枚举对象
*/
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, Integer ordinal) {
E[] values = clazz.getEnumConstants();
try {
return Objects.nonNull(ordinal) ? values[ordinal] : null;
} catch (IndexOutOfBoundsException e) {
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
}
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
if (ordinal == null) {
throw new IllegalArgumentException("ordinal 不能为空");
}
E[] values = clazz.getEnumConstants();
if (ordinal >= 0 && ordinal < values.length) {
return ordinal;
}
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
}
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, Integer ordinal) {
if (Objects.isNull(ordinal)) {
return null;
}
return checkOrdinal(clazz, ordinal);
}
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, Integer ordinal) {
if (Objects.isNull(ordinal)) {
return 0;
}
return checkOrdinal(clazz, ordinal);
}
}