
com.github.rkumsher.enums.EnumUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
Collection of Java utility classes
The newest version!
package com.github.rkumsher.enums;
import java.util.Optional;
/** Utility library for working with {@link Enum}s. */
public final class EnumUtils {
private EnumUtils() {}
/**
* Returns an optional enum constant for the given type, using {@link Enum#valueOf}. If the
* constant does not exist, {@link Optional#empty()} is returned.
*
* @param enumType - the Class object of the enum type from which to search for a constant
* @param name - the name of the constant to search for
* @return optional enum constant for the given type. If the constant does not exist, {@link
* Optional#empty()} is returned.
*/
public static > Optional getIfPresent(Class enumType, String name) {
try {
return Optional.of(Enum.valueOf(enumType, name));
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy