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 17f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jiapackage com.android.i18n.phonenumbers.prefixmapper; 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/** 26f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * Abstracts the way phone prefix data is stored into memory and serialized to a stream. It is used 27f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * by {@link PhonePrefixMap} to support the most space-efficient storage strategy according to the 28f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * provided data. 299651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * 309651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * @author Philippe Liard 319651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia */ 32f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jiaabstract class PhonePrefixMapStorageStrategy { 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 56f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * sortedPhonePrefixMap} that maps phone number prefixes to description strings. 579651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * 58f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * @param sortedPhonePrefixMap a sorted map that maps phone number prefixes including country 599651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * calling code to description strings 609651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia */ 61f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia public abstract void readFromSortedMap(SortedMap<Integer, String> sortedPhonePrefixMap); 629651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia 639651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia /** 649651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * Sets the internal state of the underlying storage implementation reading the provided {@code 659651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * objectInput}. 669651f4d6f2740017c0c4ea9c1c340af6f644d609Shaopeng Jia * 67f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * @param objectInput the object input stream from which the phone prefix 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 * 76f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * @param objectOutput the object output stream to which the phone prefix 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 /** 82f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia * @return the number of entries contained in the phone prefix 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