Home | History | Annotate | Download | only in geocoding
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      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 boolean isFlyweight() {
     40     return false;
     41   }
     42 
     43   @Override
     44   public int getPrefix(int index) {
     45     return phoneNumberPrefixes[index];
     46   }
     47 
     48   @Override
     49   public String getDescription(int index) {
     50     return descriptions[index];
     51   }
     52 
     53   @Override
     54   public void readFromSortedMap(SortedMap<Integer, String> sortedAreaCodeMap) {
     55     numOfEntries = sortedAreaCodeMap.size();
     56     phoneNumberPrefixes = new int[numOfEntries];
     57     descriptions = new String[numOfEntries];
     58     int index = 0;
     59     for (int prefix : sortedAreaCodeMap.keySet()) {
     60       phoneNumberPrefixes[index++] = prefix;
     61       possibleLengths.add((int) Math.log10(prefix) + 1);
     62     }
     63     sortedAreaCodeMap.values().toArray(descriptions);
     64   }
     65 
     66   @Override
     67   public void readExternal(ObjectInput objectInput) throws IOException {
     68     numOfEntries = objectInput.readInt();
     69     if (phoneNumberPrefixes == null || phoneNumberPrefixes.length < numOfEntries) {
     70       phoneNumberPrefixes = new int[numOfEntries];
     71     }
     72     if (descriptions == null || descriptions.length < numOfEntries) {
     73       descriptions = new String[numOfEntries];
     74     }
     75     for (int i = 0; i < numOfEntries; i++) {
     76       phoneNumberPrefixes[i] = objectInput.readInt();
     77       descriptions[i] = objectInput.readUTF();
     78     }
     79     int sizeOfLengths = objectInput.readInt();
     80     possibleLengths.clear();
     81     for (int i = 0; i < sizeOfLengths; i++) {
     82       possibleLengths.add(objectInput.readInt());
     83     }
     84   }
     85 
     86   @Override
     87   public void writeExternal(ObjectOutput objectOutput) throws IOException {
     88     objectOutput.writeInt(numOfEntries);
     89     for (int i = 0; i < numOfEntries; i++) {
     90       objectOutput.writeInt(phoneNumberPrefixes[i]);
     91       objectOutput.writeUTF(descriptions[i]);
     92     }
     93     int sizeOfLengths = possibleLengths.size();
     94     objectOutput.writeInt(sizeOfLengths);
     95     for (Integer length : possibleLengths) {
     96       objectOutput.writeInt(length);
     97     }
     98   }
     99 }
    100