Class SmsCostPolicy
java.lang.Object
com.irurueta.authserver.authorization.domain.sms.SmsCostPolicy
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 Summary
FieldsModifier and TypeFieldDescriptionCountries that may receive messages, as upper-case ISO alpha-2 codes.Cost of a message per country, in millionths of the budget currency.private final longBudget of a day.static final DurationLength of the window of the daily budget.private final longCost of a message to an allowed country with no cost of its own.private final DurationLength of the short window.private final longBudget of the short window. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongcostMicros(String country) Gets what a message to a country costs.dailyCap()Gets the cap of a day, as the ledger checks it.booleanisCountryAllowed(String country) Tells whether messages may be sent to a country.booleanisWithinDailyCap(long spentMicros) Tells whether the spend of the last day is within its budget.booleanisWithinWindowCap(long spentMicros) Tells whether the spend of the short window is within its budget.private static voidrequireCountry(String country) Rejects anything but an upper-case ISO 3166-1 alpha-2 code.window()Gets the length of the short window the window budget applies to.Gets the cap of the short window, as the ledger checks it.
-
Field Details
-
DAY
Length of the window of the daily budget. -
allowedCountries
Countries that may receive messages, as upper-case ISO alpha-2 codes. -
costMicrosByCountry
Cost of a message per country, in millionths of the budget currency. -
defaultCostMicros
private final long defaultCostMicrosCost of a message to an allowed country with no cost of its own. -
window
Length of the short window. -
windowCapMicros
private final long windowCapMicrosBudget of the short window. -
dailyCapMicros
private final long dailyCapMicrosBudget 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
Tells whether messages may be sent to a country.- Parameters:
country- ISO alpha-2 code of the country, ornull.- Returns:
truewhen the country is allowed.
-
costMicros
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
Gets the length of the short window the window budget applies to.- Returns:
- the window.
-
windowCap
Gets the cap of the short window, as the ledger checks it.- Returns:
- the window and its budget.
-
dailyCap
Gets the cap of a day, as the ledger checks it.- Returns:
DAYand 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:
truewhen 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:
truewhen the spend does not exceed the budget.
-
requireCountry
Rejects anything but an upper-case ISO 3166-1 alpha-2 code.- Parameters:
country- value to check.
-