Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/localebuilder.h
48729 views
// © 2018 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html#License2#ifndef __LOCALEBUILDER_H__3#define __LOCALEBUILDER_H__45#include "unicode/locid.h"6#include "unicode/stringpiece.h"7#include "unicode/uobject.h"8#include "unicode/utypes.h"91011#ifndef U_HIDE_DRAFT_API12/**13* \file14* \brief C++ API: Builder API for Locale15*/1617U_NAMESPACE_BEGIN18class CharString;1920/**21* <code>LocaleBuilder</code> is used to build instances of <code>Locale</code>22* from values configured by the setters. Unlike the <code>Locale</code>23* constructors, the <code>LocaleBuilder</code> checks if a value configured by a24* setter satisfies the syntax requirements defined by the <code>Locale</code>25* class. A <code>Locale</code> object created by a <code>LocaleBuilder</code> is26* well-formed and can be transformed to a well-formed IETF BCP 47 language tag27* without losing information.28*29* <p>The following example shows how to create a <code>Locale</code> object30* with the <code>LocaleBuilder</code>.31* <blockquote>32* <pre>33* UErrorCode status = U_ZERO_ERROR;34* Locale aLocale = LocaleBuilder()35* .setLanguage("sr")36* .setScript("Latn")37* .setRegion("RS")38* .build(status);39* if (U_SUCCESS(status)) {40* // ...41* }42* </pre>43* </blockquote>44*45* <p>LocaleBuilders can be reused; <code>clear()</code> resets all46* fields to their default values.47*48* <p>LocaleBuilder tracks errors in an internal UErrorCode. For all setters,49* except setLanguageTag and setLocale, LocaleBuilder will return immediately50* if the internal UErrorCode is in error state.51* To reset internal state and error code, call clear method.52* The setLanguageTag and setLocale method will first clear the internal53* UErrorCode, then track the error of the validation of the input parameter54* into the internal UErrorCode.55*56* @draft ICU 6457*/58class U_COMMON_API LocaleBuilder : public UObject {59public:60/**61* Constructs an empty LocaleBuilder. The default value of all62* fields, extensions, and private use information is the63* empty string.64*65* @draft ICU 6466*/67LocaleBuilder();6869/**70* Destructor71* @draft ICU 6472*/73virtual ~LocaleBuilder();7475/**76* Resets the <code>LocaleBuilder</code> to match the provided77* <code>locale</code>. Existing state is discarded.78*79* <p>All fields of the locale must be well-formed.80* <p>This method clears the internal UErrorCode.81*82* @param locale the locale83* @return This builder.84*85* @draft ICU 6486*/87LocaleBuilder& setLocale(const Locale& locale);8889/**90* Resets the LocaleBuilder to match the provided91* [Unicode Locale Identifier](http://www.unicode.org/reports/tr35/tr35.html#unicode_locale_id) .92* Discards the existing state. the empty string cause the builder to be93* reset, like {@link #clear}. Grandfathered tags are converted to their94* canonical form before being processed. Otherwise, the <code>language95* tag</code> must be well-formed, or else the build() method will later96* report an U_ILLEGAL_ARGUMENT_ERROR.97*98* <p>This method clears the internal UErrorCode.99*100* @param tag the language tag, defined as101* [unicode_locale_id](http://www.unicode.org/reports/tr35/tr35.html#unicode_locale_id).102* @return This builder.103* @draft ICU 64104*/105LocaleBuilder& setLanguageTag(StringPiece tag);106107/**108* Sets the language. If <code>language</code> is the empty string, the109* language in this <code>LocaleBuilder</code> is removed. Otherwise, the110* <code>language</code> must be well-formed, or else the build() method will111* later report an U_ILLEGAL_ARGUMENT_ERROR.112*113* <p>The syntax of language value is defined as114* [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag).115*116* @param language the language117* @return This builder.118* @draft ICU 64119*/120LocaleBuilder& setLanguage(StringPiece language);121122/**123* Sets the script. If <code>script</code> is the empty string, the script in124* this <code>LocaleBuilder</code> is removed.125* Otherwise, the <code>script</code> must be well-formed, or else the build()126* method will later report an U_ILLEGAL_ARGUMENT_ERROR.127*128* <p>The script value is a four-letter script code as129* [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag)130* defined by ISO 15924131*132* @param script the script133* @return This builder.134* @draft ICU 64135*/136LocaleBuilder& setScript(StringPiece script);137138/**139* Sets the region. If region is the empty string, the region in this140* <code>LocaleBuilder</code> is removed. Otherwise, the <code>region</code>141* must be well-formed, or else the build() method will later report an142* U_ILLEGAL_ARGUMENT_ERROR.143*144* <p>The region value is defined by145* [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag)146* as a two-letter ISO 3166 code or a three-digit UN M.49 area code.147*148* <p>The region value in the <code>Locale</code> created by the149* <code>LocaleBuilder</code> is always normalized to upper case.150*151* @param region the region152* @return This builder.153* @draft ICU 64154*/155LocaleBuilder& setRegion(StringPiece region);156157/**158* Sets the variant. If variant is the empty string, the variant in this159* <code>LocaleBuilder</code> is removed. Otherwise, the <code>variant</code>160* must be well-formed, or else the build() method will later report an161* U_ILLEGAL_ARGUMENT_ERROR.162*163* <p><b>Note:</b> This method checks if <code>variant</code>164* satisfies the165* [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag)166* syntax requirements, and normalizes the value to lowercase letters. However,167* the <code>Locale</code> class does not impose any syntactic168* restriction on variant. To set an ill-formed variant, use a Locale constructor.169* If there are multiple unicode_variant_subtag, the caller must concatenate170* them with '-' as separator (ex: "foobar-fibar").171*172* @param variant the variant173* @return This builder.174* @draft ICU 64175*/176LocaleBuilder& setVariant(StringPiece variant);177178/**179* Sets the extension for the given key. If the value is the empty string,180* the extension is removed. Otherwise, the <code>key</code> and181* <code>value</code> must be well-formed, or else the build() method will182* later report an U_ILLEGAL_ARGUMENT_ERROR.183*184* <p><b>Note:</b> The key ('u') is used for the Unicode locale extension.185* Setting a value for this key replaces any existing Unicode locale key/type186* pairs with those defined in the extension.187*188* <p><b>Note:</b> The key ('x') is used for the private use code. To be189* well-formed, the value for this key needs only to have subtags of one to190* eight alphanumeric characters, not two to eight as in the general case.191*192* @param key the extension key193* @param value the extension value194* @return This builder.195* @draft ICU 64196*/197LocaleBuilder& setExtension(char key, StringPiece value);198199/**200* Sets the Unicode locale keyword type for the given key. If the type201* StringPiece is constructed with a nullptr, the keyword is removed.202* If the type is the empty string, the keyword is set without type subtags.203* Otherwise, the key and type must be well-formed, or else the build()204* method will later report an U_ILLEGAL_ARGUMENT_ERROR.205*206* <p>Keys and types are converted to lower case.207*208* <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}209* replaces all Unicode locale keywords with those defined in the210* extension.211*212* @param key the Unicode locale key213* @param type the Unicode locale type214* @return This builder.215* @draft ICU 64216*/217LocaleBuilder& setUnicodeLocaleKeyword(218StringPiece key, StringPiece type);219220/**221* Adds a unicode locale attribute, if not already present, otherwise222* has no effect. The attribute must not be empty string and must be223* well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status224* during the build() call.225*226* @param attribute the attribute227* @return This builder.228* @draft ICU 64229*/230LocaleBuilder& addUnicodeLocaleAttribute(StringPiece attribute);231232/**233* Removes a unicode locale attribute, if present, otherwise has no234* effect. The attribute must not be empty string and must be well-formed235* or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the build() call.236*237* <p>Attribute comparison for removal is case-insensitive.238*239* @param attribute the attribute240* @return This builder.241* @draft ICU 64242*/243LocaleBuilder& removeUnicodeLocaleAttribute(StringPiece attribute);244245/**246* Resets the builder to its initial, empty state.247* <p>This method clears the internal UErrorCode.248*249* @return this builder250* @draft ICU 64251*/252LocaleBuilder& clear();253254/**255* Resets the extensions to their initial, empty state.256* Language, script, region and variant are unchanged.257*258* @return this builder259* @draft ICU 64260*/261LocaleBuilder& clearExtensions();262263/**264* Returns an instance of <code>Locale</code> created from the fields set265* on this builder.266* If any set methods or during the build() call require memory allocation267* but fail U_MEMORY_ALLOCATION_ERROR will be set to status.268* If any of the fields set by the setters are not well-formed, the status269* will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will270* not change after the build() call and the caller is free to keep using271* the same builder to build more locales.272*273* @return a new Locale274* @draft ICU 64275*/276Locale build(UErrorCode& status);277278private:279UErrorCode status_;280char language_[9];281char script_[5];282char region_[4];283CharString *variant_; // Pointer not object so we need not #include internal charstr.h.284icu::Locale *extensions_; // Pointer not object. Storage for all other fields.285286};287288U_NAMESPACE_END289290#endif // U_HIDE_DRAFT_API291#endif // __LOCALEBUILDER_H__292293294