8276302: Locale.filterTags methods ignore actual weight when matching "*" (as if it is 1)

Reviewed-by: naoto
This commit is contained in:
Daniel Le 2021-12-23 14:11:11 +00:00 committed by Naoto Sato
parent 214f98f6b0
commit 87cc4e5009
2 changed files with 73 additions and 75 deletions

View file

@ -124,8 +124,18 @@ public final class LocaleMatcher {
for (LanguageRange lr : nonZeroRanges) {
String range = lr.getRange();
if (range.equals("*")) {
tags = removeTagsMatchingBasicZeroRange(zeroRanges, tags);
return new ArrayList<String>(tags);
for (String tag : tags) {
// change to lowercase for case-insensitive matching
String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
if (!caseInsensitiveMatch(list, lowerCaseTag)
&& !shouldIgnoreFilterBasicMatch(zeroRanges, lowerCaseTag)) {
// preserving the case of the input tag
list.add(tag);
}
}
break;
} else {
for (String tag : tags) {
// change to lowercase for case-insensitive matching
@ -148,44 +158,6 @@ public final class LocaleMatcher {
return list;
}
/**
* Removes the tag(s) which are falling in the basic exclusion range(s) i.e
* range(s) with q=0 and returns the updated collection. If the basic
* language ranges contains '*' as one of its non zero range then instead of
* returning all the tags, remove those which are matching the range with
* quality weight q=0.
*/
private static Collection<String> removeTagsMatchingBasicZeroRange(
List<LanguageRange> zeroRange, Collection<String> tags) {
if (zeroRange.isEmpty()) {
tags = removeDuplicates(tags);
return tags;
}
List<String> matchingTags = new ArrayList<>();
for (String tag : tags) {
// change to lowercase for case-insensitive matching
String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
if (!shouldIgnoreFilterBasicMatch(zeroRange, lowerCaseTag)
&& !caseInsensitiveMatch(matchingTags, lowerCaseTag)) {
matchingTags.add(tag); // preserving the case of the input tag
}
}
return matchingTags;
}
/**
* Remove duplicate tags from the given {@code tags} by
* ignoring case considerations.
*/
private static Collection<String> removeDuplicates(
Collection<String> tags) {
Set<String> distinctTags = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
return tags.stream().filter(x -> distinctTags.add(x))
.toList();
}
/**
* Returns true if the given {@code list} contains an element which matches
* with the given {@code tag} ignoring case considerations.
@ -240,8 +212,18 @@ public final class LocaleMatcher {
for (LanguageRange lr : nonZeroRanges) {
String range = lr.getRange();
if (range.equals("*")) {
tags = removeTagsMatchingExtendedZeroRange(zeroRanges, tags);
return new ArrayList<String>(tags);
for (String tag : tags) {
// change to lowercase for case-insensitive matching
String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
if (!caseInsensitiveMatch(list, lowerCaseTag)
&& !shouldIgnoreFilterExtendedMatch(zeroRanges, lowerCaseTag)) {
// preserving the case of the input tag
list.add(tag);
}
}
break;
}
String[] rangeSubtags = range.split("-");
for (String tag : tags) {
@ -267,33 +249,6 @@ public final class LocaleMatcher {
return list;
}
/**
* Removes the tag(s) which are falling in the extended exclusion range(s)
* i.e range(s) with q=0 and returns the updated collection. If the extended
* language ranges contains '*' as one of its non zero range then instead of
* returning all the tags, remove those which are matching the range with
* quality weight q=0.
*/
private static Collection<String> removeTagsMatchingExtendedZeroRange(
List<LanguageRange> zeroRange, Collection<String> tags) {
if (zeroRange.isEmpty()) {
tags = removeDuplicates(tags);
return tags;
}
List<String> matchingTags = new ArrayList<>();
for (String tag : tags) {
// change to lowercase for case-insensitive matching
String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
if (!shouldIgnoreFilterExtendedMatch(zeroRange, lowerCaseTag)
&& !caseInsensitiveMatch(matchingTags, lowerCaseTag)) {
matchingTags.add(tag); // preserve the case of the input tag
}
}
return matchingTags;
}
/**
* The tag which is falling in the extended exclusion range(s) should
* not be considered as the matching tag. Ignores the tag matching with the