summaryrefslogtreecommitdiff
path: root/repackaged/libphonenumber/src/com/android/i18n/phonenumbers/SingleFileMetadataSourceImpl.java
blob: 8b15cbb86c5f4feefe8a9d9cc3df95e0f7529f7c (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
/* GENERATED SOURCE. DO NOT MODIFY. */
/*
 * Copyright (C) 2015 The Libphonenumber Authors
 *
 * 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.phonenumbers;

import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Implementation of {@link MetadataSource} that reads from a single resource file.
 */
final class SingleFileMetadataSourceImpl implements MetadataSource {
  // The name of the binary file containing phone number metadata for different regions.
  // This enables us to set up with different metadata, such as for testing.
  private final String phoneNumberMetadataFileName;

  // The {@link MetadataLoader} used to inject alternative metadata sources.
  private final MetadataLoader metadataLoader;

  private final AtomicReference<MetadataManager.SingleFileMetadataMaps> phoneNumberMetadataRef =
      new AtomicReference<MetadataManager.SingleFileMetadataMaps>();

  // It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
  // @VisibleForTesting
  SingleFileMetadataSourceImpl(String phoneNumberMetadataFileName, MetadataLoader metadataLoader) {
    this.phoneNumberMetadataFileName = phoneNumberMetadataFileName;
    this.metadataLoader = metadataLoader;
  }

  // It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
  SingleFileMetadataSourceImpl(MetadataLoader metadataLoader) {
    this(MetadataManager.SINGLE_FILE_PHONE_NUMBER_METADATA_FILE_NAME, metadataLoader);
  }

  @Override
  public PhoneMetadata getMetadataForRegion(String regionCode) {
    return MetadataManager.getSingleFileMetadataMaps(phoneNumberMetadataRef,
        phoneNumberMetadataFileName, metadataLoader).get(regionCode);
  }

  @Override
  public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) {
    // A country calling code is non-geographical if it only maps to the non-geographical region
    // code, i.e. "001". If this is not true of the given country calling code, then we will return
    // null here. If not for the atomic reference, such as if we were loading in multiple stages, we
    // would check that the passed in country calling code was indeed non-geographical to avoid
    // loading costs for a null result. Here though we do not check this since the entire data must
    // be loaded anyway if any of it is needed at some point in the life cycle of this class.
    return MetadataManager.getSingleFileMetadataMaps(phoneNumberMetadataRef,
        phoneNumberMetadataFileName, metadataLoader).get(countryCallingCode);
  }
}