Package net.thauvin.erik.frankfurter
Class CurrencyRegistry
java.lang.Object
net.thauvin.erik.frankfurter.CurrencyRegistry
The CurrencyRegistry class serves as a central repository for managing a collection of currencies.
Thread safety: All public methods are thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new currency with symbol and name, usingLocale.ROOTas the default locale.voidAdds a new currency to the registry.Fetches the collection of available currencies and their corresponding full names from the Frankfurter API.voidClears the pattern cache to free memory.booleanChecks if any currency matches the given symbol pattern using regular expression (case-insensitive).findByName(String pattern) Finds a currency by its name using a regular expression pattern (case-insensitive).findBySymbol(String pattern) Finds a currency by its symbol using a regular expression pattern (case-insensitive).Returns all currencies in the registry as an unmodifiable list.Retrieves all currency symbols available in the registry.static CurrencyRegistryGets the default singleton instance of the currency registry.intReturns the current size of the pattern cache.voidrefresh()Fetches the collection of available currencies and their corresponding full names.voidreset()Resets the registry to its original default state.Searches for currencies by code or name using a regular expression pattern (case-insensitive).intsize()Returns the number of currencies in the registry.
-
Method Details
-
availableCurrencies
public static Map<String,String> availableCurrencies() throws IOException, JsonSyntaxException, InterruptedExceptionFetches the collection of available currencies and their corresponding full names from the Frankfurter API.- Returns:
- A map where the keys are currency symbols (e.g.,
USD) and the values are the corresponding currency names (e.g.,United States Dollar). - Throws:
IOException- if an input or output exception occurs during the API request.JsonSyntaxException- if the JSON response from the API does not match the expected format.InterruptedException- if the operation is interrupted.
-
getInstance
Gets the default singleton instance of the currency registry.- Returns:
- The default currency registry
-
add
Adds a new currency to the registry.If a currency with the same symbol exists, it will be replaced with the new entry.
- Parameters:
currency- The currency to add- Throws:
IllegalArgumentException- if the currency isnullor has anullsymbol
-
add
Adds a new currency with symbol and name, usingLocale.ROOTas the default locale.Does not replace existing currencies and returns
falseif the symbol already exists.- Parameters:
symbol- The currency symbol (e.g., "USD")name- The currency name (e.g., "United States Dollar")- Throws:
IllegalArgumentException- if the symbol isnullor empty
-
clearPatternCache
public void clearPatternCache()Clears the pattern cache to free memory. -
contains
Checks if any currency matches the given symbol pattern using regular expression (case-insensitive).- Parameters:
pattern- The regular expression pattern to match against currency symbols- Returns:
trueif at least one currency matches,falseotherwise or if the pattern is invalid
-
findByName
Finds a currency by its name using a regular expression pattern (case-insensitive).- Parameters:
pattern- The regular expression pattern to match against currency names- Returns:
- An Optional containing the first currency that matches, or empty if none found or pattern invalid
-
findBySymbol
Finds a currency by its symbol using a regular expression pattern (case-insensitive).- Parameters:
pattern- The regular expression pattern to match against currency symbols- Returns:
- An Optional containing the first currency that matches, or empty if none found or pattern invalid
-
getAllCurrencies
Returns all currencies in the registry as an unmodifiable list.- Returns:
- Unmodifiable list of all currencies
-
getAllSymbols
Retrieves all currency symbols available in the registry.- Returns:
- An unmodifiable list of all currency symbols.
-
patternCacheSize
public int patternCacheSize()Returns the current size of the pattern cache. -
refresh
Fetches the collection of available currencies and their corresponding full names.- Throws:
IOException- if an input or output exception occurs during the API requestJsonSyntaxException- if the JSON response from the API does not match the expected formatInterruptedException
-
reset
public void reset()Resets the registry to its original default state. -
search
Searches for currencies by code or name using a regular expression pattern (case-insensitive).- Parameters:
pattern- The regular expression pattern to search with- Returns:
- List of matching currencies, or empty list if the pattern is invalid
-
size
public int size()Returns the number of currencies in the registry.- Returns:
- The count of currencies
-