Class CurrencyRegistry

java.lang.Object
net.thauvin.erik.frankfurter.CurrencyRegistry

public final class CurrencyRegistry extends Object
The CurrencyRegistry class serves as a central repository for managing a collection of currencies.

Thread safety: All public methods are thread-safe.

  • Method Details

    • availableCurrencies

      public static Map<String,String> availableCurrencies() throws IOException, JsonSyntaxException, InterruptedException
      Fetches 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

      public static CurrencyRegistry getInstance()
      Gets the default singleton instance of the currency registry.
      Returns:
      The default currency registry
    • add

      public void add(Currency currency) throws IllegalArgumentException
      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 is null or has a null symbol
    • add

      public void add(String symbol, String name) throws IllegalArgumentException
      Adds a new currency with symbol and name, using Locale.ROOT as the default locale.

      Does not replace existing currencies and returns false if 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 is null or empty
    • clearPatternCache

      public void clearPatternCache()
      Clears the pattern cache to free memory.
    • contains

      public boolean contains(String pattern)
      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:
      true if at least one currency matches, false otherwise or if the pattern is invalid
    • findByName

      public Optional<Currency> findByName(String pattern)
      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

      public Optional<Currency> findBySymbol(String pattern)
      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

      public List<Currency> getAllCurrencies()
      Returns all currencies in the registry as an unmodifiable list.
      Returns:
      Unmodifiable list of all currencies
    • getAllSymbols

      public List<String> 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

      public void refresh() throws IOException, JsonSyntaxException, InterruptedException
      Fetches the collection of available currencies and their corresponding full names.
      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
    • reset

      public void reset()
      Resets the registry to its original default state.
    • search

      public List<Currency> search(String pattern)
      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