pandalib-util javadoc and small API changes
This commit is contained in:
@@ -3,6 +3,9 @@ package fr.pandacube.lib.util;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* This class provides methods for manipulating enums.
|
||||
*/
|
||||
public class EnumUtil {
|
||||
|
||||
/**
|
||||
@@ -11,6 +14,7 @@ public class EnumUtil {
|
||||
* @param enumType the enum class.
|
||||
* @param separator a string which will be used as a separator
|
||||
* @return a string representation of the enum class.
|
||||
* @param <T> the type of the enum.
|
||||
*/
|
||||
public static <T extends Enum<T>> String enumList(Class<T> enumType, String separator) {
|
||||
return Arrays.stream(enumType.getEnumConstants())
|
||||
@@ -19,67 +23,65 @@ public class EnumUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* 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>.
|
||||
*
|
||||
* @param enumType the enum class.
|
||||
* @return a string representation of the enum class.
|
||||
* @param <T> the type of the enum.
|
||||
*/
|
||||
public static <T extends Enum<T>> String enumList(Class<T> enumType) {
|
||||
return enumList(enumType, ", ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de rechercher l'existance d'un élément dans un enum, de façon
|
||||
* insensible à la casse
|
||||
* Search for a specific enum entry in the provided enum type, using the case-insensitive search string.
|
||||
*
|
||||
* @param enumType la classe correpondant à l'enum à lister
|
||||
* @param search l'élément à rechercher, insensible à la casse
|
||||
* @return l'élément de l'énumarétion, si elle a été trouvée, null sinon
|
||||
* @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.
|
||||
*/
|
||||
public static <T extends Enum<T>> T searchEnum(Class<T> enumType, String search) {
|
||||
T[] elements = enumType.getEnumConstants();
|
||||
|
||||
for (T el : elements)
|
||||
if (el.name().equalsIgnoreCase(search)) return el;
|
||||
for (T el : enumType.getEnumConstants()) {
|
||||
if (el.name().equalsIgnoreCase(search)) {
|
||||
return el;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de rechercher l'existance d'un élément dans un enum, de façon
|
||||
* insensible à la casse
|
||||
* La validité de la classe passé en premier paramètre est vérifiée
|
||||
* dynamiquement et non
|
||||
* statiquement. Préférez l'utilisation de
|
||||
* {@link #searchEnum(Class, String)} quand c'est possible.
|
||||
* 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.
|
||||
*
|
||||
* @param enumType la classe correpondant à l'enum à lister
|
||||
* @param search l'élément à rechercher, insensible à la casse
|
||||
* @return l'élément de l'énumération, si elle a été trouvée et si la classe
|
||||
* passée en paramètre est un enum, null dans les autres cas
|
||||
* For a statically checked enum type, uses {@link #searchEnum(Class, String)} instead.
|
||||
*
|
||||
* @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 or if the provideid type is not an enum.
|
||||
*/
|
||||
public static Enum<?> searchUncheckedEnum(Class<?> enumType, String search) {
|
||||
if (!enumType.isEnum()) return null;
|
||||
Enum<?>[] elements = (Enum<?>[]) enumType.getEnumConstants();
|
||||
|
||||
for (Enum<?> el : elements)
|
||||
if (el.name().equalsIgnoreCase(search)) return el;
|
||||
if (!enumType.isEnum())
|
||||
return null;
|
||||
for (Enum<?> el : (Enum<?>[]) enumType.getEnumConstants()) {
|
||||
if (el.name().equalsIgnoreCase(search)) {
|
||||
return el;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne une valeur aléatoire parmis les élément de l'Enum spécifié en
|
||||
* paramètre.
|
||||
* Pick a random value from an enum type.
|
||||
*
|
||||
* @param enumType l'enum dans lequel piocher la valeur
|
||||
* @return une des valeurs de l'enum
|
||||
* @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.
|
||||
*/
|
||||
public static <T extends Enum<T>> T randomValue(Class<T> enumType) {
|
||||
T[] elements = enumType.getEnumConstants();
|
||||
|
||||
return elements[RandomUtil.rand.nextInt(elements.length)];
|
||||
return RandomUtil.arrayElement(enumType.getEnumConstants());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user