001package org.unix4j.convert;
002
003public class EnumConverters {
004        public static class StringToEnumConverter<E extends Enum<E>> implements ValueConverter<E> {
005                private final Class<E> enumClass;
006                public StringToEnumConverter(Class<E> enumClass) {
007                        this.enumClass = enumClass;
008                }
009                @Override
010                public E convert(Object value) throws IllegalArgumentException {
011                        if (value != null) {
012                                final String name = value.toString();
013                                try {
014                                        return Enum.valueOf(enumClass, name);
015                                } catch (IllegalArgumentException e) {
016                                        return null;
017                                }
018                        }
019                        return null;
020                }
021        };
022}