1/*
2 * Copyright (C) 2011 The Libphonenumber Authors
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
17package com.android.i18n.phonenumbers.prefixmapper;
18
19import java.io.IOException;
20import java.io.ObjectInput;
21import java.io.ObjectOutput;
22import java.util.SortedMap;
23import java.util.TreeSet;
24
25/**
26 * Abstracts the way phone prefix data is stored into memory and serialized to a stream. It is used
27 * by {@link PhonePrefixMap} to support the most space-efficient storage strategy according to the
28 * provided data.
29 *
30 * @author Philippe Liard
31 */
32abstract class PhonePrefixMapStorageStrategy {
33  protected int numOfEntries = 0;
34  protected final TreeSet<Integer> possibleLengths = new TreeSet<Integer>();
35
36  /**
37   * Gets the phone number prefix located at the provided {@code index}.
38   *
39   * @param index  the index of the prefix that needs to be returned
40   * @return  the phone number prefix at the provided index
41   */
42  public abstract int getPrefix(int index);
43
44  /**
45   * Gets the description corresponding to the phone number prefix located at the provided {@code
46   * index}. If the description is not available in the current language an empty string is
47   * returned.
48   *
49   * @param index  the index of the phone number prefix that needs to be returned
50   * @return  the description corresponding to the phone number prefix at the provided index
51   */
52  public abstract String getDescription(int index);
53
54  /**
55   * Sets the internal state of the underlying storage implementation from the provided {@code
56   * sortedPhonePrefixMap} that maps phone number prefixes to description strings.
57   *
58   * @param sortedPhonePrefixMap  a sorted map that maps phone number prefixes including country
59   *    calling code to description strings
60   */
61  public abstract void readFromSortedMap(SortedMap<Integer, String> sortedPhonePrefixMap);
62
63  /**
64   * Sets the internal state of the underlying storage implementation reading the provided {@code
65   * objectInput}.
66   *
67   * @param objectInput  the object input stream from which the phone prefix map is read
68   * @throws IOException  if an error occurred reading the provided input stream
69   */
70  public abstract void readExternal(ObjectInput objectInput) throws IOException;
71
72  /**
73   * Writes the internal state of the underlying storage implementation to the provided {@code
74   * objectOutput}.
75   *
76   * @param objectOutput  the object output stream to which the phone prefix map is written
77   * @throws IOException  if an error occurred writing to the provided output stream
78   */
79  public abstract void writeExternal(ObjectOutput objectOutput) throws IOException;
80
81  /**
82   * @return  the number of entries contained in the phone prefix map
83   */
84  public int getNumOfEntries() {
85    return numOfEntries;
86  }
87
88  /**
89   * @return  the set containing the possible lengths of prefixes
90   */
91  public TreeSet<Integer> getPossibleLengths() {
92    return possibleLengths;
93  }
94
95  @Override
96  public String toString() {
97    StringBuilder output = new StringBuilder();
98    int numOfEntries = getNumOfEntries();
99
100    for (int i = 0; i < numOfEntries; i++) {
101      output.append(getPrefix(i))
102          .append("|")
103          .append(getDescription(i))
104          .append("\n");
105    }
106    return output.toString();
107  }
108}
109