Package com.tuladb.currency
Class CurrencyScales
java.lang.Object
com.tuladb.currency.CurrencyScales
Per-currency scales from the engine's currency registry.
The first use of a numeric asks the engine (MSG_CURRENCY_GET);
a found scale is cached forever, because the engine refuses to change a
registered currency's scale. A miss is cached for
NOT_REGISTERED_TTL_NANOS (2 s): a currency registered elsewhere is
picked up within that window, and an unregistered one does not cost a round
trip per call; until then it is a CurrencyNotRegisteredException,
never a guessed scale. A currency created through
createCurrency(com.tuladb.Models.CurrencyInstance) is cached at once. Safe to share between threads.
CurrencyScales scales = new CurrencyScales(client::getCurrency, client::createCurrency);
long minor = scales.toMinor(124, "800.00"); // 80000
String text = scales.format(124, minor); // "800.00"
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe create half of the registry:LedgerClient::createCurrency, ...static interfaceWhere registry entries come from:LedgerClient::getCurrency,SmartLedgerClient::getCurrency, a pooled lookup, ... -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longHow long a miss is remembered (the shared rule: 2 s). -
Constructor Summary
ConstructorsConstructorDescriptionCurrencyScales(CurrencyScales.CurrencyLookup lookup, CurrencyScales.CurrencyCreator creator) Withcreator,createCurrency(com.tuladb.Models.CurrencyInstance)registers and caches at once.CurrencyScales(CurrencyScales.CurrencyLookup lookup, CurrencyScales.CurrencyCreator creator, long missTtlNanos, LongSupplier clockNanos) Explicit miss TTL and clock (nanoseconds), for tests. -
Method Summary
Modifier and TypeMethodDescriptioncreateCurrency(Models.CurrencyInstance request) Register through the creator given at construction and cache the registered scale at once.voidforget(int numeric) Drop one cached scale or miss.format(int numeric, long minor) intremember(int numeric, int scale) Record a scale learned elsewhere (e.g. a create reply); clears a remembered miss.intscale(int numeric) The registered scale ofnumeric, asking the engine on first use.longlongtoMinor(int numeric, BigDecimal value)
-
Field Details
-
NOT_REGISTERED_TTL_NANOS
public static final long NOT_REGISTERED_TTL_NANOSHow long a miss is remembered (the shared rule: 2 s).- See Also:
-
-
Constructor Details
-
CurrencyScales
-
CurrencyScales
Withcreator,createCurrency(com.tuladb.Models.CurrencyInstance)registers and caches at once. -
CurrencyScales
public CurrencyScales(CurrencyScales.CurrencyLookup lookup, CurrencyScales.CurrencyCreator creator, long missTtlNanos, LongSupplier clockNanos) Explicit miss TTL and clock (nanoseconds), for tests.
-
-
Method Details
-
scale
The registered scale ofnumeric, asking the engine on first use.- Throws:
CurrencyNotRegisteredException- the engine has no such currencyIOException- the registry could not be asked
-
remember
public int remember(int numeric, int scale) Record a scale learned elsewhere (e.g. a create reply); clears a remembered miss. -
createCurrency
Register through the creator given at construction and cache the registered scale at once.- Throws:
IOException
-
toMinor
- Throws:
IOException
-
toMinor
- Throws:
IOException
-
format
- Throws:
IOException
-
forget
public void forget(int numeric) Drop one cached scale or miss. Only needed when the cache outlives the engine it was filled from.
-