1 /* 2 * Copyright (C) 2017 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 package com.android.libcore.timezone.tzlookup; 17 18 import com.android.libcore.timezone.tzlookup.proto.CountryZonesFile; 19 import com.google.protobuf.TextFormat; 20 21 import java.io.BufferedReader; 22 import java.io.FileReader; 23 import java.io.IOException; 24 import java.text.ParseException; 25 import java.util.List; 26 import java.util.stream.Collectors; 27 28 /** 29 * A class containing utility methods for details with CountryZonesFile proto objects. 30 */ 31 public final class CountryZonesFileSupport { 32 CountryZonesFileSupport()33 private CountryZonesFileSupport() {} 34 parseCountryZonesTextFile(String file)35 public static CountryZonesFile.CountryZones parseCountryZonesTextFile(String file) 36 throws IOException, ParseException { 37 try (BufferedReader fileReader = new BufferedReader(new FileReader(file))) { 38 CountryZonesFile.CountryZones.Builder builder = 39 CountryZonesFile.CountryZones.newBuilder(); 40 TextFormat.getParser().merge(fileReader, builder); 41 return builder.build(); 42 } catch (TextFormat.ParseException e) { 43 ParseException e2 = new ParseException("Error reading proto file: " + file, 0); 44 e2.initCause(e); 45 throw e2; 46 } 47 } 48 extractIsoCodes(List<CountryZonesFile.Country> countries)49 static List<String> extractIsoCodes(List<CountryZonesFile.Country> countries) { 50 return countries.stream() 51 .map(CountryZonesFile.Country::getIsoCode) 52 .collect(Collectors.toList()); 53 } 54 extractIds(List<CountryZonesFile.TimeZoneMapping> timeZones)55 static List<String> extractIds(List<CountryZonesFile.TimeZoneMapping> timeZones) { 56 return timeZones.stream() 57 .map(CountryZonesFile.TimeZoneMapping::getId) 58 .collect(Collectors.toList()); 59 } 60 } 61