Home | History | Annotate | Download | only in geocoding
      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 
     17 package com.android.i18n.phonenumbers.geocoding;
     18 
     19 import java.io.IOException;
     20 import java.io.ObjectInput;
     21 import java.io.ObjectOutput;
     22 import java.util.SortedMap;
     23 
     24 /**
     25  * Default area code map storage strategy that is used for data not containing description
     26  * duplications. It is mainly intended to avoid the overhead of the string table management when it
     27  * is actually unnecessary (i.e no string duplication).
     28  *
     29  * @author Shaopeng Jia
     30  */
     31 class DefaultMapStorage extends AreaCodeMapStorageStrategy {
     32 
     33   public DefaultMapStorage() {}
     34 
     35   private int[] phoneNumberPrefixes;
     36   private String[] descriptions;
     37 
     38   @Override
     39   public int getPrefix(int index) {
     40     return phoneNumberPrefixes[index];
     41   }
     42 
     43   @Override
     44   public String getDescription(int index) {
     45     return descriptions[index];
     46   }
     47 
     48   @Override
     49   public void readFromSortedMap(SortedMap<Integer, String> sortedAreaCodeMap) {
     50     numOfEntries = sortedAreaCodeMap.size();
     51     phoneNumberPrefixes = new int[numOfEntries];
     52     descriptions = new String[numOfEntries];
     53     int index = 0;
     54     for (int prefix : sortedAreaCodeMap.keySet()) {
     55       phoneNumberPrefixes[index++] = prefix;
     56       possibleLengths.add((int) Math.log10(prefix) + 1);
     57     }
     58     sortedAreaCodeMap.values().toArray(descriptions);
     59   }
     60 
     61   @Override
     62   public void readExternal(ObjectInput objectInput) throws IOException {
     63     numOfEntries = objectInput.readInt();
     64     if (phoneNumberPrefixes == null || phoneNumberPrefixes.length < numOfEntries) {
     65       phoneNumberPrefixes = new int[numOfEntries];
     66     }
     67     if (descriptions == null || descriptions.length < numOfEntries) {
     68       descriptions = new String[numOfEntries];
     69     }
     70     for (int i = 0; i < numOfEntries; i++) {
     71       phoneNumberPrefixes[i] = objectInput.readInt();
     72       descriptions[i] = objectInput.readUTF();
     73     }
     74     int sizeOfLengths = objectInput.readInt();
     75     possibleLengths.clear();
     76     for (int i = 0; i < sizeOfLengths; i++) {
     77       possibleLengths.add(objectInput.readInt());
     78     }
     79   }
     80 
     81   @Override
     82   public void writeExternal(ObjectOutput objectOutput) throws IOException {
     83     objectOutput.writeInt(numOfEntries);
     84     for (int i = 0; i < numOfEntries; i++) {
     85       objectOutput.writeInt(phoneNumberPrefixes[i]);
     86       objectOutput.writeUTF(descriptions[i]);
     87     }
     88     int sizeOfLengths = possibleLengths.size();
     89     objectOutput.writeInt(sizeOfLengths);
     90     for (Integer length : possibleLengths) {
     91       objectOutput.writeInt(length);
     92     }
     93   }
     94 }
     95