Skip to content

Commit

Permalink
Remove usage of deprecated java.util.Locale constructor (apache#12761)
Browse files Browse the repository at this point in the history
This commit removes usages of the deprecated java.util.Locale constructor, replacing with Locale.Builder.
  • Loading branch information
ChrisHegarty authored Nov 6, 2023
1 parent 1448015 commit 8d4f9e5
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/** 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
Expand All @@ -70,11 +67,7 @@ public int doLogic() throws Exception {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,19 +793,17 @@ public void testLocale() throws Exception {

// 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) {
Expand All @@ -832,22 +830,28 @@ private String[] getLocaleConfig(String localeParam) {
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testTitle() throws Exception {
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=\"ş\">"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public QueryParserConfig getConfig() {
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testNLSLoading_ja() {
}

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);
Expand Down

0 comments on commit 8d4f9e5

Please sign in to comment.