All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kohsuke.github.internal.EnumUtils Maven / Gradle / Ivy

The newest version!
package org.kohsuke.github.internal;

import java.util.Locale;
import java.util.logging.Logger;

/**
 * Utils for Enums.
 */
public final class EnumUtils {

    private static final Logger LOGGER = Logger.getLogger(EnumUtils.class.getName());

    /**
     * Returns an enum value matching the value if found, null if the value is null and {@code defaultEnum} if the value
     * cannot be matched to a value of the enum.
     * 

* The value is converted to uppercase before being matched to the enum values. * * @param * the type of the enum * @param enumClass * the type of the enum * @param value * the value to interpret * @param defaultEnum * the default enum value if the value doesn't match one of the enum value * @return an enum value or null */ public static > E getNullableEnumOrDefault(Class enumClass, String value, E defaultEnum) { if (value == null) { return null; } return getEnumOrDefault(enumClass, value, defaultEnum); } /** * Returns an enum value matching the value if found, {@code defaultEnum} if the value is null or cannot be matched * to a value of the enum. * * @param * the type of the enum * @param enumClass * the type of the enum * @param value * the value to interpret * @param defaultEnum * the default enum value if the value doesn't match one of the enum value * @return an enum value */ public static > E getEnumOrDefault(Class enumClass, String value, E defaultEnum) { try { if (value != null) { return Enum.valueOf(enumClass, value.toUpperCase(Locale.ROOT)); } } catch (IllegalArgumentException e) { } LOGGER.warning("Unknown value " + value + " for enum class " + enumClass.getName() + ", defaulting to " + defaultEnum.name()); return defaultEnum; } private EnumUtils() { } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy