Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame^] | 1 | package aQute.bnd.build.model.conversions; |
| 2 | |
| 3 | public class EnumConverter<E extends Enum<E>> implements Converter<E,String> { |
| 4 | |
| 5 | private final Class<E> enumType; |
| 6 | private final E defaultValue; |
| 7 | |
| 8 | public static <E extends Enum<E>> EnumConverter<E> create(Class<E> enumType) { |
| 9 | return new EnumConverter<E>(enumType, null); |
| 10 | } |
| 11 | |
| 12 | public static <E extends Enum<E>> EnumConverter<E> create(Class<E> enumType, E defaultValue) { |
| 13 | return new EnumConverter<E>(enumType, defaultValue); |
| 14 | } |
| 15 | |
| 16 | private EnumConverter(Class<E> enumType, E defaultValue) { |
| 17 | this.enumType = enumType; |
| 18 | this.defaultValue = defaultValue; |
| 19 | } |
| 20 | |
| 21 | public E convert(String input) throws IllegalArgumentException { |
| 22 | if (input == null) |
| 23 | return defaultValue; |
| 24 | return Enum.valueOf(enumType, input); |
| 25 | } |
| 26 | |
| 27 | } |