Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/localebuilder.h
48729 views
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html#License
3
#ifndef __LOCALEBUILDER_H__
4
#define __LOCALEBUILDER_H__
5
6
#include "unicode/locid.h"
7
#include "unicode/stringpiece.h"
8
#include "unicode/uobject.h"
9
#include "unicode/utypes.h"
10
11
12
#ifndef U_HIDE_DRAFT_API
13
/**
14
* \file
15
* \brief C++ API: Builder API for Locale
16
*/
17
18
U_NAMESPACE_BEGIN
19
class CharString;
20
21
/**
22
* <code>LocaleBuilder</code> is used to build instances of <code>Locale</code>
23
* from values configured by the setters. Unlike the <code>Locale</code>
24
* constructors, the <code>LocaleBuilder</code> checks if a value configured by a
25
* setter satisfies the syntax requirements defined by the <code>Locale</code>
26
* class. A <code>Locale</code> object created by a <code>LocaleBuilder</code> is
27
* well-formed and can be transformed to a well-formed IETF BCP 47 language tag
28
* without losing information.
29
*
30
* <p>The following example shows how to create a <code>Locale</code> object
31
* with the <code>LocaleBuilder</code>.
32
* <blockquote>
33
* <pre>
34
* UErrorCode status = U_ZERO_ERROR;
35
* Locale aLocale = LocaleBuilder()
36
* .setLanguage("sr")
37
* .setScript("Latn")
38
* .setRegion("RS")
39
* .build(status);
40
* if (U_SUCCESS(status)) {
41
* // ...
42
* }
43
* </pre>
44
* </blockquote>
45
*
46
* <p>LocaleBuilders can be reused; <code>clear()</code> resets all
47
* fields to their default values.
48
*
49
* <p>LocaleBuilder tracks errors in an internal UErrorCode. For all setters,
50
* except setLanguageTag and setLocale, LocaleBuilder will return immediately
51
* if the internal UErrorCode is in error state.
52
* To reset internal state and error code, call clear method.
53
* The setLanguageTag and setLocale method will first clear the internal
54
* UErrorCode, then track the error of the validation of the input parameter
55
* into the internal UErrorCode.
56
*
57
* @draft ICU 64
58
*/
59
class U_COMMON_API LocaleBuilder : public UObject {
60
public:
61
/**
62
* Constructs an empty LocaleBuilder. The default value of all
63
* fields, extensions, and private use information is the
64
* empty string.
65
*
66
* @draft ICU 64
67
*/
68
LocaleBuilder();
69
70
/**
71
* Destructor
72
* @draft ICU 64
73
*/
74
virtual ~LocaleBuilder();
75
76
/**
77
* Resets the <code>LocaleBuilder</code> to match the provided
78
* <code>locale</code>. Existing state is discarded.
79
*
80
* <p>All fields of the locale must be well-formed.
81
* <p>This method clears the internal UErrorCode.
82
*
83
* @param locale the locale
84
* @return This builder.
85
*
86
* @draft ICU 64
87
*/
88
LocaleBuilder& setLocale(const Locale& locale);
89
90
/**
91
* Resets the LocaleBuilder to match the provided
92
* [Unicode Locale Identifier](http://www.unicode.org/reports/tr35/tr35.html#unicode_locale_id) .
93
* Discards the existing state. the empty string cause the builder to be
94
* reset, like {@link #clear}. Grandfathered tags are converted to their
95
* canonical form before being processed. Otherwise, the <code>language
96
* tag</code> must be well-formed, or else the build() method will later
97
* report an U_ILLEGAL_ARGUMENT_ERROR.
98
*
99
* <p>This method clears the internal UErrorCode.
100
*
101
* @param tag the language tag, defined as
102
* [unicode_locale_id](http://www.unicode.org/reports/tr35/tr35.html#unicode_locale_id).
103
* @return This builder.
104
* @draft ICU 64
105
*/
106
LocaleBuilder& setLanguageTag(StringPiece tag);
107
108
/**
109
* Sets the language. If <code>language</code> is the empty string, the
110
* language in this <code>LocaleBuilder</code> is removed. Otherwise, the
111
* <code>language</code> must be well-formed, or else the build() method will
112
* later report an U_ILLEGAL_ARGUMENT_ERROR.
113
*
114
* <p>The syntax of language value is defined as
115
* [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag).
116
*
117
* @param language the language
118
* @return This builder.
119
* @draft ICU 64
120
*/
121
LocaleBuilder& setLanguage(StringPiece language);
122
123
/**
124
* Sets the script. If <code>script</code> is the empty string, the script in
125
* this <code>LocaleBuilder</code> is removed.
126
* Otherwise, the <code>script</code> must be well-formed, or else the build()
127
* method will later report an U_ILLEGAL_ARGUMENT_ERROR.
128
*
129
* <p>The script value is a four-letter script code as
130
* [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag)
131
* defined by ISO 15924
132
*
133
* @param script the script
134
* @return This builder.
135
* @draft ICU 64
136
*/
137
LocaleBuilder& setScript(StringPiece script);
138
139
/**
140
* Sets the region. If region is the empty string, the region in this
141
* <code>LocaleBuilder</code> is removed. Otherwise, the <code>region</code>
142
* must be well-formed, or else the build() method will later report an
143
* U_ILLEGAL_ARGUMENT_ERROR.
144
*
145
* <p>The region value is defined by
146
* [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag)
147
* as a two-letter ISO 3166 code or a three-digit UN M.49 area code.
148
*
149
* <p>The region value in the <code>Locale</code> created by the
150
* <code>LocaleBuilder</code> is always normalized to upper case.
151
*
152
* @param region the region
153
* @return This builder.
154
* @draft ICU 64
155
*/
156
LocaleBuilder& setRegion(StringPiece region);
157
158
/**
159
* Sets the variant. If variant is the empty string, the variant in this
160
* <code>LocaleBuilder</code> is removed. Otherwise, the <code>variant</code>
161
* must be well-formed, or else the build() method will later report an
162
* U_ILLEGAL_ARGUMENT_ERROR.
163
*
164
* <p><b>Note:</b> This method checks if <code>variant</code>
165
* satisfies the
166
* [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag)
167
* syntax requirements, and normalizes the value to lowercase letters. However,
168
* the <code>Locale</code> class does not impose any syntactic
169
* restriction on variant. To set an ill-formed variant, use a Locale constructor.
170
* If there are multiple unicode_variant_subtag, the caller must concatenate
171
* them with '-' as separator (ex: "foobar-fibar").
172
*
173
* @param variant the variant
174
* @return This builder.
175
* @draft ICU 64
176
*/
177
LocaleBuilder& setVariant(StringPiece variant);
178
179
/**
180
* Sets the extension for the given key. If the value is the empty string,
181
* the extension is removed. Otherwise, the <code>key</code> and
182
* <code>value</code> must be well-formed, or else the build() method will
183
* later report an U_ILLEGAL_ARGUMENT_ERROR.
184
*
185
* <p><b>Note:</b> The key ('u') is used for the Unicode locale extension.
186
* Setting a value for this key replaces any existing Unicode locale key/type
187
* pairs with those defined in the extension.
188
*
189
* <p><b>Note:</b> The key ('x') is used for the private use code. To be
190
* well-formed, the value for this key needs only to have subtags of one to
191
* eight alphanumeric characters, not two to eight as in the general case.
192
*
193
* @param key the extension key
194
* @param value the extension value
195
* @return This builder.
196
* @draft ICU 64
197
*/
198
LocaleBuilder& setExtension(char key, StringPiece value);
199
200
/**
201
* Sets the Unicode locale keyword type for the given key. If the type
202
* StringPiece is constructed with a nullptr, the keyword is removed.
203
* If the type is the empty string, the keyword is set without type subtags.
204
* Otherwise, the key and type must be well-formed, or else the build()
205
* method will later report an U_ILLEGAL_ARGUMENT_ERROR.
206
*
207
* <p>Keys and types are converted to lower case.
208
*
209
* <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
210
* replaces all Unicode locale keywords with those defined in the
211
* extension.
212
*
213
* @param key the Unicode locale key
214
* @param type the Unicode locale type
215
* @return This builder.
216
* @draft ICU 64
217
*/
218
LocaleBuilder& setUnicodeLocaleKeyword(
219
StringPiece key, StringPiece type);
220
221
/**
222
* Adds a unicode locale attribute, if not already present, otherwise
223
* has no effect. The attribute must not be empty string and must be
224
* well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status
225
* during the build() call.
226
*
227
* @param attribute the attribute
228
* @return This builder.
229
* @draft ICU 64
230
*/
231
LocaleBuilder& addUnicodeLocaleAttribute(StringPiece attribute);
232
233
/**
234
* Removes a unicode locale attribute, if present, otherwise has no
235
* effect. The attribute must not be empty string and must be well-formed
236
* or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the build() call.
237
*
238
* <p>Attribute comparison for removal is case-insensitive.
239
*
240
* @param attribute the attribute
241
* @return This builder.
242
* @draft ICU 64
243
*/
244
LocaleBuilder& removeUnicodeLocaleAttribute(StringPiece attribute);
245
246
/**
247
* Resets the builder to its initial, empty state.
248
* <p>This method clears the internal UErrorCode.
249
*
250
* @return this builder
251
* @draft ICU 64
252
*/
253
LocaleBuilder& clear();
254
255
/**
256
* Resets the extensions to their initial, empty state.
257
* Language, script, region and variant are unchanged.
258
*
259
* @return this builder
260
* @draft ICU 64
261
*/
262
LocaleBuilder& clearExtensions();
263
264
/**
265
* Returns an instance of <code>Locale</code> created from the fields set
266
* on this builder.
267
* If any set methods or during the build() call require memory allocation
268
* but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
269
* If any of the fields set by the setters are not well-formed, the status
270
* will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
271
* not change after the build() call and the caller is free to keep using
272
* the same builder to build more locales.
273
*
274
* @return a new Locale
275
* @draft ICU 64
276
*/
277
Locale build(UErrorCode& status);
278
279
private:
280
UErrorCode status_;
281
char language_[9];
282
char script_[5];
283
char region_[4];
284
CharString *variant_; // Pointer not object so we need not #include internal charstr.h.
285
icu::Locale *extensions_; // Pointer not object. Storage for all other fields.
286
287
};
288
289
U_NAMESPACE_END
290
291
#endif // U_HIDE_DRAFT_API
292
#endif // __LOCALEBUILDER_H__
293
294