@@ -4,8 +4,7 @@ examples of error prone issues below:
44#### Constructors
55
66The constructors don't validate the parameters at all, they just "trust" it
7- 100%. \
8- This is also true for the static method ` Locale.of ` , introduced in JDK 19.
7+ 100%. This is also true for the static method ` Locale.of ` , introduced in JDK 19.
98
109For example:
1110
@@ -49,10 +48,9 @@ getVariant() :
4948We can see that while the ` toString() ` value for both locales are equivalent,
5049the individual parts are different. More specifically, the first locale is
5150incorrect since ` #Hant ` is supposed to be the script for the locale rather than
52- the variant. \
53- There's no reliable way of getting a correct result through a ` Locale `
54- constructor, so we should prefer using ` Locale.forLanguageTag() ` (and the IETF
55- BCP 47 format) for correctness.
51+ the variant. There's no reliable way of getting a correct result through a
52+ ` Locale ` constructor, so we should prefer using ` Locale.forLanguageTag() ` (and
53+ the IETF BCP 47 format) for correctness.
5654
5755** Note:** You might see a ` .replace('_', '-') ` appended to a suggested fix for
5856the error prone checker for this bug pattern. This is sanitization measure to
@@ -68,7 +66,6 @@ If the initial code started with a `String` that was split at `'_'` or `'-'`,
6866just to be used for locale, the right fix is to use ` toLanguageTag() ` .
6967
7068``` java
71- // Initial code
7269void someMethod(String localeId) {
7370 String [] parts = localeId. split(" _" );
7471 Locale locale = switch (parts. size) {
@@ -78,10 +75,11 @@ void someMethod(String localeId) {
7875 }
7976 // use the locale
8077}
78+ ```
8179
82- // Fixed code
80+ ``` java
8381void someMethod(String localeId) {
84- Locale locale = Locale . forLanguageTag. replace(' _' , ' -' );
82+ Locale locale = Locale . forLanguageTag(localeId . replace(' _' , ' -' ) );
8583 // use the locale
8684}
8785```
@@ -90,15 +88,15 @@ If the initial code started separate "pieces" (language, region, variant) the
9088right fix is to use a ` Locale.Builder() ` .
9189
9290``` java
93- // Initial code
9491void someMethod(@NotNull String langId, String regionId) {
9592 Locale locale (regionId == null )
9693 ? new Locale (langId) // or Locale.of
9794 : new Locale (langId, regionId); // or Locale.of
9895 // use the locale
9996}
97+ ```
10098
101- // Fixed code
99+ ``` java
102100void someMethod(@NotNull String langId, String regionId) {
103101 Locale . Builder builder = new Locale .Builder ();
104102 builder. setLanguage(langId);
@@ -110,7 +108,7 @@ void someMethod(@NotNull String langId, String regionId) {
110108}
111109```
112110
113- #### toString()
111+ #### ` toString() `
114112
115113This poses the inverse of the constructor problem.
116114
@@ -121,11 +119,10 @@ Locale derivedLocale = ??? // Not clean way to get a correct locale from myLocal
121119```
122120
123121The ` toString() ` implementation for ` Locale ` isn't necessarily incorrect in
124- itself. \
125- It is intended to be _ "concise but informative representation that is easy for a
126- person to read"_ (see documentation at
122+ itself. It is intended to be * "concise but informative representation that is
123+ easy for a person to read"* (see documentation at
127124[ Object.toString()] ( https://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString\(\) ) ).
128125
129126So it is not intended to produce a value that can be turned back into a
130- ` Locale ` . It is not a serialization format. \
131- It often produces a value that _ looks _ like a locale identifier, but it is not.
127+ ` Locale ` . It is not a serialization format. It often produces a value that
128+ * looks * like a locale identifier, but it is not.
0 commit comments