Java tutorial
//package com.java2s; public class Main { /** * Parses a String val through an Enum class and returns a match or returns defaultValue * * @param value The value to attempt to match * @param enuma Enum class to match value in * @param defaultValue Value to return if a match isn't found * @return A parsed object */ public static <T extends Enum<T>> T stringToEnum(String value, Class<T> enuma, T defaultValue) { if (value == null || enuma == null) return defaultValue; T valueFound = Enum.valueOf(enuma, value.replace(" ", "_").toUpperCase()); if (valueFound != null) return valueFound; return defaultValue; } }