public < T extends Enum < T > > T validateEnum ( Config config , String value , Class < T > type , T ... choices ) { if ( choices . length == 0 ) { choices = type . getEnumConstants ( ) ; } Preconditions . checkArgument ( choices . length > 0 ) ; try { T result = Enum . valueOf ( type , value ) ; if ( ! Arrays . asList ( choices ) . contains ( result ) ) { throw new IllegalArgumentException ( ) ; } return result ; } catch ( IllegalArgumentException e ) { throw new MorphlineCompilationException ( String . format ( "Invalid choice: '%s' (choose from {%s})" , value , Joiner . on ( "," ) . join ( choices ) ) , config ) ; } }
Validates that an enum of the given type with the given value exists and that this enum is contained in the given list of permitted choices ; finally returns that enum object .