Conversions { @SuppressWarnings("unchecked") public static <T extends Enum<T>> T variantToEnum(final Object variant, final Class<T> enumClazz) { if (variant == null) { return null; } if (variant instanceof String) { return Enum.valueOf(enumClazz, ((String) variant).trim().toUpperCase()); } else if (variant instanceof Number) { final Integer index = variantToInteger(variant); final T[] enumConstants = enumClazz.getEnumConstants(); if (index < 0 || index >= enumConstants.length) { throw new IllegalArgumentException( String.format("Variant of type \"%s\", and value \"%s\" is out of range [, %d]", variant.getClass(), variant, enumConstants.length ) ); } return enumConstants[index]; } else if (variant instanceof Enum) { return (T) variant; } throw new IllegalArgumentException(String.format("\"%s\", and value \"%s\" is not one of %s", variant.getClass(), variant, Arrays.toString(SUPPORTED_VARIANT_TO_ENUM_TYPES))); }  static Integer variantToInteger(Object variant); static DateTime variantToDateTime(final Object variant); static DateTime variantToDateTime(final Object variant, final DateTimeZone timeZone); @SuppressWarnings("unchecked") static T variantToEnum(final Object variant, final Class<T> enumClazz); static Boolean variantToBoolean(final Object input);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEnumShouldFailUnsupportedType(){ Conversions.variantToEnum(Lists.newArrayList(),MockEnum.class); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEnumShouldFailInvalidEnumIndex(){ final int invalidIndex = MockEnum.class.getEnumConstants().length + 1; Conversions.variantToEnum(invalidIndex, MockEnum.class); }