Remove usage of deprecated java.util.Locale constructor (#12761)

This commit removes usages of the deprecated java.util.Locale constructor, replacing with Locale.Builder.
This commit is contained in:
Chris Hegarty 2023-11-06 08:06:45 +00:00 committed by GitHub
parent 1448015641
commit 8d4f9e50cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 41 additions and 41 deletions

View File

@ -20,7 +20,7 @@ import java.util.Locale;
/** A stemmer for Brazilian Portuguese words. */
class BrazilianStemmer {
private static final Locale locale = new Locale("pt", "BR");
private static final Locale locale = new Locale.Builder().setLanguageTag("pt-BR").build();
/** Changed term */
private String TERM;

View File

@ -33,7 +33,7 @@ class GermanStemmer {
/** Amount of characters that are removed with <code>substitute()</code> while stemming. */
private int substCount = 0;
private static final Locale locale = new Locale("de", "DE");
private static final Locale locale = new Locale.Builder().setLanguageTag("de-DE").build();
/**
* Stemms the given term to an unique <code>discriminator</code>.

View File

@ -38,7 +38,8 @@ public class ThaiTokenizer extends SegmentingTokenizerBase {
*/
public static final boolean DBBI_AVAILABLE;
private static final BreakIterator proto = BreakIterator.getWordInstance(new Locale("th"));
private static final BreakIterator proto =
BreakIterator.getWordInstance(new Locale.Builder().setLanguageTag("th").build());
static {
// check that we have a working dictionary-based break iterator for thai

View File

@ -26,7 +26,8 @@ public class TestCollationKeyAnalyzer extends CollationTestBase {
// Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
// RuleBasedCollator. However, the Arabic Locale seems to order the Farsi
// characters properly.
private Collator collator = Collator.getInstance(new Locale("ar"));
private Collator collator =
Collator.getInstance(new Locale.Builder().setLanguageTag("ar").build());
private Analyzer analyzer;
@Override

View File

@ -24,7 +24,8 @@ import org.apache.lucene.util.BytesRef;
public class TestICUCollationKeyAnalyzer extends CollationTestBase {
private Collator collator = Collator.getInstance(new Locale("fa"));
private Collator collator =
Collator.getInstance(new Locale.Builder().setLanguageTag("fa").build());
private Analyzer analyzer;
@Override

View File

@ -17,47 +17,44 @@
package org.apache.lucene.benchmark.byTask.tasks;
import java.util.Locale;
import java.util.StringTokenizer;
import org.apache.lucene.benchmark.byTask.PerfRunData;
/**
* Set a {@link java.util.Locale} for use in benchmarking.
*
* <p>Locales can be specified in the following ways:
* <p>Locales can be specified as BCP-47 language tag or as ROOT or empty string.
*
* <ul>
* <li><code>de</code>: Language "de"
* <li><code>en,US</code>: Language "en", country "US"
* <li><code>no,NO,NY</code>: Language "no", country "NO", variant "NY"
* <li><code>en-US</code>: Language "en", country "US"
* <li><code>ROOT</code>: The root (language-agnostic) Locale
* <li>&lt;empty string&gt;: Erase the Locale (null)
* </ul>
*/
public class NewLocaleTask extends PerfTask {
private String language;
private String country;
private String variant;
private String tag;
/**
* Create a new {@link java.util.Locale} and set it it in the getRunData() for use by all future
* Create a new {@link java.util.Locale} and set it in the getRunData() for use by all future
* tasks.
*/
public NewLocaleTask(PerfRunData runData) {
super(runData);
}
static Locale createLocale(String language, String country, String variant) {
if (language == null || language.length() == 0) return null;
static Locale createLocale(String tag) {
if (tag == null || tag.length() == 0) return null;
String lang = language;
if (lang.equalsIgnoreCase("ROOT")) lang = ""; // empty language is the root locale in the JDK
if (tag.equalsIgnoreCase("ROOT")) {
return Locale.ROOT;
}
return new Locale(lang, country, variant);
return new Locale.Builder().setLanguageTag(tag).build();
}
@Override
public int doLogic() throws Exception {
Locale locale = createLocale(language, country, variant);
Locale locale = createLocale(tag);
getRunData().setLocale(locale);
System.out.println(
"Changed Locale to: "
@ -70,11 +67,7 @@ public class NewLocaleTask extends PerfTask {
@Override
public void setParams(String params) {
super.setParams(params);
language = country = variant = "";
StringTokenizer st = new StringTokenizer(params, ",");
if (st.hasMoreTokens()) language = st.nextToken();
if (st.hasMoreTokens()) country = st.nextToken();
if (st.hasMoreTokens()) variant = st.nextToken();
tag = params;
}
@Override

View File

@ -793,19 +793,17 @@ public class TestPerfTasksLogic extends BenchmarkTestCase {
// ROOT locale
benchmark = execBenchmark(getLocaleConfig("ROOT"));
assertEquals(new Locale(""), benchmark.getRunData().getLocale());
assertEquals(Locale.ROOT, benchmark.getRunData().getLocale());
// specify just a language
benchmark = execBenchmark(getLocaleConfig("de"));
assertEquals(new Locale("de"), benchmark.getRunData().getLocale());
assertEquals(
new Locale.Builder().setLanguageTag("de").build(), benchmark.getRunData().getLocale());
// specify language + country
benchmark = execBenchmark(getLocaleConfig("en,US"));
assertEquals(new Locale("en", "US"), benchmark.getRunData().getLocale());
// specify language + country + variant
benchmark = execBenchmark(getLocaleConfig("no,NO,NY"));
assertEquals(new Locale("no", "NO", "NY"), benchmark.getRunData().getLocale());
benchmark = execBenchmark(getLocaleConfig("en-US"));
assertEquals(
new Locale.Builder().setLanguageTag("en-US").build(), benchmark.getRunData().getLocale());
}
private String[] getLocaleConfig(String localeParam) {
@ -832,22 +830,28 @@ public class TestPerfTasksLogic extends BenchmarkTestCase {
public void testCollator() throws Exception {
// ROOT locale
Benchmark benchmark = execBenchmark(getCollatorConfig("ROOT", "impl:jdk"));
CollationKeyAnalyzer expected = new CollationKeyAnalyzer(Collator.getInstance(new Locale("")));
CollationKeyAnalyzer expected = new CollationKeyAnalyzer(Collator.getInstance(Locale.ROOT));
assertEqualCollation(expected, benchmark.getRunData().getAnalyzer(), "foobar");
// specify just a language
benchmark = execBenchmark(getCollatorConfig("de", "impl:jdk"));
expected = new CollationKeyAnalyzer(Collator.getInstance(new Locale("de")));
expected =
new CollationKeyAnalyzer(
Collator.getInstance(new Locale.Builder().setLanguageTag("de").build()));
assertEqualCollation(expected, benchmark.getRunData().getAnalyzer(), "foobar");
// specify language + country
benchmark = execBenchmark(getCollatorConfig("en,US", "impl:jdk"));
expected = new CollationKeyAnalyzer(Collator.getInstance(new Locale("en", "US")));
benchmark = execBenchmark(getCollatorConfig("en-US", "impl:jdk"));
expected =
new CollationKeyAnalyzer(
Collator.getInstance(new Locale.Builder().setLanguageTag("en-US").build()));
assertEqualCollation(expected, benchmark.getRunData().getAnalyzer(), "foobar");
// specify language + country + variant
benchmark = execBenchmark(getCollatorConfig("no,NO,NY", "impl:jdk"));
expected = new CollationKeyAnalyzer(Collator.getInstance(new Locale("no", "NO", "NY")));
benchmark = execBenchmark(getCollatorConfig("nn-NO", "impl:jdk"));
expected =
new CollationKeyAnalyzer(
Collator.getInstance(new Locale.Builder().setLanguageTag("nn-NO").build()));
assertEqualCollation(expected, benchmark.getRunData().getAnalyzer(), "foobar");
}

View File

@ -96,7 +96,7 @@ public class TestHtmlParser extends LuceneTestCase {
public void testTurkish() throws Exception {
final Locale saved = Locale.getDefault();
try {
Locale.setDefault(new Locale("tr", "TR"));
Locale.setDefault(new Locale.Builder().setLanguageTag("tr-TR").build());
String text =
"<html><HEAD><TITLE>ııı</TITLE></head><body>"
+ "<IMG SRC=\"../images/head.jpg\" WIDTH=570 HEIGHT=47 BORDER=0 ALT=\"ş\">"

View File

@ -421,7 +421,7 @@ public final class QueryParserPaneProvider implements QueryParserTabOperator {
.fuzzyMinSim(fuzzyMinSimFloat)
.fuzzyPrefixLength(fuzzyPrefLenInt)
.dateResolution(DateTools.Resolution.valueOf((String) dateResCB.getSelectedItem()))
.locale(new Locale(locationTF.getText()))
.locale(new Locale.Builder().setLanguageTag(locationTF.getText()).build())
.timeZone(TimeZone.getTimeZone(timezoneTF.getText()))
.typeMap(typeMap)
.build();

View File

@ -68,7 +68,7 @@ public class TestNLS extends LuceneTestCase {
}
public void testNLSLoading_xx_XX() {
Locale locale = new Locale("xx", "XX", "");
Locale locale = new Locale.Builder().setLanguageTag("xx-XX").build();
String message =
NLS.getLocalizedMessage(
MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, locale);