19651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia/*
2ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia * Copyright (C) 2011 The Libphonenumber Authors
39651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia *
49651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * Licensed under the Apache License, Version 2.0 (the "License");
59651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * you may not use this file except in compliance with the License.
69651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * You may obtain a copy of the License at
79651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia *
89651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * http://www.apache.org/licenses/LICENSE-2.0
99651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia *
109651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * Unless required by applicable law or agreed to in writing, software
119651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * distributed under the License is distributed on an "AS IS" BASIS,
129651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * See the License for the specific language governing permissions and
149651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * limitations under the License.
159651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia */
169651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
1793f6965c2c041ac707bf1b3bcf5a3f60e452f421Shaopeng Jiapackage com.android.i18n.phonenumbers.geocoding;
189651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
199651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jiaimport java.io.IOException;
209651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jiaimport java.io.ObjectInput;
219651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jiaimport java.io.ObjectOutput;
229651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jiaimport java.util.SortedMap;
239651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jiaimport java.util.TreeSet;
249651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
259651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia/**
26f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * Abstracts the way area code data is stored into memory and serialized to a stream. It is used by
27f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * {@link AreaCodeMap} to support the most space-efficient storage strategy according to the
28f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * provided data.
299651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia *
309651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * @author Philippe Liard
319651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia */
329651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jiaabstract class AreaCodeMapStorageStrategy {
339651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  protected int numOfEntries = 0;
349651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  protected final TreeSet<Integer> possibleLengths = new TreeSet<Integer>();
359651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
369651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  /**
379651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * Gets the phone number prefix located at the provided {@code index}.
389651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   *
399651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @param index  the index of the prefix that needs to be returned
409651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @return  the phone number prefix at the provided index
419651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   */
429651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  public abstract int getPrefix(int index);
439651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
449651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  /**
459651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * Gets the description corresponding to the phone number prefix located at the provided {@code
46ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia   * index}. If the description is not available in the current language an empty string is
47ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia   * returned.
489651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   *
499651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @param index  the index of the phone number prefix that needs to be returned
509651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @return  the description corresponding to the phone number prefix at the provided index
519651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   */
529651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  public abstract String getDescription(int index);
539651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
549651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  /**
559651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * Sets the internal state of the underlying storage implementation from the provided {@code
569651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * sortedAreaCodeMap} that maps phone number prefixes to description strings.
579651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   *
589651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @param sortedAreaCodeMap  a sorted map that maps phone number prefixes including country
599651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   *    calling code to description strings
609651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   */
619651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  public abstract void readFromSortedMap(SortedMap<Integer, String> sortedAreaCodeMap);
629651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
639651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  /**
649651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * Sets the internal state of the underlying storage implementation reading the provided {@code
659651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * objectInput}.
669651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   *
679651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @param objectInput  the object input stream from which the area code map is read
689651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @throws IOException  if an error occurred reading the provided input stream
699651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   */
709651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  public abstract void readExternal(ObjectInput objectInput) throws IOException;
719651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
729651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  /**
739651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * Writes the internal state of the underlying storage implementation to the provided {@code
749651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * objectOutput}.
759651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   *
769651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @param objectOutput  the object output stream to which the area code map is written
779651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   * @throws IOException  if an error occurred writing to the provided output stream
789651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia   */
799651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  public abstract void writeExternal(ObjectOutput objectOutput) throws IOException;
809651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
81ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  /**
82ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia   * @return  the number of entries contained in the area code map
83ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia   */
84ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  public int getNumOfEntries() {
85ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    return numOfEntries;
86ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  }
87ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia
88ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  /**
89ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia   * @return  the set containing the possible lengths of prefixes
90ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia   */
91ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  public TreeSet<Integer> getPossibleLengths() {
92ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    return possibleLengths;
93ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  }
94ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia
95f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia  @Override
96f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia  public String toString() {
97f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    StringBuilder output = new StringBuilder();
98f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    int numOfEntries = getNumOfEntries();
999651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia
100f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    for (int i = 0; i < numOfEntries; i++) {
101ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia      output.append(getPrefix(i))
102ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia          .append("|")
103ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia          .append(getDescription(i))
104ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia          .append("\n");
1059651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia    }
106f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    return output.toString();
1079651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia  }
1089651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia}
109