src/java.base/share/classes/sun/util/locale/LocaleMatcher.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     1
/*
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     4
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    10
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    15
 * accompanied this code).
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    16
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    20
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    23
 * questions.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    24
 */
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    25
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    26
package sun.util.locale;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    27
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    28
import java.util.ArrayList;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    29
import java.util.Collection;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    30
import java.util.HashMap;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    31
import java.util.List;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    32
import java.util.Locale;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    33
import java.util.Locale.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    34
import static java.util.Locale.FilteringMode.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    35
import static java.util.Locale.LanguageRange.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    36
import java.util.Map;
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
    37
import java.util.Set;
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
    38
import java.util.TreeSet;
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
    39
import java.util.stream.Collectors;
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    40
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    41
/**
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    42
 * Implementation for BCP47 Locale matching
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    43
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    44
 */
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    45
public final class LocaleMatcher {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    46
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    47
    public static List<Locale> filter(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    48
                                      Collection<Locale> locales,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    49
                                      FilteringMode mode) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    50
        if (priorityList.isEmpty() || locales.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    51
            return new ArrayList<>(); // need to return a empty mutable List
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    52
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    53
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    54
        // Create a list of language tags to be matched.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    55
        List<String> tags = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    56
        for (Locale locale : locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    57
            tags.add(locale.toLanguageTag());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    58
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    59
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    60
        // Filter language tags.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    61
        List<String> filteredTags = filterTags(priorityList, tags, mode);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    62
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    63
        // Create a list of matching locales.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    64
        List<Locale> filteredLocales = new ArrayList<>(filteredTags.size());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    65
        for (String tag : filteredTags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    66
              filteredLocales.add(Locale.forLanguageTag(tag));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    67
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    68
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    69
        return filteredLocales;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    70
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    71
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    72
    public static List<String> filterTags(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    73
                                          Collection<String> tags,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    74
                                          FilteringMode mode) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    75
        if (priorityList.isEmpty() || tags.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    76
            return new ArrayList<>(); // need to return a empty mutable List
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    77
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    78
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    79
        ArrayList<LanguageRange> list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    80
        if (mode == EXTENDED_FILTERING) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    81
            return filterExtended(priorityList, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    82
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    83
            list = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    84
            for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    85
                String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    86
                if (range.startsWith("*-")
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    87
                    || range.indexOf("-*") != -1) { // Extended range
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    88
                    if (mode == AUTOSELECT_FILTERING) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    89
                        return filterExtended(priorityList, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    90
                    } else if (mode == MAP_EXTENDED_RANGES) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    91
                        if (range.charAt(0) == '*') {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    92
                            range = "*";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    93
                        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    94
                            range = range.replaceAll("-[*]", "");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    95
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    96
                        list.add(new LanguageRange(range, lr.getWeight()));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    97
                    } else if (mode == REJECT_EXTENDED_RANGES) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    98
                        throw new IllegalArgumentException("An extended range \""
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    99
                                      + range
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   100
                                      + "\" found in REJECT_EXTENDED_RANGES mode.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   101
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   102
                } else { // Basic range
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   103
                    list.add(lr);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   104
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   105
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   106
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   107
            return filterBasic(list, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   108
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   109
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   110
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   111
    private static List<String> filterBasic(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   112
                                            Collection<String> tags) {
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   113
        int splitIndex = splitRanges(priorityList);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   114
        List<LanguageRange> nonZeroRanges;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   115
        List<LanguageRange> zeroRanges;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   116
        if (splitIndex != -1) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   117
            nonZeroRanges = priorityList.subList(0, splitIndex);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   118
            zeroRanges = priorityList.subList(splitIndex, priorityList.size());
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   119
        } else {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   120
            nonZeroRanges = priorityList;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   121
            zeroRanges = List.of();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   122
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   123
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   124
        List<String> list = new ArrayList<>();
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   125
        for (LanguageRange lr : nonZeroRanges) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   126
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   127
            if (range.equals("*")) {
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   128
                tags = removeTagsMatchingBasicZeroRange(zeroRanges, tags);
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   129
                return new ArrayList<String>(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   130
            } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   131
                for (String tag : tags) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   132
                    // change to lowercase for case-insensitive matching
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   133
                    String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   134
                    if (lowerCaseTag.startsWith(range)) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   135
                        int len = range.length();
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   136
                        if ((lowerCaseTag.length() == len
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   137
                                || lowerCaseTag.charAt(len) == '-')
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   138
                            && !caseInsensitiveMatch(list, lowerCaseTag)
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   139
                            && !shouldIgnoreFilterBasicMatch(zeroRanges,
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   140
                                    lowerCaseTag)) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   141
                            // preserving the case of the input tag
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   142
                            list.add(tag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   143
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   144
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   145
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   146
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   147
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   148
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   149
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   150
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   151
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   152
    /**
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   153
     * Removes the tag(s) which are falling in the basic exclusion range(s) i.e
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   154
     * range(s) with q=0 and returns the updated collection. If the basic
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   155
     * language ranges contains '*' as one of its non zero range then instead of
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   156
     * returning all the tags, remove those which are matching the range with
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   157
     * quality weight q=0.
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   158
     */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   159
    private static Collection<String> removeTagsMatchingBasicZeroRange(
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   160
            List<LanguageRange> zeroRange, Collection<String> tags) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   161
        if (zeroRange.isEmpty()) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   162
            tags = removeDuplicates(tags);
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   163
            return tags;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   164
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   165
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   166
        List<String> matchingTags = new ArrayList<>();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   167
        for (String tag : tags) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   168
            // change to lowercase for case-insensitive matching
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   169
            String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   170
            if (!shouldIgnoreFilterBasicMatch(zeroRange, lowerCaseTag)
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   171
                    && !caseInsensitiveMatch(matchingTags, lowerCaseTag)) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   172
                matchingTags.add(tag); // preserving the case of the input tag
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   173
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   174
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   175
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   176
        return matchingTags;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   177
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   178
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   179
    /**
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   180
     * Remove duplicate tags from the given {@code tags} by
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   181
     * ignoring case considerations.
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   182
     */
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   183
    private static Collection<String> removeDuplicates(
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   184
            Collection<String> tags) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   185
        Set<String> distinctTags = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   186
        return tags.stream().filter(x -> distinctTags.add(x))
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   187
                .collect(Collectors.toList());
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   188
    }
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   189
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   190
    /**
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   191
     * Returns true if the given {@code list} contains an element which matches
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   192
     * with the given {@code tag} ignoring case considerations.
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   193
     */
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   194
    private static boolean caseInsensitiveMatch(List<String> list, String tag) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   195
        return list.stream().anyMatch((element)
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   196
                -> (element.equalsIgnoreCase(tag)));
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   197
    }
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   198
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   199
    /**
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   200
     * The tag which is falling in the basic exclusion range(s) should not
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   201
     * be considered as the matching tag. Ignores the tag matching with the
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   202
     * non-zero ranges, if the tag also matches with one of the basic exclusion
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   203
     * ranges i.e. range(s) having quality weight q=0
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   204
     */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   205
    private static boolean shouldIgnoreFilterBasicMatch(
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   206
            List<LanguageRange> zeroRange, String tag) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   207
        if (zeroRange.isEmpty()) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   208
            return false;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   209
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   210
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   211
        for (LanguageRange lr : zeroRange) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   212
            String range = lr.getRange();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   213
            if (range.equals("*")) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   214
                return true;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   215
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   216
            if (tag.startsWith(range)) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   217
                int len = range.length();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   218
                if ((tag.length() == len || tag.charAt(len) == '-')) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   219
                    return true;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   220
                }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   221
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   222
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   223
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   224
        return false;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   225
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   226
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   227
    private static List<String> filterExtended(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   228
                                               Collection<String> tags) {
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   229
        int splitIndex = splitRanges(priorityList);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   230
        List<LanguageRange> nonZeroRanges;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   231
        List<LanguageRange> zeroRanges;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   232
        if (splitIndex != -1) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   233
            nonZeroRanges = priorityList.subList(0, splitIndex);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   234
            zeroRanges = priorityList.subList(splitIndex, priorityList.size());
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   235
        } else {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   236
            nonZeroRanges = priorityList;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   237
            zeroRanges = List.of();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   238
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   239
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   240
        List<String> list = new ArrayList<>();
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   241
        for (LanguageRange lr : nonZeroRanges) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   242
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   243
            if (range.equals("*")) {
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   244
                tags = removeTagsMatchingExtendedZeroRange(zeroRanges, tags);
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   245
                return new ArrayList<String>(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   246
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   247
            String[] rangeSubtags = range.split("-");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   248
            for (String tag : tags) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   249
                // change to lowercase for case-insensitive matching
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   250
                String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   251
                String[] tagSubtags = lowerCaseTag.split("-");
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   252
                if (!rangeSubtags[0].equals(tagSubtags[0])
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   253
                    && !rangeSubtags[0].equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   254
                    continue;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   255
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   256
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   257
                int rangeIndex = matchFilterExtendedSubtags(rangeSubtags,
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   258
                        tagSubtags);
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   259
                if (rangeSubtags.length == rangeIndex
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   260
                        && !caseInsensitiveMatch(list, lowerCaseTag)
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   261
                        && !shouldIgnoreFilterExtendedMatch(zeroRanges,
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   262
                                lowerCaseTag)) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   263
                    list.add(tag); // preserve the case of the input tag
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   264
                }
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   265
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   266
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   267
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   268
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   269
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   270
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   271
    /**
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   272
     * Removes the tag(s) which are falling in the extended exclusion range(s)
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   273
     * i.e range(s) with q=0 and returns the updated collection. If the extended
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   274
     * language ranges contains '*' as one of its non zero range then instead of
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   275
     * returning all the tags, remove those which are matching the range with
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   276
     * quality weight q=0.
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   277
     */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   278
    private static Collection<String> removeTagsMatchingExtendedZeroRange(
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   279
            List<LanguageRange> zeroRange, Collection<String> tags) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   280
        if (zeroRange.isEmpty()) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   281
            tags = removeDuplicates(tags);
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   282
            return tags;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   283
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   284
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   285
        List<String> matchingTags = new ArrayList<>();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   286
        for (String tag : tags) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   287
            // change to lowercase for case-insensitive matching
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   288
            String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   289
            if (!shouldIgnoreFilterExtendedMatch(zeroRange, lowerCaseTag)
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   290
                    && !caseInsensitiveMatch(matchingTags, lowerCaseTag)) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   291
                matchingTags.add(tag); // preserve the case of the input tag
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   292
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   293
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   294
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   295
        return matchingTags;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   296
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   297
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   298
    /**
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   299
     * The tag which is falling in the extended exclusion range(s) should
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   300
     * not be considered as the matching tag. Ignores the tag matching with the
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   301
     * non zero range(s), if the tag also matches with one of the extended
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   302
     * exclusion range(s) i.e. range(s) having quality weight q=0
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   303
     */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   304
    private static boolean shouldIgnoreFilterExtendedMatch(
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   305
            List<LanguageRange> zeroRange, String tag) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   306
        if (zeroRange.isEmpty()) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   307
            return false;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   308
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   309
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   310
        String[] tagSubtags = tag.split("-");
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   311
        for (LanguageRange lr : zeroRange) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   312
            String range = lr.getRange();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   313
            if (range.equals("*")) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   314
                return true;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   315
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   316
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   317
            String[] rangeSubtags = range.split("-");
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   318
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   319
            if (!rangeSubtags[0].equals(tagSubtags[0])
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   320
                    && !rangeSubtags[0].equals("*")) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   321
                continue;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   322
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   323
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   324
            int rangeIndex = matchFilterExtendedSubtags(rangeSubtags,
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   325
                    tagSubtags);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   326
            if (rangeSubtags.length == rangeIndex) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   327
                return true;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   328
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   329
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   330
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   331
        return false;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   332
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   333
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   334
    private static int matchFilterExtendedSubtags(String[] rangeSubtags,
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   335
            String[] tagSubtags) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   336
        int rangeIndex = 1;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   337
        int tagIndex = 1;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   338
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   339
        while (rangeIndex < rangeSubtags.length
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   340
                && tagIndex < tagSubtags.length) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   341
            if (rangeSubtags[rangeIndex].equals("*")) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   342
                rangeIndex++;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   343
            } else if (rangeSubtags[rangeIndex]
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   344
                    .equals(tagSubtags[tagIndex])) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   345
                rangeIndex++;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   346
                tagIndex++;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   347
            } else if (tagSubtags[tagIndex].length() == 1
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   348
                    && !tagSubtags[tagIndex].equals("*")) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   349
                break;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   350
            } else {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   351
                tagIndex++;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   352
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   353
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   354
        return rangeIndex;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   355
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   356
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   357
    public static Locale lookup(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   358
                                Collection<Locale> locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   359
        if (priorityList.isEmpty() || locales.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   360
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   361
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   362
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   363
        // Create a list of language tags to be matched.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   364
        List<String> tags = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   365
        for (Locale locale : locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   366
            tags.add(locale.toLanguageTag());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   367
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   368
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   369
        // Look up a language tags.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   370
        String lookedUpTag = lookupTag(priorityList, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   371
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   372
        if (lookedUpTag == null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   373
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   374
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   375
            return Locale.forLanguageTag(lookedUpTag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   376
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   377
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   378
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   379
    public static String lookupTag(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   380
                                   Collection<String> tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   381
        if (priorityList.isEmpty() || tags.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   382
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   383
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   384
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   385
        int splitIndex = splitRanges(priorityList);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   386
        List<LanguageRange> nonZeroRanges;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   387
        List<LanguageRange> zeroRanges;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   388
        if (splitIndex != -1) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   389
            nonZeroRanges = priorityList.subList(0, splitIndex);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   390
            zeroRanges = priorityList.subList(splitIndex, priorityList.size());
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   391
        } else {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   392
            nonZeroRanges = priorityList;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   393
            zeroRanges = List.of();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   394
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   395
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   396
        for (LanguageRange lr : nonZeroRanges) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   397
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   398
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   399
            // Special language range ("*") is ignored in lookup.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   400
            if (range.equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   401
                continue;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   402
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   403
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   404
            String rangeForRegex = range.replace("*", "\\p{Alnum}*");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   405
            while (!rangeForRegex.isEmpty()) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   406
                for (String tag : tags) {
45838
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   407
                    // change to lowercase for case-insensitive matching
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   408
                    String lowerCaseTag = tag.toLowerCase(Locale.ROOT);
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   409
                    if (lowerCaseTag.matches(rangeForRegex)
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   410
                            && !shouldIgnoreLookupMatch(zeroRanges, lowerCaseTag)) {
c6223c3b4594 8032842: Locale.filterTags()/lookupTag() methods return lowercased language tags
nishjain
parents: 41955
diff changeset
   411
                        return tag; // preserve the case of the input tag
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   412
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   413
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   414
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   415
                // Truncate from the end....
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   416
                rangeForRegex = truncateRange(rangeForRegex);
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   417
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   418
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   419
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   420
        return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   421
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   422
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   423
    /**
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   424
     * The tag which is falling in the exclusion range(s) should not be
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   425
     * considered as the matching tag. Ignores the tag matching with the
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   426
     * non zero range(s), if the tag also matches with one of the exclusion
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   427
     * range(s) i.e. range(s) having quality weight q=0.
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   428
     */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   429
    private static boolean shouldIgnoreLookupMatch(List<LanguageRange> zeroRange,
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   430
            String tag) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   431
        for (LanguageRange lr : zeroRange) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   432
            String range = lr.getRange();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   433
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   434
            // Special language range ("*") is ignored in lookup.
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   435
            if (range.equals("*")) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   436
                continue;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   437
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   438
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   439
            String rangeForRegex = range.replace("*", "\\p{Alnum}*");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   440
            while (!rangeForRegex.isEmpty()) {
40114
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   441
                if (tag.matches(rangeForRegex)) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   442
                    return true;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   443
                }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   444
                // Truncate from the end....
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   445
                rangeForRegex = truncateRange(rangeForRegex);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   446
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   447
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   448
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   449
        return false;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   450
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   451
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   452
    /* Truncate the range from end during the lookup match */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   453
    private static String truncateRange(String rangeForRegex) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   454
        int index = rangeForRegex.lastIndexOf('-');
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   455
        if (index >= 0) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   456
            rangeForRegex = rangeForRegex.substring(0, index);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   457
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   458
            // if range ends with an extension key, truncate it.
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   459
            index = rangeForRegex.lastIndexOf('-');
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   460
            if (index >= 0 && index == rangeForRegex.length() - 2) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   461
                rangeForRegex
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   462
                        = rangeForRegex.substring(0, rangeForRegex.length() - 2);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   463
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   464
        } else {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   465
            rangeForRegex = "";
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   466
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   467
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   468
        return rangeForRegex;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   469
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   470
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   471
    /* Returns the split index of the priority list, if it contains
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   472
     * language range(s) with quality weight as 0 i.e. q=0, else -1
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   473
     */
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   474
    private static int splitRanges(List<LanguageRange> priorityList) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   475
        int size = priorityList.size();
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   476
        for (int index = 0; index < size; index++) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   477
            LanguageRange range = priorityList.get(index);
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   478
            if (range.getWeight() == 0) {
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   479
                return index;
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   480
            }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   481
        }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   482
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   483
        return -1; // no q=0 range exists
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   484
    }
0194b0ba95c6 8035133: Locale matching: Weight q=0 isn't handled correctly.
nishjain
parents: 39053
diff changeset
   485
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   486
    public static List<LanguageRange> parse(String ranges) {
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   487
        ranges = ranges.replace(" ", "").toLowerCase(Locale.ROOT);
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   488
        if (ranges.startsWith("accept-language:")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   489
            ranges = ranges.substring(16); // delete unnecessary prefix
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   490
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   491
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   492
        String[] langRanges = ranges.split(",");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   493
        List<LanguageRange> list = new ArrayList<>(langRanges.length);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   494
        List<String> tempList = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   495
        int numOfRanges = 0;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   496
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   497
        for (String range : langRanges) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   498
            int index;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   499
            String r;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   500
            double w;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   501
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   502
            if ((index = range.indexOf(";q=")) == -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   503
                r = range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   504
                w = MAX_WEIGHT;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   505
            } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   506
                r = range.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   507
                index += 3;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   508
                try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   509
                    w = Double.parseDouble(range.substring(index));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   510
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   511
                catch (Exception e) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   512
                    throw new IllegalArgumentException("weight=\""
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   513
                                  + range.substring(index)
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   514
                                  + "\" for language range \"" + r + "\"");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   515
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   516
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   517
                if (w < MIN_WEIGHT || w > MAX_WEIGHT) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   518
                    throw new IllegalArgumentException("weight=" + w
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   519
                                  + " for language range \"" + r
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   520
                                  + "\". It must be between " + MIN_WEIGHT
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   521
                                  + " and " + MAX_WEIGHT + ".");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   522
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   523
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   524
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   525
            if (!tempList.contains(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   526
                LanguageRange lr = new LanguageRange(r, w);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   527
                index = numOfRanges;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   528
                for (int j = 0; j < numOfRanges; j++) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   529
                    if (list.get(j).getWeight() < w) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   530
                        index = j;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   531
                        break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   532
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   533
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   534
                list.add(index, lr);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   535
                numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   536
                tempList.add(r);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   537
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   538
                // Check if the range has an equivalent using IANA LSR data.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   539
                // If yes, add it to the User's Language Priority List as well.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   540
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   541
                // aa-XX -> aa-YY
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   542
                String equivalent;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   543
                if ((equivalent = getEquivalentForRegionAndVariant(r)) != null
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   544
                    && !tempList.contains(equivalent)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   545
                    list.add(index+1, new LanguageRange(equivalent, w));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   546
                    numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   547
                    tempList.add(equivalent);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   548
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   549
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   550
                String[] equivalents;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   551
                if ((equivalents = getEquivalentsForLanguage(r)) != null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   552
                    for (String equiv: equivalents) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   553
                        // aa-XX -> bb-XX(, cc-XX)
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   554
                        if (!tempList.contains(equiv)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   555
                            list.add(index+1, new LanguageRange(equiv, w));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   556
                            numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   557
                            tempList.add(equiv);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   558
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   559
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   560
                        // bb-XX -> bb-YY(, cc-YY)
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   561
                        equivalent = getEquivalentForRegionAndVariant(equiv);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   562
                        if (equivalent != null
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   563
                            && !tempList.contains(equivalent)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   564
                            list.add(index+1, new LanguageRange(equivalent, w));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   565
                            numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   566
                            tempList.add(equivalent);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   567
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   568
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   569
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   570
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   571
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   572
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   573
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   574
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   575
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   576
    /**
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   577
     * A faster alternative approach to String.replaceFirst(), if the given
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   578
     * string is a literal String, not a regex.
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   579
     */
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   580
    private static String replaceFirstSubStringMatch(String range,
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   581
            String substr, String replacement) {
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   582
        int pos = range.indexOf(substr);
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   583
        if (pos == -1) {
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   584
            return range;
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   585
        } else {
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   586
            return range.substring(0, pos) + replacement
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   587
                    + range.substring(pos + substr.length());
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   588
        }
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   589
    }
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   590
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   591
    private static String[] getEquivalentsForLanguage(String range) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   592
        String r = range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   593
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   594
        while (!r.isEmpty()) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   595
            if (LocaleEquivalentMaps.singleEquivMap.containsKey(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   596
                String equiv = LocaleEquivalentMaps.singleEquivMap.get(r);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   597
                // Return immediately for performance if the first matching
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   598
                // subtag is found.
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   599
                return new String[]{replaceFirstSubStringMatch(range,
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   600
                    r, equiv)};
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   601
            } else if (LocaleEquivalentMaps.multiEquivsMap.containsKey(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   602
                String[] equivs = LocaleEquivalentMaps.multiEquivsMap.get(r);
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   603
                String[] result = new String[equivs.length];
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   604
                for (int i = 0; i < equivs.length; i++) {
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   605
                    result[i] = replaceFirstSubStringMatch(range,
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   606
                            r, equivs[i]);
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   607
                }
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   608
                return result;
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   609
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   610
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   611
            // Truncate the last subtag simply.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   612
            int index = r.lastIndexOf('-');
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   613
            if (index == -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   614
                break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   615
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   616
            r = r.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   617
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   618
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   619
        return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   620
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   621
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   622
    private static String getEquivalentForRegionAndVariant(String range) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   623
        int extensionKeyIndex = getExtentionKeyIndex(range);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   624
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   625
        for (String subtag : LocaleEquivalentMaps.regionVariantEquivMap.keySet()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   626
            int index;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   627
            if ((index = range.indexOf(subtag)) != -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   628
                // Check if the matching text is a valid region or variant.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   629
                if (extensionKeyIndex != Integer.MIN_VALUE
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   630
                    && index > extensionKeyIndex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   631
                    continue;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   632
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   633
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   634
                int len = index + subtag.length();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   635
                if (range.length() == len || range.charAt(len) == '-') {
41955
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   636
                    return replaceFirstSubStringMatch(range, subtag,
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   637
                            LocaleEquivalentMaps.regionVariantEquivMap
6a6fd4af5236 8166994: Improve sun.util.locale.LocaleMatcher
nishjain
parents: 40114
diff changeset
   638
                                    .get(subtag));
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   639
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   640
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   641
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   642
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   643
        return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   644
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   645
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   646
    private static int getExtentionKeyIndex(String s) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   647
        char[] c = s.toCharArray();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   648
        int index = Integer.MIN_VALUE;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   649
        for (int i = 1; i < c.length; i++) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   650
            if (c[i] == '-') {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   651
                if (i - index == 2) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   652
                    return index;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   653
                } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   654
                    index = i;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   655
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   656
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   657
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   658
        return Integer.MIN_VALUE;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   659
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   660
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   661
    public static List<LanguageRange> mapEquivalents(
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   662
                                          List<LanguageRange>priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   663
                                          Map<String, List<String>> map) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   664
        if (priorityList.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   665
            return new ArrayList<>(); // need to return a empty mutable List
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   666
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   667
        if (map == null || map.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   668
            return new ArrayList<LanguageRange>(priorityList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   669
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   670
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   671
        // Create a map, key=originalKey.toLowerCaes(), value=originalKey
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   672
        Map<String, String> keyMap = new HashMap<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   673
        for (String key : map.keySet()) {
39053
668c400a8dd1 8159420: The LanguageRange.parse() method is throwing IllegalArgumentException in Turkish Locale
nishjain
parents: 38950
diff changeset
   674
            keyMap.put(key.toLowerCase(Locale.ROOT), key);
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   675
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   676
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   677
        List<LanguageRange> list = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   678
        for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   679
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   680
            String r = range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   681
            boolean hasEquivalent = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   682
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   683
            while (!r.isEmpty()) {
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   684
                if (keyMap.containsKey(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   685
                    hasEquivalent = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   686
                    List<String> equivalents = map.get(keyMap.get(r));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   687
                    if (equivalents != null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   688
                        int len = r.length();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   689
                        for (String equivalent : equivalents) {
39053
668c400a8dd1 8159420: The LanguageRange.parse() method is throwing IllegalArgumentException in Turkish Locale
nishjain
parents: 38950
diff changeset
   690
                            list.add(new LanguageRange(equivalent.toLowerCase(Locale.ROOT)
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   691
                                     + range.substring(len),
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   692
                                     lr.getWeight()));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   693
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   694
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   695
                    // Return immediately if the first matching subtag is found.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   696
                    break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   697
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   698
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   699
                // Truncate the last subtag simply.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   700
                int index = r.lastIndexOf('-');
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   701
                if (index == -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   702
                    break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   703
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   704
                r = r.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   705
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   706
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   707
            if (!hasEquivalent) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   708
                list.add(lr);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   709
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   710
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   711
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   712
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   713
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   714
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   715
    private LocaleMatcher() {}
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   716
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   717
}