private static void buildMatchVariables(
      TypeSpec.Builder type, Map<String, Set<String>> territoryData, LanguageMatchingData data) {
    
    Map<String, Set<String>> territories = flatten(territoryData);
    
    CodeBlock.Builder code = CodeBlock.builder();
    code.add("new $T<$T, $T<$T>>() {{\n", HASHMAP, STRING, SET, STRING);
    for (Map.Entry<String, Set<String>> entry : territories.entrySet()) {
      code.add("  put($S, new $T<>(", entry.getKey(), HASHSET);
      List<String> list = new ArrayList<>(entry.getValue());
      Collections.sort(list);
      addStringList(code, list, "    ");
      code.add("));\n");
    }
    code.add("\n}}");
    
    TypeName typeName = ParameterizedTypeName.get(MAP, STRING, SET_STRING);
    type.addField(FieldSpec.builder(typeName, "TERRITORIES", PROTECTED, STATIC, FINAL)
        .initializer(code.build())
        .build());

    // Generate mapping for each variable and its inverse.
    Map<String, Set<String>> variables = new LinkedHashMap<>();
    
    for (Pair<String, String> pair : data.matchVariables) {
      String variable = pair._1;
      
      Set<String> countries = new HashSet<>();
      for (String regionId : pair._2.split("\\+")) {
        Set<String> children = territories.get(regionId);
        if (children != null) {
          countries.addAll(children);
        } else {
          countries.add(regionId);
        }
      }

      variables.put(variable, countries);
    }

    // Build the final variable tables.
    code = CodeBlock.builder();
    code.add("new $T<$T, $T<$T>>() {{\n", HASHMAP, STRING, SET, STRING);
    for (Map.Entry<String, Set<String>> entry : variables.entrySet()) {
      code.add("  put($S, new $T<>(", entry.getKey(), HASHSET);
      List<String> list = new ArrayList<>(entry.getValue());
      Collections.sort(list);
      addStringList(code, list, "    ");
      code.add("));\n");
    }
    code.add("\n}}");
    
    type.addField(FieldSpec.builder(typeName, "VARIABLES", PROTECTED, STATIC, FINAL)
        .initializer(code.build())
        .build());
  }
private static void buildMatchVariables ( TypeSpec . Builder type , Map < String , Set < String > > territoryData , LanguageMatchingData data ) { Map < String , Set < String > > territories = flatten ( territoryData ) ; CodeBlock . Builder code = CodeBlock . builder ( ) ; code . add ( "new $T<$T, $T<$T>>() {{\n" , HASHMAP , STRING , SET , STRING ) ; for ( Map . Entry < String , Set < String > > entry : territories . entrySet ( ) ) { code . add ( "  put($S, new $T<>(" , entry . getKey ( ) , HASHSET ) ; List < String > list = new ArrayList < > ( entry . getValue ( ) ) ; Collections . sort ( list ) ; addStringList ( code , list , "    " ) ; code . add ( "));\n" ) ; } code . add ( "\n}}" ) ; TypeName typeName = ParameterizedTypeName . get ( MAP , STRING , SET_STRING ) ; type . addField ( FieldSpec . builder ( typeName , "TERRITORIES" , PROTECTED , STATIC , FINAL ) . initializer ( code . build ( ) ) . build ( ) ) ; Map < String , Set < String > > variables = new LinkedHashMap < > ( ) ; for ( Pair < String , String > pair : data . matchVariables ) { String variable = pair . _1 ; Set < String > countries = new HashSet < > ( ) ; for ( String regionId : pair . _2 . split ( "\\+" ) ) { Set < String > children = territories . get ( regionId ) ; if ( children != null ) { countries . addAll ( children ) ; } else { countries . add ( regionId ) ; } } variables . put ( variable , countries ) ; } code = CodeBlock . builder ( ) ; code . add ( "new $T<$T, $T<$T>>() {{\n" , HASHMAP , STRING , SET , STRING ) ; for ( Map . Entry < String , Set < String > > entry : variables . entrySet ( ) ) { code . add ( "  put($S, new $T<>(" , entry . getKey ( ) , HASHSET ) ; List < String > list = new ArrayList < > ( entry . getValue ( ) ) ; Collections . sort ( list ) ; addStringList ( code , list , "    " ) ; code . add ( "));\n" ) ; } code . add ( "\n}}" ) ; type . addField ( FieldSpec . builder ( typeName , "VARIABLES" , PROTECTED , STATIC , FINAL ) . initializer ( code . build ( ) ) . build ( ) ) ; }
Build table containing language matching variables.