Beispiel #1
0
        /**
         * Returns an area-level text description in the given language for the given phone number.
         *
         * @param number  the phone number for which we want to get a text description
         * @param lang  two-letter lowercase ISO language codes as defined by ISO 639-1
         * @param script  four-letter titlecase (the first letter is uppercase and the rest of the letters
         *     are lowercase) ISO script codes as defined in ISO 15924
         * @param region  two-letter uppercase ISO country codes as defined by ISO 3166-1
         * @return  an area-level text description in the given language for the given phone number, or an
         *     empty string if such a description is not available
         */
        private String getAreaDescriptionForNumber(
            PhoneNumber number, String lang, String script, String region)
        {
            int countryCallingCode = number.getCountryCode();
            // As the NANPA data is split into multiple files covering 3-digit areas, use a phone number
            // prefix of 4 digits for NANPA instead, e.g. 1650.
            int phonePrefix = (countryCallingCode != 1) ?
                              countryCallingCode : (1000 + (int)(number.getNationalNumber() / 10000000));
            AreaCodeMap phonePrefixDescriptions =
                getPhonePrefixDescriptions(phonePrefix, lang, script, region);
            String description = (phonePrefixDescriptions != null)
        ? phonePrefixDescriptions.lookup(number)
        : null;

            // When a location is not available in the requested language, fall back to English.
            if ((description == null || description.length() == 0) && mayFallBackToEnglish(lang))
            {
                AreaCodeMap defaultMap = getPhonePrefixDescriptions(phonePrefix, "en", "", "");
                if (defaultMap == null)
                {
                    return("");
                }
                description = defaultMap.lookup(number);
            }
            return(description != null ? (string)description : "");
        }
Beispiel #2
0
        private void loadAreaCodeMapFromFile(String fileName)
        {
            InputStream source =
                Extensions.getResourceAsStream(phonePrefixDataDirectory + fileName);
            ObjectInputStream @in = null;

            try {
                @in = new ObjectInputStream(source);
                AreaCodeMap map = new AreaCodeMap();
                map.readExternal(@in);
                availablePhonePrefixMaps.put(fileName, map);
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, e.toString());
            } finally {
                close(@in);
            }
        }
 public void testGetSmallerMapStorageChoosesFlyweightImpl()
 {
     AreaCodeMapStorageStrategy mapStorage =
     new AreaCodeMap().getSmallerMapStorage(createFlyweightStorageMapCandidate());
     assertTrue(mapStorage is FlyweightMapStorage);
 }
        /**
           * Creates a new area code map serializing the provided area code map to a stream and then reading
           * this stream. The resulting area code map is expected to be strictly equal to the provided one
           * from which it was generated.
           */
        private static AreaCodeMap createNewAreaCodeMap(AreaCodeMap areaCodeMap)
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            areaCodeMap.writeExternal(objectOutputStream);
            objectOutputStream.flush();

            AreaCodeMap newAreaCodeMap = new AreaCodeMap();
            newAreaCodeMap.readExternal(
            new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
            return newAreaCodeMap;
        }
        public void testReadWriteExternalWithFlyweightStrategy()
        {
            AreaCodeMap localAreaCodeMap = new AreaCodeMap();
            localAreaCodeMap.readAreaCodeMap(createFlyweightStorageMapCandidate());
            assertTrue(localAreaCodeMap.getAreaCodeMapStorage() is FlyweightMapStorage);

            AreaCodeMap newAreaCodeMap;
            newAreaCodeMap = createNewAreaCodeMap(localAreaCodeMap);
            assertEquals(localAreaCodeMap.toString(), newAreaCodeMap.toString());
        }
 private void loadAreaCodeMapFromFile(String fileName)
 {
     InputStream source =
     Extensions.getResourceAsStream(phonePrefixDataDirectory + fileName);
     ObjectInputStream @in = null;
     try {
       @in = new ObjectInputStream(source);
       AreaCodeMap map = new AreaCodeMap();
       map.readExternal(@in);
       availablePhonePrefixMaps.put(fileName, map);
     } catch (IOException e) {
       LOGGER.log(Level.WARNING, e.toString());
     } finally {
       close(@in);
     }
 }