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 import java.util.TreeSet;
     24 
     25 /**
     26  * Abstracts the way area code data is stored into memory and serialized to a stream. It is used by
     27  * {@link AreaCodeMap} to support the most space-efficient storage strategy according to the
     28  * provided data.
     29  *
     30  * @author Philippe Liard
     31  */
     32 // @VisibleForTesting
     33 abstract class AreaCodeMapStorageStrategy {
     34   protected int numOfEntries = 0;
     35   protected final TreeSet<Integer> possibleLengths = new TreeSet<Integer>();
     36 
     37   public AreaCodeMapStorageStrategy() {}
     38 
     39   /**
     40    * Returns whether the underlying implementation of this abstract class is flyweight.
     41    * It is expected to be flyweight if it implements the {@code FlyweightMapStorage} class.
     42    *
     43    * @return  whether the underlying implementation of this abstract class is flyweight
     44    */
     45   public abstract boolean isFlyweight();
     46 
     47   /**
     48    * @return  the number of entries contained in the area code map
     49    */
     50   public int getNumOfEntries() {
     51     return numOfEntries;
     52   }
     53 
     54   /**
     55    * @return  the set containing the possible lengths of prefixes
     56    */
     57   public TreeSet<Integer> getPossibleLengths() {
     58     return possibleLengths;
     59   }
     60 
     61   /**
     62    * Gets the phone number prefix located at the provided {@code index}.
     63    *
     64    * @param index  the index of the prefix that needs to be returned
     65    * @return  the phone number prefix at the provided index
     66    */
     67   public abstract int getPrefix(int index);
     68 
     69   /**
     70    * Gets the description corresponding to the phone number prefix located at the provided {@code
     71    * index}.
     72    *
     73    * @param index  the index of the phone number prefix that needs to be returned
     74    * @return  the description corresponding to the phone number prefix at the provided index
     75    */
     76   public abstract String getDescription(int index);
     77 
     78   /**
     79    * Sets the internal state of the underlying storage implementation from the provided {@code
     80    * sortedAreaCodeMap} that maps phone number prefixes to description strings.
     81    *
     82    * @param sortedAreaCodeMap  a sorted map that maps phone number prefixes including country
     83    *    calling code to description strings
     84    */
     85   public abstract void readFromSortedMap(SortedMap<Integer, String> sortedAreaCodeMap);
     86 
     87   /**
     88    * Sets the internal state of the underlying storage implementation reading the provided {@code
     89    * objectInput}.
     90    *
     91    * @param objectInput  the object input stream from which the area code map is read
     92    * @throws IOException  if an error occurred reading the provided input stream
     93    */
     94   public abstract void readExternal(ObjectInput objectInput) throws IOException;
     95 
     96   /**
     97    * Writes the internal state of the underlying storage implementation to the provided {@code
     98    * objectOutput}.
     99    *
    100    * @param objectOutput  the object output stream to which the area code map is written
    101    * @throws IOException  if an error occurred writing to the provided output stream
    102    */
    103   public abstract void writeExternal(ObjectOutput objectOutput) throws IOException;
    104 
    105   @Override
    106   public String toString() {
    107     StringBuilder output = new StringBuilder();
    108     int numOfEntries = getNumOfEntries();
    109 
    110     for (int i = 0; i < numOfEntries; i++) {
    111       output.append(getPrefix(i));
    112       output.append("|");
    113       output.append(getDescription(i));
    114       output.append("\n");
    115     }
    116     return output.toString();
    117   }
    118 }
    119