java.lang.Object
com.irurueta.authserver.authorization.domain.sms.SmsCostPolicy

public final class SmsCostPolicy extends Object
The rules that keep text messages from costing too much: the countries messages may go to, what a message costs in each and the budget per window and per day. It is pure: the spend is kept by a SmsCostLedgerRepository.
  • Field Details

    • DAY

      public static final Duration DAY
      Length of the window of the daily budget.
    • allowedCountries

      private final Set<String> allowedCountries
      Countries that may receive messages, as upper-case ISO alpha-2 codes.
    • costMicrosByCountry

      private final Map<String,Long> costMicrosByCountry
      Cost of a message per country, in millionths of the budget currency.
    • defaultCostMicros

      private final long defaultCostMicros
      Cost of a message to an allowed country with no cost of its own.
    • window

      private final Duration window
      Length of the short window.
    • windowCapMicros

      private final long windowCapMicros
      Budget of the short window.
    • dailyCapMicros

      private final long dailyCapMicros
      Budget of a day.
  • Constructor Details

    • SmsCostPolicy

      public SmsCostPolicy(Set<String> allowedCountries, Map<String,Long> costMicrosByCountry, long defaultCostMicros, Duration window, long windowCapMicros, long dailyCapMicros)
      Creates the policy.
      Parameters:
      allowedCountries - countries that may receive messages; empty allows none.
      costMicrosByCountry - cost of a message per country; not negative values.
      defaultCostMicros - cost of a message to an allowed country that has none of its own; not negative.
      window - length of the short window; positive.
      windowCapMicros - budget of the short window; not negative.
      dailyCapMicros - budget of a day; not negative.
      Throws:
      IllegalArgumentException - if a value is missing, a country is not an ISO alpha-2 code, or a cost, cap or the window is out of range.
  • Method Details

    • isCountryAllowed

      public boolean isCountryAllowed(String country)
      Tells whether messages may be sent to a country.
      Parameters:
      country - ISO alpha-2 code of the country, or null.
      Returns:
      true when the country is allowed.
    • costMicros

      public long costMicros(String country)
      Gets what a message to a country costs.
      Parameters:
      country - ISO alpha-2 code of the country.
      Returns:
      the cost in millionths of the budget currency; the default when the country has none of its own.
    • window

      public Duration window()
      Gets the length of the short window the window budget applies to.
      Returns:
      the window.
    • windowCap

      public SmsSpendCap windowCap()
      Gets the cap of the short window, as the ledger checks it.
      Returns:
      the window and its budget.
    • dailyCap

      public SmsSpendCap dailyCap()
      Gets the cap of a day, as the ledger checks it.
      Returns:
      DAY and its budget.
    • isWithinWindowCap

      public boolean isWithinWindowCap(long spentMicros)
      Tells whether the spend of the short window is within its budget.
      Parameters:
      spentMicros - spend in the window, the message being sent included.
      Returns:
      true when the spend does not exceed the budget.
    • isWithinDailyCap

      public boolean isWithinDailyCap(long spentMicros)
      Tells whether the spend of the last day is within its budget.
      Parameters:
      spentMicros - spend in the last day, the message being sent included.
      Returns:
      true when the spend does not exceed the budget.
    • requireCountry

      private static void requireCountry(String country)
      Rejects anything but an upper-case ISO 3166-1 alpha-2 code.
      Parameters:
      country - value to check.