public final class EnumUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static Optional<Class<? extends Enum<?>>> |
asEnumClassIfPossible(@Nullable Class<?> clazz)
Returns an optional with the class cast as enum extension, empty otherwise.
|
static Optional<Class<? extends Enum<?>>> |
getAssociatedEnumType(@Nullable Class<?> clazz)
Returns an optional with the class if it is an enum, or with the actual enum class if the given class is
a synthetic class of an enum entry.
|
static boolean |
isEnumOrEnumEntryType(@Nullable Class<?> clazz)
Indicates whether the given class is an enum or a synthetic class of an enum entry.
|
static <E extends Enum<E>> |
toEnumSet(Class<E> enumType)
Returns a collector that collects the results of a stream into an
EnumSet. |
static <T> Optional<T> |
tryValueOf(@Nullable Class<T> clazz,
@Nullable String name)
Returns an optional of the enum entry with the given name if the provided class is an enum and has an entry that
matches exactly the given name.
|
static <T> Optional<T> |
tryValueOfCaseInsensitive(@Nullable Class<T> clazz,
@Nullable String name)
Returns an optional of the enum entry with the given name if the provided class is an enum and has an entry that
matches the given name in a case-insensitive manner.
|
public static <T> Optional<T> tryValueOf(@Nullable @Nullable Class<T> clazz, @Nullable @Nullable String name)
T - the class typeclazz - the class to check for enum entries, or nullname - the name to match, or nullpublic static <T> Optional<T> tryValueOfCaseInsensitive(@Nullable @Nullable Class<T> clazz, @Nullable @Nullable String name)
T - the class typeclazz - the class to check for enum entries, or nullname - the name to match (case-insensitive), or nullpublic static Optional<Class<? extends Enum<?>>> asEnumClassIfPossible(@Nullable @Nullable Class<?> clazz)
getAssociatedEnumType(java.lang.Class<?>) if synthetic classes of enum entries should be
converted to their enum type.
Examples:
Class<?> class1 = TimeUnit.class;
Class<?> class2 = String.class;
EnumUtils.typeAsEnumClass(class1) = Optional.of(TimeUnit.class)
EnumUtils.typeAsEnumClass(class2) = Optional.empty()
clazz - the class to inspect, or nullpublic static boolean isEnumOrEnumEntryType(@Nullable
@Nullable Class<?> clazz)
Examples:
Class<?> class1 = NumericShaper.Range.class;
Class<?> class2 = NumericShaper.Range.ETHIOPIC.getClass(); // NumericShaper$Range$1.class
EnumUtils.isEnumOrEnumEntryType(class1) = true
EnumUtils.isEnumOrEnumEntryType(class2) = true
EnumUtils.isEnumOrEnumEntryType(null) = false
EnumUtils.isEnumOrEnumEntryType(int.class) = false
clazz - the class to inspect, or nullpublic static Optional<Class<? extends Enum<?>>> getAssociatedEnumType(@Nullable @Nullable Class<?> clazz)
Examples:
Class<?> class1 = NumericShaper.Range.class;
Class<?> class2 = NumericShaper.Range.ETHIOPIC.getClass(); // NumericShaper$Range$1.class
EnumUtils.asEnumType(class1) = Optional.of(NumericShaper.Range.class)
EnumUtils.asEnumType(class2) = Optional.of(NumericShaper.Range.class)
EnumUtils.asEnumType(null) = Optional.empty()
EnumUtils.asEnumType(int.class) = Optional.empty()
Note: to always get the actual enum class of an enum entry, use Enum.getDeclaringClass().
clazz - the class to inspect, or nullpublic static <E extends Enum<E>> Collector<E,?,EnumSet<E>> toEnumSet(Class<E> enumType)
EnumSet. Convenience method for
brevity.E - the enum typeenumType - the class of the enum the stream consists ofCopyright © 2023. All rights reserved.