getExpectedCost method

ShortNumberCost getExpectedCost(
  1. PhoneNumber number
)

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 getExpectedCostForRegion(PhoneNumber, String). However, if the country calling code is shared by multiple regions, then it returns the highest cost in the sequence ShortNumberCost.premiumRate, ShortNumberCost.unknownCost, ShortNumberCost.standardRate, ShortNumberCost.tollFree. The reason for the position of unknownCost in this order is that if a number is unknownCost in one region but standardRate or tollFree in another, its expected cost cannot be estimated as one of the latter since it might be a premiumRate number.

For example, if a number is standardRate in the US, but tollFree in Canada, the expected cost returned by this method will be standardRate, 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 [getExpectedCostForRegion(PhoneNumber, String)] instead.

number the short number for which we want to know the expected cost category returns the highest expected cost category of the short number in the region(s) with the given country calling code.

Implementation

ShortNumberCost getExpectedCost(PhoneNumber number) {
  List<String> regionCodes =
      _getRegionCodesForCountryCode(number.countryCode);
  if (regionCodes.isEmpty) {
    return ShortNumberCost.unknownCost;
  }
  if (regionCodes.length == 1) {
    return getExpectedCostForRegion(number, regionCodes[0]);
  }
  ShortNumberCost cost = ShortNumberCost.tollFree;
  for (String regionCode in regionCodes) {
    ShortNumberCost costForRegion =
        getExpectedCostForRegion(number, regionCode);
    switch (costForRegion) {
      case ShortNumberCost.premiumRate:
        return ShortNumberCost.premiumRate;
      case ShortNumberCost.unknownCost:
        cost = ShortNumberCost.unknownCost;
        break;
      case ShortNumberCost.standardRate:
        if (cost != ShortNumberCost.unknownCost) {
          cost = ShortNumberCost.standardRate;
        }
        break;
      case ShortNumberCost.tollFree:
        // Do nothing.
        break;
    }
  }
  return cost;
}