1// Copyright (C) 2014 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef I18N_ADDRESSINPUT_REGION_DATA_BUILDER_H_
16#define I18N_ADDRESSINPUT_REGION_DATA_BUILDER_H_
17
18#include <libaddressinput/util/basictypes.h>
19
20#include <map>
21#include <string>
22
23namespace i18n {
24namespace addressinput {
25
26class PreloadSupplier;
27class RegionData;
28
29class RegionDataBuilder {
30 public:
31  // Does not take ownership of |supplier|, which should not be NULL.
32  explicit RegionDataBuilder(PreloadSupplier* supplier);
33  ~RegionDataBuilder();
34
35  // Returns a tree of administrative subdivisions for the |region_code|.
36  // Examples:
37  //   US with en-US UI language.
38  //   |______________________
39  //   |           |          |
40  //   v           v          v
41  // AL:Alabama  AK:Alaska  AS:American Samoa  ...
42  //
43  //   KR with ko-Latn UI language.
44  //   |______________________________________
45  //       |               |                  |
46  //       v               v                  v
47  // 강원도:Gangwon  경기도:Gyeonggi  경상남도:Gyeongnam  ...
48  //
49  //   KR with ko-KR UI language.
50  //   |_______________________________
51  //       |            |              |
52  //       v            v              v
53  // 강원도:강원  경기도:경기  경상남도:경남  ...
54  //
55  // The BCP 47 |ui_language_tag| is used to choose the best supported language
56  // tag for this region (assigned to |best_region_tree_language_tag|). For
57  // example, Canada has both English and French names for its administrative
58  // subdivisions. If the UI language is French, then the French names are used.
59  // The |best_region_tree_language_tag| value may be an empty string.
60  //
61  // Should be called only if supplier->IsLoaded(region_code) returns true. The
62  // |best_region_tree_language_tag| parameter should not be NULL.
63  const RegionData& Build(const std::string& region_code,
64                          const std::string& ui_language_tag,
65                          std::string* best_region_tree_language_tag);
66
67 private:
68  typedef std::map<std::string, const RegionData*> LanguageRegionMap;
69  typedef std::map<std::string, LanguageRegionMap*> RegionCodeDataMap;
70
71  PreloadSupplier* const supplier_;  // Not owned.
72  RegionCodeDataMap cache_;
73
74  DISALLOW_COPY_AND_ASSIGN(RegionDataBuilder);
75};
76
77}  // namespace addressinput
78}  // namespace i18n
79
80#endif  // I18N_ADDRESSINPUT_REGION_DATA_BUILDER_H_
81