1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.util; 18 19 import java.util.ArrayList; 20 import java.util.Collections; 21 import java.util.List; 22 import libcore.util.CountryTimeZones.TimeZoneMapping; 23 24 /** 25 * An in-memory representation of country <-> time zone mapping data. 26 */ 27 public final class CountryZonesFinder { 28 29 private final List<CountryTimeZones> countryTimeZonesList; 30 31 CountryZonesFinder(List<CountryTimeZones> countryTimeZonesList) { 32 this.countryTimeZonesList = new ArrayList<>(countryTimeZonesList); 33 } 34 35 // VisibleForTesting 36 public static CountryZonesFinder createForTests(List<CountryTimeZones> countryTimeZonesList) { 37 return new CountryZonesFinder(countryTimeZonesList); 38 } 39 40 /** 41 * Returns an immutable list of country ISO codes with time zones. The codes can be passed to 42 * {@link #lookupCountryTimeZones(String)} and similar methods. 43 */ 44 public List<String> lookupAllCountryIsoCodes() { 45 List<String> isoCodes = new ArrayList<>(countryTimeZonesList.size()); 46 for (CountryTimeZones countryTimeZones : countryTimeZonesList) { 47 isoCodes.add(countryTimeZones.getCountryIso()); 48 } 49 return Collections.unmodifiableList(isoCodes); 50 } 51 52 /** 53 * Returns an immutable list of {@link CountryTimeZones} for countries that use the specified 54 * time zone. An exact, case-sensitive match is performed on the zone ID. This method never 55 * returns null. 56 */ 57 public List<CountryTimeZones> lookupCountryTimeZonesForZoneId(String zoneId) { 58 List<CountryTimeZones> matches = new ArrayList<>(2); 59 for (CountryTimeZones countryTimeZones : countryTimeZonesList) { 60 boolean match = TimeZoneMapping.containsTimeZoneId( 61 countryTimeZones.getTimeZoneMappings(), zoneId); 62 if (match) { 63 matches.add(countryTimeZones); 64 } 65 } 66 return Collections.unmodifiableList(matches); 67 } 68 69 /** 70 * Returns a {@link CountryTimeZones} object associated with the specified country code. If one 71 * cannot be found this method returns {@code null}. 72 */ 73 public CountryTimeZones lookupCountryTimeZones(String countryIso) { 74 String normalizedCountryIso = TimeZoneFinder.normalizeCountryIso(countryIso); 75 for (CountryTimeZones countryTimeZones : countryTimeZonesList) { 76 if (countryTimeZones.getCountryIso().equals(normalizedCountryIso)) { 77 return countryTimeZones; 78 } 79 } 80 return null; 81 } 82 } 83