2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-02-16 20:07:51 +01:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* This class provides methods for manipulating enums.
|
|
|
|
*/
|
2016-02-16 20:07:51 +01:00
|
|
|
public class EnumUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all enum constants which are in the specified enum class.
|
2016-07-14 14:22:23 +02:00
|
|
|
*
|
2016-02-16 20:07:51 +01:00
|
|
|
* @param enumType the enum class.
|
|
|
|
* @param separator a string which will be used as a separator
|
|
|
|
* @return a string representation of the enum class.
|
2022-07-28 01:13:35 +02:00
|
|
|
* @param <T> the type of the enum.
|
2016-02-16 20:07:51 +01:00
|
|
|
*/
|
|
|
|
public static <T extends Enum<T>> String enumList(Class<T> enumType, String separator) {
|
2022-07-10 00:55:56 +02:00
|
|
|
return Arrays.stream(enumType.getEnumConstants())
|
|
|
|
.map(Enum::name)
|
|
|
|
.collect(Collectors.joining(separator));
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-28 01:13:35 +02:00
|
|
|
* List all enum constants which are in the specified enum class.
|
|
|
|
* It is equivalent to call {@link #enumList(Class, String)} with the second parameter <code>", "</code>.
|
2016-07-14 14:22:23 +02:00
|
|
|
*
|
2016-02-16 20:07:51 +01:00
|
|
|
* @param enumType the enum class.
|
|
|
|
* @return a string representation of the enum class.
|
2022-07-28 01:13:35 +02:00
|
|
|
* @param <T> the type of the enum.
|
2016-02-16 20:07:51 +01:00
|
|
|
*/
|
|
|
|
public static <T extends Enum<T>> String enumList(Class<T> enumType) {
|
|
|
|
return enumList(enumType, ", ");
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
/**
|
2022-07-28 01:13:35 +02:00
|
|
|
* Search for a specific enum entry in the provided enum type, using the case-insensitive search string.
|
2016-07-14 14:22:23 +02:00
|
|
|
*
|
2022-07-28 01:13:35 +02:00
|
|
|
* @param enumType the class of the enum in which to search
|
|
|
|
* @param search the case-insensitive name of the enum value to return.
|
|
|
|
* @return the element found in the enum, or null if not found.
|
|
|
|
* @param <T> the type of the enum.
|
2016-02-16 20:07:51 +01:00
|
|
|
*/
|
|
|
|
public static <T extends Enum<T>> T searchEnum(Class<T> enumType, String search) {
|
2022-07-28 01:13:35 +02:00
|
|
|
for (T el : enumType.getEnumConstants()) {
|
|
|
|
if (el.name().equalsIgnoreCase(search)) {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
return null;
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-07-14 01:46:44 +02:00
|
|
|
/**
|
2022-07-28 01:13:35 +02:00
|
|
|
* Search for a specific enum entry in the provided enum type, using the case-insensitive search string.
|
|
|
|
* unlike {@link #searchEnum(Class, String)}, this method does not statically check the enum type, in case it is not
|
|
|
|
* known at compilation time.
|
|
|
|
*
|
|
|
|
* For a statically checked enum type, uses {@link #searchEnum(Class, String)} instead.
|
2016-07-14 14:22:23 +02:00
|
|
|
*
|
2022-07-28 01:13:35 +02:00
|
|
|
* @param enumType the class of the enum in which to search
|
|
|
|
* @param search the case-insensitive name of the enum value to return.
|
2023-06-20 00:15:46 +02:00
|
|
|
* @return the element found in the enum, or null if not found or if the provided type is not an enum.
|
2016-07-14 01:46:44 +02:00
|
|
|
*/
|
|
|
|
public static Enum<?> searchUncheckedEnum(Class<?> enumType, String search) {
|
2022-07-28 01:13:35 +02:00
|
|
|
if (!enumType.isEnum())
|
|
|
|
return null;
|
|
|
|
for (Enum<?> el : (Enum<?>[]) enumType.getEnumConstants()) {
|
|
|
|
if (el.name().equalsIgnoreCase(search)) {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
return null;
|
2016-07-14 01:46:44 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
/**
|
2022-07-28 01:13:35 +02:00
|
|
|
* Pick a random value from an enum type.
|
2016-07-14 14:22:23 +02:00
|
|
|
*
|
2022-07-28 01:13:35 +02:00
|
|
|
* @param enumType the class of the enum in which to pick the value from
|
|
|
|
* @return one of the enum value, or null if the provided enum is empty.
|
|
|
|
* @param <T> the type of the enum.
|
2016-02-16 20:07:51 +01:00
|
|
|
*/
|
|
|
|
public static <T extends Enum<T>> T randomValue(Class<T> enumType) {
|
2022-07-28 01:13:35 +02:00
|
|
|
return RandomUtil.arrayElement(enumType.getEnumConstants());
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
|
|
|
|
2024-06-06 23:59:32 +02:00
|
|
|
private EnumUtil() {}
|
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|