aboutsummaryrefslogtreecommitdiff
path: root/s2storage/src/readonly/java/com/android/timezone/location/storage/tzs2range/BankedTzIdSets.java
blob: d92fccde98231b0993d95be387515ec76785088e (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (C) 2020 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.timezone.location.storage.tzs2range;

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

/**
 * An immutable data structure for storing sets of TZ IDs to be referenced by geo data. The
 * structure is banked so that a limited range of IDs can be used to access the sets. i.e. a chunk
 * of geo data can associated with a specific bank and then all individual set IDs are scoped to
 * that bank. This reduces the number of bits needed to store sets of TZ IDs.
 */
public final class BankedTzIdSets {

    private final List<String> mStringsByIndex;

    private final List<Bank> mBanks;

    private BankedTzIdSets(List<String> stringsByIndex, List<Bank> banks) {
        mStringsByIndex = Collections.unmodifiableList(new ArrayList<>(stringsByIndex));
        mBanks = Collections.unmodifiableList(new ArrayList<>(banks));
    }

    /** Returns the bank associated with the ID. */
    public Bank getBank(int bankId) {
        return mBanks.get(bankId);
    }

    /** Returns the number of banks. */
    public int getBankCount() {
        return mBanks.size();
    }

    /** Returns the Strings referenced by every bank. */
    public List<String> getStringsByIndex() {
        return mStringsByIndex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        BankedTzIdSets that = (BankedTzIdSets) o;
        return mStringsByIndex.equals(that.mStringsByIndex)
                && mBanks.equals(that.mBanks);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mStringsByIndex, mBanks);
    }

    @Override
    public String toString() {
        return "BankedTzIdSets{"
                + "mStringsByIndex=" + mStringsByIndex
                + ", mBanks=" + mBanks
                + '}';
    }

    /** A builder of {@link BankedTzIdSets}. */
    public static class Builder {

        private final List<String> mStringsByIndex = new ArrayList<>();

        private final List<BankBuilder> mBankBuilders = new ArrayList<>();

        private int mNextBankId = 0;

        /**
         * Sets the time zone ID strings.
         */
        public Builder setStringsByIndex(List<String> stringsByIndex) {
            if (!this.mStringsByIndex.isEmpty()) {
                throw new IllegalStateException("stringsByIndex is already set");
            }
            mStringsByIndex.addAll(stringsByIndex);
            return this;
        }

        /**
         * Adds a new Bank, returns the {@link BankBuilder} to use to populate it. The
         * {@link Builder} keeps track of each {@link BankBuilder} it generates and will call
         * {@link BankBuilder#build()} from {@link #build()}.
         */
        public BankBuilder newBank() {
            BankBuilder bankBuilder = new BankBuilder(mNextBankId++);
            mBankBuilders.add(bankBuilder);
            return bankBuilder;
        }

        /**
         * Builds the {@link BankedTzIdSets}.
         */
        public BankedTzIdSets build() {
            List<Bank> banks = new ArrayList<>(mBankBuilders.size());
            for (BankBuilder bankBuilder : mBankBuilders) {
                banks.add(bankBuilder.build());
            }
            return new BankedTzIdSets(mStringsByIndex, banks);
        }

        /**
         * A builder of {@link Bank} instances.
         */
        public final class BankBuilder {

            private final List<TzIdSet> mTzIdSets = new ArrayList<>();

            private final int mId;

            BankBuilder(int id) {
                mId = id;
            }

            /**
             * Adds a new set of IDs to the bank. The IDs are the indexes of strings held at the
             * {@link BankedTzIdSets} level (see {@link BankedTzIdSets#getStringsByIndex()}.
             */
            public BankBuilder addTzIdSet(List<Integer> stringIds) {
                List<String> strings = new ArrayList<>(stringIds.size());
                for (int stringId : stringIds) {
                    String string = mStringsByIndex.get(stringId);
                    strings.add(string);
                }
                BankedTzIdSets.TzIdSet tzIdSet = new TzIdSet(stringIds, strings);
                mTzIdSets.add(tzIdSet);
                return this;
            }

            /** Builds and returns the {@link Bank}. */
            public Bank build() {
                return new Bank(mId, mTzIdSets);
            }
        }
    }

    /** An immutable bank of sets of time zone IDs. */
    public static class Bank {

        private final int mId;

        private final List<TzIdSet> mTzIdSets;

        Bank(int id, List<TzIdSet> tzIdSets) {
            mId = id;
            mTzIdSets = Collections.unmodifiableList(new ArrayList<>(tzIdSets));
        }

        public int getId() {
            return mId;
        }

        public TzIdSet getTzIdSet(int index) {
            return mTzIdSets.get(index);
        }

        public int getTzIdSetCount() {
            return mTzIdSets.size();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Bank bank = (Bank) o;
            return mId == bank.mId
                    && mTzIdSets.equals(bank.mTzIdSets);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mId, mTzIdSets);
        }

        @Override
        public String toString() {
            return "Bank{"
                    + "mId=" + mId
                    + ", mTzIdSets=" + mTzIdSets
                    + '}';
        }
    }

    /** A set of time zone IDs. */
    public static class TzIdSet {

        private final List<Integer> mStringIds;

        private final List<String> mStrings;

        TzIdSet(List<Integer> stringIds, List<String> strings) {
            mStringIds = Collections.unmodifiableList(new ArrayList<>(stringIds));
            mStrings = Collections.unmodifiableList(new ArrayList<>(strings));
        }

        /**
         * Returns the IDs of the strings in this set, referencing
         * {@link BankedTzIdSets#getStringsByIndex()}.
         */
        public List<Integer> getStringIds() {
            return mStringIds;
        }

        /**
         * Returns the strings in this set.
         */
        public List<String> getTzIds() {
            return mStrings;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            TzIdSet tzIdSet = (TzIdSet) o;
            return mStringIds.equals(tzIdSet.mStringIds)
                    && mStrings.equals(tzIdSet.mStrings);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mStringIds, mStrings);
        }
    }
}