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

io.dropwizard.util.Enums Maven / Gradle / Ivy

There is a newer version: 0.0.3
Show newest version
package io.dropwizard.util;

/**
 * Helper methods for enum types.
 */
public class Enums {

    /**
     * Convert a string to an enum with more permissive rules than {@link Enum} valueOf().
     * 

* This method is more permissive in the following ways: *

    *
  • Whitespace is permitted but stripped from the input.
  • *
  • Dashes and periods in the value are converted to underscores.
  • *
  • Matching against the enum values is case insensitive.
  • *
* @param value The string to convert. * @param constants The list of constants for the {@link Enum} to which you wish to convert. * @return The enum or null, if no enum constant matched the input value. */ public static Enum fromStringFuzzy(String value, Enum[] constants) { final String text = value.replaceAll("\\s+","") .replace('-', '_') .replace('.', '_'); for (Enum constant : constants) { if (constant.name().equalsIgnoreCase(text)) { return constant; } } // In some cases there are certain enums that don't follow the same pattern across an enterprise. So this // means that you have a mix of enums that use toString(), some use @JsonCreator, and some just use the // standard constant name(). This block handles finding the proper enum by toString() for (Enum constant : constants) { if (constant.toString().equalsIgnoreCase(value)) { return constant; } } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy