private static void createLocales(TypeSpec.Builder type, List<LocaleID> defaultContent, Set<LocaleID> available) {
    TypeSpec.Builder localeInterface = TypeSpec.interfaceBuilder("Locale")
        .addModifiers(PUBLIC, STATIC)
        .addMethod(MethodSpec.methodBuilder("language")
            .addModifiers(PUBLIC, ABSTRACT)
            .returns(String.class).build())
        .addMethod(MethodSpec.methodBuilder("script")
            .addModifiers(PUBLIC, ABSTRACT)
            .returns(String.class).build())
        .addMethod(MethodSpec.methodBuilder("territory")
            .addModifiers(PUBLIC, ABSTRACT)
            .returns(String.class).build())
        .addMethod(MethodSpec.methodBuilder("variant")
            .addModifiers(PUBLIC, ABSTRACT)
            .returns(String.class).build())
        .addMethod(MethodSpec.methodBuilder("compact")
            .addModifiers(PUBLIC, ABSTRACT)
            .returns(String.class).build())
        .addMethod(MethodSpec.methodBuilder("expanded")
            .addModifiers(PUBLIC, ABSTRACT)
            .returns(String.class).build());
    
    List<LocaleID> localeFields = new ArrayList<>();
    List<String> allLocales = new ArrayList<>();
    List<String> bundles = new ArrayList<>();
    
    for (LocaleID locale : available) {
      localeFields.add(locale);
      allLocales.add(locale.safe);
      bundles.add(locale.safe);
    }
    
    MethodSpec.Builder method = MethodSpec.methodBuilder("registerDefaultContent")
        .addModifiers(PRIVATE, STATIC);
    
    for (LocaleID locale : defaultContent) {
      LocaleID dest = new LocaleID(locale.language, "", "", "");
      if (available.contains(dest)) {
        localeFields.add(locale);
        allLocales.add(locale.safe);
        method.addStatement("DEFAULT_CONTENT.put(Locale.$L, Locale.$L)",
            locale.safe,
            dest.safe);
      }
    }
    type.addMethod(method.build());

    Collections.sort(localeFields);
    for (LocaleID locale : localeFields) {
      addLocaleField(localeInterface, locale.safe, locale);
    }

    StringBuilder buf = new StringBuilder("$T.unmodifiableList($T.asList(\n");
    for (int i = 0; i < allLocales.size(); i++) {
      if (i > 0) {
        buf.append(",\n");
      }
      buf.append("  Locale.$L");
    }
    buf.append("))");

    Collections.sort(allLocales);
    
    List<Object> arguments = new ArrayList<>();
    arguments.add(Collections.class);
    arguments.add(Arrays.class);
    arguments.addAll(allLocales);
    
    FieldSpec.Builder field = FieldSpec.builder(LIST_CLDR_LOCALE_IF, "AVAILABLE_LOCALES", PRIVATE, STATIC, FINAL);
    field.initializer(buf.toString(), arguments.toArray());
    type.addField(field.build());
    
    buf = new StringBuilder("$T.unmodifiableList($T.asList(\n");
    for (int i = 0; i < bundles.size(); i++) {
      if (i > 0) {
        buf.append(",\n");
      }
      buf.append("  Locale.$L");
    }
    buf.append("))");
    
    Collections.sort(bundles);
    arguments = new ArrayList<>();
    arguments.add(Collections.class);
    arguments.add(Arrays.class);
    arguments.addAll(bundles);
    
    field = FieldSpec.builder(LIST_CLDR_LOCALE_IF, "AVAILABLE_BUNDLES", PRIVATE, STATIC, FINAL);
    field.initializer(buf.toString(), arguments.toArray());
    type.addField(field.build());
    
    method = MethodSpec.methodBuilder("availableLocales")
        .addModifiers(PUBLIC, FINAL)
        .returns(LIST_CLDR_LOCALE_IF)
        .addStatement("return AVAILABLE_LOCALES");
    
    type.addMethod(method.build());
    
    method = MethodSpec.methodBuilder("availableBundles")
        .addModifiers(PUBLIC, FINAL)
        .returns(LIST_CLDR_LOCALE_IF)
        .addStatement("return AVAILABLE_BUNDLES");
    
    type.addMethod(method.build());

    type.addType(localeInterface.build());
  }
private static void createLocales ( TypeSpec . Builder type , List < LocaleID > defaultContent , Set < LocaleID > available ) { TypeSpec . Builder localeInterface = TypeSpec . interfaceBuilder ( "Locale" ) . addModifiers ( PUBLIC , STATIC ) . addMethod ( MethodSpec . methodBuilder ( "language" ) . addModifiers ( PUBLIC , ABSTRACT ) . returns ( String . class ) . build ( ) ) . addMethod ( MethodSpec . methodBuilder ( "script" ) . addModifiers ( PUBLIC , ABSTRACT ) . returns ( String . class ) . build ( ) ) . addMethod ( MethodSpec . methodBuilder ( "territory" ) . addModifiers ( PUBLIC , ABSTRACT ) . returns ( String . class ) . build ( ) ) . addMethod ( MethodSpec . methodBuilder ( "variant" ) . addModifiers ( PUBLIC , ABSTRACT ) . returns ( String . class ) . build ( ) ) . addMethod ( MethodSpec . methodBuilder ( "compact" ) . addModifiers ( PUBLIC , ABSTRACT ) . returns ( String . class ) . build ( ) ) . addMethod ( MethodSpec . methodBuilder ( "expanded" ) . addModifiers ( PUBLIC , ABSTRACT ) . returns ( String . class ) . build ( ) ) ; List < LocaleID > localeFields = new ArrayList < > ( ) ; List < String > allLocales = new ArrayList < > ( ) ; List < String > bundles = new ArrayList < > ( ) ; for ( LocaleID locale : available ) { localeFields . add ( locale ) ; allLocales . add ( locale . safe ) ; bundles . add ( locale . safe ) ; } MethodSpec . Builder method = MethodSpec . methodBuilder ( "registerDefaultContent" ) . addModifiers ( PRIVATE , STATIC ) ; for ( LocaleID locale : defaultContent ) { LocaleID dest = new LocaleID ( locale . language , "" , "" , "" ) ; if ( available . contains ( dest ) ) { localeFields . add ( locale ) ; allLocales . add ( locale . safe ) ; method . addStatement ( "DEFAULT_CONTENT.put(Locale.$L, Locale.$L)" , locale . safe , dest . safe ) ; } } type . addMethod ( method . build ( ) ) ; Collections . sort ( localeFields ) ; for ( LocaleID locale : localeFields ) { addLocaleField ( localeInterface , locale . safe , locale ) ; } StringBuilder buf = new StringBuilder ( "$T.unmodifiableList($T.asList(\n" ) ; for ( int i = 0 ; i < allLocales . size ( ) ; i ++ ) { if ( i > 0 ) { buf . append ( ",\n" ) ; } buf . append ( "  Locale.$L" ) ; } buf . append ( "))" ) ; Collections . sort ( allLocales ) ; List < Object > arguments = new ArrayList < > ( ) ; arguments . add ( Collections . class ) ; arguments . add ( Arrays . class ) ; arguments . addAll ( allLocales ) ; FieldSpec . Builder field = FieldSpec . builder ( LIST_CLDR_LOCALE_IF , "AVAILABLE_LOCALES" , PRIVATE , STATIC , FINAL ) ; field . initializer ( buf . toString ( ) , arguments . toArray ( ) ) ; type . addField ( field . build ( ) ) ; buf = new StringBuilder ( "$T.unmodifiableList($T.asList(\n" ) ; for ( int i = 0 ; i < bundles . size ( ) ; i ++ ) { if ( i > 0 ) { buf . append ( ",\n" ) ; } buf . append ( "  Locale.$L" ) ; } buf . append ( "))" ) ; Collections . sort ( bundles ) ; arguments = new ArrayList < > ( ) ; arguments . add ( Collections . class ) ; arguments . add ( Arrays . class ) ; arguments . addAll ( bundles ) ; field = FieldSpec . builder ( LIST_CLDR_LOCALE_IF , "AVAILABLE_BUNDLES" , PRIVATE , STATIC , FINAL ) ; field . initializer ( buf . toString ( ) , arguments . toArray ( ) ) ; type . addField ( field . build ( ) ) ; method = MethodSpec . methodBuilder ( "availableLocales" ) . addModifiers ( PUBLIC , FINAL ) . returns ( LIST_CLDR_LOCALE_IF ) . addStatement ( "return AVAILABLE_LOCALES" ) ; type . addMethod ( method . build ( ) ) ; method = MethodSpec . methodBuilder ( "availableBundles" ) . addModifiers ( PUBLIC , FINAL ) . returns ( LIST_CLDR_LOCALE_IF ) . addStatement ( "return AVAILABLE_BUNDLES" ) ; type . addMethod ( method . build ( ) ) ; type . addType ( localeInterface . build ( ) ) ; }
Generate the mapping for default content locales.
See: http://cldr.unicode.org/translation/default-content