@Test public void convertMyEnum() { hyphenateEnumConverter = new HyphenateEnumConverter(MyEnum.class); MyEnum myEnum = (MyEnum) hyphenateEnumConverter.convert("DISCARD"); assertEquals(myEnum, MyEnum.DISCARD); myEnum = (MyEnum) hyphenateEnumConverter.convert("discard"); assertEquals(myEnum, MyEnum.DISCARD); myEnum = (MyEnum) hyphenateEnumConverter.convert("READ_UNCOMMITTED"); assertEquals(myEnum, MyEnum.READ_UNCOMMITTED); myEnum = (MyEnum) hyphenateEnumConverter.convert("read-uncommitted"); assertEquals(myEnum, MyEnum.READ_UNCOMMITTED); myEnum = (MyEnum) hyphenateEnumConverter.convert("SIGUSR1"); assertEquals(myEnum, MyEnum.SIGUSR1); myEnum = (MyEnum) hyphenateEnumConverter.convert("sigusr1"); assertEquals(myEnum, MyEnum.SIGUSR1); myEnum = (MyEnum) hyphenateEnumConverter.convert("TrendBreaker"); assertEquals(myEnum, MyEnum.TrendBreaker); myEnum = (MyEnum) hyphenateEnumConverter.convert("trend-breaker"); assertEquals(myEnum, MyEnum.TrendBreaker); myEnum = (MyEnum) hyphenateEnumConverter.convert("MAKING_LifeDifficult"); assertEquals(myEnum, MyEnum.MAKING_LifeDifficult); myEnum = (MyEnum) hyphenateEnumConverter.convert("making-life-difficult"); assertEquals(myEnum, MyEnum.MAKING_LifeDifficult); myEnum = (MyEnum) hyphenateEnumConverter.convert("YeOldeJBoss"); assertEquals(myEnum, MyEnum.YeOldeJBoss); myEnum = (MyEnum) hyphenateEnumConverter.convert("ye-olde-jboss"); assertEquals(myEnum, MyEnum.YeOldeJBoss); }
@Override public E convert(String value) { value = value.trim(); if (value.isEmpty()) { return null; } final String hyphenatedValue = hyphenate(value); final Enum<?> enumValue = values.get(hyphenatedValue); if (enumValue != null) { return enumType.cast(enumValue); } throw new IllegalArgumentException(String.format("Cannot convert %s to enum %s", value, enumType)); }
HyphenateEnumConverter implements Converter<E>, Serializable { @Override public E convert(String value) { value = value.trim(); if (value.isEmpty()) { return null; } final String hyphenatedValue = hyphenate(value); final Enum<?> enumValue = values.get(hyphenatedValue); if (enumValue != null) { return enumType.cast(enumValue); } throw new IllegalArgumentException(String.format("Cannot convert %s to enum %s", value, enumType)); } }
HyphenateEnumConverter implements Converter<E>, Serializable { @Override public E convert(String value) { value = value.trim(); if (value.isEmpty()) { return null; } final String hyphenatedValue = hyphenate(value); final Enum<?> enumValue = values.get(hyphenatedValue); if (enumValue != null) { return enumType.cast(enumValue); } throw new IllegalArgumentException(String.format("Cannot convert %s to enum %s", value, enumType)); } HyphenateEnumConverter(Class<E> enumType); }
HyphenateEnumConverter implements Converter<E>, Serializable { @Override public E convert(String value) { value = value.trim(); if (value.isEmpty()) { return null; } final String hyphenatedValue = hyphenate(value); final Enum<?> enumValue = values.get(hyphenatedValue); if (enumValue != null) { return enumType.cast(enumValue); } throw new IllegalArgumentException(String.format("Cannot convert %s to enum %s", value, enumType)); } HyphenateEnumConverter(Class<E> enumType); static HyphenateEnumConverter<E> of(Class<E> enumType); @Override E convert(String value); }
HyphenateEnumConverter implements Converter<E>, Serializable { @Override public E convert(String value) { value = value.trim(); if (value.isEmpty()) { return null; } final String hyphenatedValue = hyphenate(value); final Enum<?> enumValue = values.get(hyphenatedValue); if (enumValue != null) { return enumType.cast(enumValue); } throw new IllegalArgumentException(String.format("Cannot convert %s to enum %s", value, enumType)); } HyphenateEnumConverter(Class<E> enumType); static HyphenateEnumConverter<E> of(Class<E> enumType); @Override E convert(String value);  }