summaryrefslogtreecommitdiff
path: root/android_icu4j/libcore_bridge/src/java/com/android/i18n/timezone/CountryZonesFinder.java
blob: 80a316e1f3e10dbb42b5180d8d72c833a4031eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.i18n.timezone;

import static com.android.i18n.timezone.XmlUtils.normalizeCountryIso;

import com.android.i18n.timezone.CountryTimeZones.TimeZoneMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * An in-memory representation of country <-> time zone mapping data.
 * @hide
 */
@libcore.api.CorePlatformApi
public final class CountryZonesFinder {

    private final List<CountryTimeZones> countryTimeZonesList;

    CountryZonesFinder(List<CountryTimeZones> countryTimeZonesList) {
        this.countryTimeZonesList = new ArrayList<>(countryTimeZonesList);
    }

    // VisibleForTesting
    public static CountryZonesFinder createForTests(List<CountryTimeZones> countryTimeZonesList) {
        return new CountryZonesFinder(countryTimeZonesList);
    }

    /**
     * Returns an immutable list of country ISO codes with time zones. The codes can be passed to
     * {@link #lookupCountryTimeZones(String)} and similar methods.
     */
    @libcore.api.CorePlatformApi
    public List<String> lookupAllCountryIsoCodes() {
        List<String> isoCodes = new ArrayList<>(countryTimeZonesList.size());
        for (CountryTimeZones countryTimeZones : countryTimeZonesList) {
            isoCodes.add(countryTimeZones.getCountryIso());
        }
        return Collections.unmodifiableList(isoCodes);
    }

    /**
     * Returns an immutable list of {@link CountryTimeZones} for countries that use the specified
     * time zone. An exact, case-sensitive match is performed on the zone ID. If the match  but the method also
     * checks for alternative zone IDs. This method never returns null and will usually return a
     * list containing a single element. It can return an empty list if the zone ID is
     * not recognized or it is not associated with a country.
     */
    @libcore.api.CorePlatformApi
    public List<CountryTimeZones> lookupCountryTimeZonesForZoneId(String zoneId) {
        List<CountryTimeZones> matches = new ArrayList<>(2);

        // This implementation is deliberately flexible about supporting alternative (newer or
        // legacy) IDs, e.g. zoneId might have come from the device's persist.sys.timezone setting,
        // which may have been set before a tzdb upgrade, so we look at alternative IDs and accept
        // them too. Most of the ~250 countries have a small number of zones (most have 1-2, the max
        // is ~30), and most zones do not have an alternative ID, those that do have 1-2.
        for (CountryTimeZones countryTimeZones : countryTimeZonesList) {
            boolean match = false;
            // We get all time zone mappings, even those with a notafter= value to ensure the most
            // complete search.
            List<TimeZoneMapping> countryTimeZoneMappings = countryTimeZones.getTimeZoneMappings();
            for (TimeZoneMapping timeZoneMapping : countryTimeZoneMappings) {
                if (timeZoneMapping.getTimeZoneId().equals(zoneId)
                        || timeZoneMapping.getAlternativeIds().contains(zoneId)) {
                    match = true;
                    break;
                }
            }
            if (match) {
                matches.add(countryTimeZones);
            }
        }
        return Collections.unmodifiableList(matches);
    }

    /**
     * Returns a {@link CountryTimeZones} object associated with the specified country code. If one
     * cannot be found this method returns {@code null}.
     */
    @libcore.api.CorePlatformApi
    public CountryTimeZones lookupCountryTimeZones(String countryIso) {
        String normalizedCountryIso = normalizeCountryIso(countryIso);
        for (CountryTimeZones countryTimeZones : countryTimeZonesList) {
            if (countryTimeZones.getCountryIso().equals(normalizedCountryIso)) {
                return countryTimeZones;
            }
        }
        return null;
    }
}