/** Validate that @name is the name of an enum constant from the * enum class @clz. * * @param clz the enum class * @param name the enum constant name; must not be null * @return the enum constant * @throws WebApplicationException wrapping an appropriate response * iff the @name is invalid */ public static <E extends Enum<E>> E validateEnum(Class<E> clz, String name) { return validateEnum(clz, name, false); }
public static <E extends Enum<E>> E validateEnum(String reason, String detail, Class<E> clz, String name, boolean toUppercase) { try { return Enum.valueOf(clz, toUppercase ? name.toUpperCase() : name); } catch (IllegalArgumentException|NullPointerException e) { detail = detail + getPossibleValues(clz); throw new WebApplicationException(response(reason, MessageFormat.format(detail, name, clz.getSimpleName()))); } }
private static Response response(String reason, String detail) { return Response.status(INVALID_ENUM_STATUS).entity(fault(reason, detail)).build(); }
/** * Use this method wherever the list of possible values is extended beyond the enum values.; * This would commonly be seen in situations we where enums are deprecated they're value become system configurable; * instead of hard-coded. e.g {@link org.ovirt.engine.core.common.osinfo.OsRepository} * @param clz the emum class * @param externalValues the extended list of values to validate the input upon * @param name the actual value to be looked up. could be the enum member or part of extended list of values */ public static <E extends Enum<E>> String validateEnum(Class<E> clz, Set<String> externalValues, String name, boolean toUppercase) { return validateEnum(INVALID_ENUM_REASON, INVALID_ENUM_DETAIL, clz, externalValues, name, toUppercase); }
public static <E extends Enum<E>> String validateEnum(String reason, String detail, Class<E> clz, Set<String> externalValues, String name, boolean toUppercase) { for (String externalValue : externalValues) { if (externalValue.equalsIgnoreCase(name)) { return name; } } try { return Enum.valueOf(clz, toUppercase ? name.toUpperCase() : name).name(); } catch (IllegalArgumentException|NullPointerException e) { detail = detail + getPossibleValues(clz, OsTypeUtils.getAllValues()); throw new WebApplicationException(response(reason, MessageFormat.format(detail, name, clz.getSimpleName()))); } }
public static <E extends Enum<E>> E validateEnum(Class<E> clz, String name, boolean toUppercase) { return validateEnum(INVALID_ENUM_REASON, INVALID_ENUM_DETAIL, clz, name, toUppercase); }
/** Validate that @name is the name of an enum constant from the * enum class @clz. * * @param reason the fault reason * @param detail the fault detail * @param clz the enum class * @param name the enum constant name; must not be null * @return the enum constant * @throws WebApplicationException wrapping an appropriate response * iff the @name is invalid */ public static <E extends Enum<E>> E validateEnum(String reason, String detail, Class<E> clz, String name) { return validateEnum(reason, detail, clz, name, false); }
public <E extends Enum<E>> E validateEnum(Class<E> clz, String name) { String reason = localize(Messages.INVALID_ENUM_REASON); String detail = localize(Messages.INVALID_ENUM_DETAIL); return EnumValidator.validateEnum(reason, detail, clz, name); }
@Override public Response resolve(Action action) { validateParameters(action, "resolutionType"); ResolutionType resolutionType = EnumValidator.validateEnum(ResolutionType.class, action.getResolutionType(), true); switch (resolutionType) { case ADD: return addToMissingServers(action); case COPY: return copy(action); default: return null; } }
@Test public void testValid() { assertEquals(Thread.State.NEW, validateEnum(Thread.State.class, "NEW")); }
@Test public void testInvalid() { try { validateEnum(Thread.State.class, "foobar"); fail("expected WebApplicationException on invalid value"); } catch (WebApplicationException wae) { verifyInvalidValueException(wae, "foobar", "State"); } }