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 package com.android.libcore.timezone.tzlookup.zonetree;
17 
18 import java.time.Instant;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Objects;
22 
23 /**
24  * A record for a country of when zones stopped being (effectively) used.
25  */
26 public final class CountryZoneUsage {
27     private final String isoCode;
28     private final Map<String, Entry> zoneIdEntryMap = new HashMap<>();
29 
CountryZoneUsage(String isoCode)30     public CountryZoneUsage(String isoCode) {
31         this.isoCode = isoCode;
32     }
33 
getIsoCode()34     public String getIsoCode() {
35         return isoCode;
36     }
37 
addEntry(String zoneId, Instant notUsedAfterInstant, String notUsedReplacementId)38     void addEntry(String zoneId, Instant notUsedAfterInstant, String notUsedReplacementId) {
39         if (zoneIdEntryMap.containsKey(zoneId)) {
40             throw new IllegalArgumentException(
41                     "Entry exists for " + zoneId + " for isoCode=" + isoCode);
42         }
43         zoneIdEntryMap.put(zoneId, new Entry(zoneId, notUsedAfterInstant, notUsedReplacementId));
44     }
45 
hasEntry(String zoneId)46     public boolean hasEntry(String zoneId) {
47         return zoneIdEntryMap.containsKey(zoneId);
48     }
49 
getNotUsedAfterInstant(String zoneId)50     public Instant getNotUsedAfterInstant(String zoneId) {
51         Entry entry = zoneIdEntryMap.get(zoneId);
52         if (entry == null) {
53             throw new IllegalArgumentException(
54                     "No entry for " + zoneId + " for isoCode=" + isoCode);
55         }
56         return entry.notUsedAfter;
57     }
58 
getNotUsedReplacementId(String zoneId)59     public String getNotUsedReplacementId(String zoneId) {
60         Entry entry = zoneIdEntryMap.get(zoneId);
61         if (entry == null) {
62             throw new IllegalArgumentException(
63                     "No entry for " + zoneId + " for isoCode=" + isoCode);
64         }
65         return entry.notUsedReplacementId;
66     }
67 
68     private static class Entry {
69         final String zoneId;
70         final Instant notUsedAfter;
71         final String notUsedReplacementId;
72 
Entry(String zoneId, Instant notUsedAfter, String notUsedReplacementId)73         Entry(String zoneId, Instant notUsedAfter, String notUsedReplacementId) {
74             this.zoneId = Objects.requireNonNull(zoneId);
75 
76             if ((notUsedAfter == null) != (notUsedReplacementId == null)) {
77                 throw new IllegalArgumentException(
78                         "Both notUsedAfter and notUsedReplacement, or neither required");
79             }
80             this.notUsedAfter = notUsedAfter;
81             this.notUsedReplacementId = notUsedReplacementId;
82         }
83     }
84 }
85