/// <summary> /// Gets a valid short number for the specified cost category. /// </summary> /// /// <param name="regionCode">the region for which an example short number is needed</param> /// <param name="cost">the cost category of number that is needed</param> /// <returns> a valid short number for the specified region and cost category. Returns an empty /// string when the metadata does not contain such information, or the cost is UNKNOWN_COST.</returns> internal string GetExampleShortNumberForCost(string regionCode, ShortNumberCost cost) { var phoneMetadata = MetadataManager.GetShortNumberMetadataForRegion(regionCode); if (phoneMetadata == null) { return(""); } PhoneNumberDesc desc = null; switch (cost) { case ShortNumberCost.TOLL_FREE: desc = phoneMetadata.TollFree; break; case ShortNumberCost.STANDARD_RATE: desc = phoneMetadata.StandardRate; break; case ShortNumberCost.PREMIUM_RATE: desc = phoneMetadata.PremiumRate; break; // UNKNOWN_COST numbers are computed by the process of elimination from the other cost // categories. } return(desc?.ExampleNumber ?? string.Empty); }
/** * Gets a valid short number for the specified cost category. * * @param regionCode the region for which an example short number is needed * @param cost the cost category of number that is needed * @return a valid short number for the specified region and cost category. Returns an empty * string when the metadata does not contain such information, or the cost is UNKNOWN_COST. */ // @VisibleForTesting internal String getExampleShortNumberForCost(String regionCode, ShortNumberCost cost) { PhoneMetadata phoneMetadata = MetadataManager.getShortNumberMetadataForRegion(regionCode); if (phoneMetadata == null) { return(""); } PhoneNumberDesc desc = null; switch (cost) { case ShortNumberCost.TOLL_FREE: desc = phoneMetadata.getTollFree(); break; case ShortNumberCost.STANDARD_RATE: desc = phoneMetadata.getStandardRate(); break; case ShortNumberCost.PREMIUM_RATE: desc = phoneMetadata.getPremiumRate(); break; default: // UNKNOWN_COST numbers are computed by the process of elimination from the other cost // categories. break; } if (desc != null && desc.HasExampleNumber()) { return(desc.getExampleNumber()); } return(""); }
/** * Gets the expected cost category of a short number (however, nothing is implied about its * validity). If the country calling code is unique to a region, this method behaves exactly the * same as {@link #getExpectedCostForRegion(String, String)}. However, if the country calling * code is shared by multiple regions, then it returns the highest cost in the sequence * PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of * UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE * or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it * might be a PREMIUM_RATE number. * * For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected cost * returned by this method will be STANDARD_RATE, since the NANPA countries share the same country * calling code. * * Note: If the region from which the number is dialed is known, it is highly preferable to call * {@link #getExpectedCostForRegion(String, String)} instead. * * @param number the short number for which we want to know the expected cost category * @return the highest expected cost category of the short number in the region(s) with the given * country calling code */ public ShortNumberCost getExpectedCost(PhoneNumber number) { List <String> regionCodes = phoneUtil.getRegionCodesForCountryCode(number.getCountryCode()); if (regionCodes.Count == 0) { return(ShortNumberCost.UNKNOWN_COST); } String shortNumber = phoneUtil.getNationalSignificantNumber(number); if (regionCodes.Count == 1) { return(getExpectedCostForRegion(shortNumber, regionCodes[0])); } ShortNumberCost cost = ShortNumberCost.TOLL_FREE; foreach (String regionCode in regionCodes) { ShortNumberCost costForRegion = getExpectedCostForRegion(shortNumber, regionCode); switch (costForRegion) { case ShortNumberCost.PREMIUM_RATE: return(ShortNumberCost.PREMIUM_RATE); case ShortNumberCost.UNKNOWN_COST: cost = ShortNumberCost.UNKNOWN_COST; break; case ShortNumberCost.STANDARD_RATE: if (cost != ShortNumberCost.UNKNOWN_COST) { cost = ShortNumberCost.STANDARD_RATE; } break; case ShortNumberCost.TOLL_FREE: // Do nothing. break; default: logger.log(Level.SEVERE, "Unrecognised cost for region: " + costForRegion); break; } } return(cost); }
/** * Gets a valid short number for the specified cost category. * * @param regionCode the region for which an example short number is needed * @param cost the cost category of number that is needed * @return a valid short number for the specified region and cost category. Returns an empty * string when the metadata does not contain such information, or the cost is UNKNOWN_COST. */ // @VisibleForTesting internal String getExampleShortNumberForCost(String regionCode, ShortNumberCost cost) { PhoneMetadata phoneMetadata = MetadataManager.getShortNumberMetadataForRegion(regionCode); if (phoneMetadata == null) { return ""; } PhoneNumberDesc desc = null; switch (cost) { case ShortNumberCost.TOLL_FREE: desc = phoneMetadata.getTollFree(); break; case ShortNumberCost.STANDARD_RATE: desc = phoneMetadata.getStandardRate(); break; case ShortNumberCost.PREMIUM_RATE: desc = phoneMetadata.getPremiumRate(); break; default: // UNKNOWN_COST numbers are computed by the process of elimination from the other cost // categories. break; } if (desc != null && desc.hasExampleNumber()) { return desc.getExampleNumber(); } return ""; }