private static void createCurrencies ( TypeSpec . Builder type , List < String > currencies ) { TypeSpec . Builder currencyType = TypeSpec . enumBuilder ( "Currency" ) . addModifiers ( PUBLIC , STATIC ) ; List < String > codes = new ArrayList <> ( ) ; codes . addAll ( currencies ) ; Collections . sort ( codes ) ; StringBuilder buf = new StringBuilder ( "$T.unmodifiableList($T.asList(\n" ) ; for ( int i = 0 ; i < codes . size ( ) ; i ++ ) { if ( i > 0 ) { buf . append ( ",\n" ) ; } currencyType . addEnumConstant ( codes . get ( i ) ) ; buf . append ( " Currency.$L" ) ; } buf . append ( "))" ) ; // Add a safe string mapping that returns null instead of throwing. MethodSpec . Builder method = MethodSpec . methodBuilder ( "fromString" ) . addModifiers ( PUBLIC , STATIC ) . addParameter ( STRING , "code" ) . returns ( Types . CLDR_CURRENCY_ENUM ) ; method . beginControlFlow ( "if (code != null)" ) ; method . beginControlFlow ( "switch (code)" ) ; for ( int i = 0 ; i < codes . size ( ) ; i ++ ) { method . addStatement ( "case $S: return $L" , codes . get ( i ) , codes . get ( i ) ) ; } method . addStatement ( "default: break" ) ; method . endControlFlow ( ) ; method . endControlFlow ( ) ; method . addStatement ( "return null" ) ; currencyType . addMethod ( method . build ( ) ) ; type . addType ( currencyType . build ( ) ) ; // Initialize field containing all currencies List < Object > arguments = new ArrayList <> ( ) ; arguments . add ( Collections . class ) ; arguments . add ( Arrays . class ) ; arguments . addAll ( codes ) ; }
Create top - level container to hold currency constants .