Class FrankfurterUtils

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

public final class FrankfurterUtils extends Object
Utility class for interacting with the Frankfurter API and performing related operations.
  • Field Details

    • API_BASE_URL

      public static final String API_BASE_URL
      The base URL for the Frankfurter API.
      See Also:
    • EUR

      public static final String EUR
      The Euro currency code.
      See Also:
    • LOGGER

      public static final Logger LOGGER
      The logger for this class.
    • MIN_DATE

      public static final LocalDate MIN_DATE
      The minimum date supported by the Frankfurter API.
  • Method Details

    • calculateEasterMonday

      public static LocalDate calculateEasterMonday(int year)
      Calculates Easter Monday for a given year.

      Easter Monday is 1 day after Easter Sunday

      Parameters:
      year - The year for which to calculate Easter Monday
      Returns:
      LocalDate representing Easter Monday
      See Also:
    • calculateEasterSunday

      public static LocalDate calculateEasterSunday(int year)
      Calculates Easter Sunday for a given year using the Western (Gregorian) calendar.

      Based on the algorithm by Jean Meeus.

      Parameters:
      year - The year for which to calculate Easter
      Returns:
      LocalDate representing Easter Sunday
    • calculateGoodFriday

      public static LocalDate calculateGoodFriday(int year)
      Calculates Good Friday for a given year.

      Good Friday is 2 days before Easter Sunday

      Parameters:
      year - The year for which to calculate Good Friday
      Returns:
      LocalDate representing Good Friday
      See Also:
    • closingDays

      public static List<LocalDate> closingDays(int year)
      Calculates a list of target closing days for a given year.

      The target closing days are:

      • New Year's Day (Jan 1)
      • Good Friday (2 days before Easter)
      • Easter Monday (1 day after Easter)
      • Lobor Day (May 1)
      • Christmas Day (Dec 25)
      • Christmas Holiday (Dec 26)
      Parameters:
      year - the year for which to generate the list of closing days
      Returns:
      a list of LocalDate objects representing the closing days in the specified year
      See Also:
    • fetchUri

      public static String fetchUri(URI uri) throws IOException
      Sends an HTTP GET request to the specified URI and retrieves the response body as a string.

      The request is configured to accept JSON responses.

      Parameters:
      uri - the URI to which the GET request will be sent
      Returns:
      the response body as a string
      Throws:
      HttpErrorException - if the response status code is not 200
      IOException - if an I/O error occurs during the request
    • formatCurrency

      public static String formatCurrency(String symbol, Double amount)
      Formats a currency amount based on the provided ISO currency symbol and amount.
      Parameters:
      symbol - The 3-letter ISO currency symbol (e.g., "USD", "EUR")
      amount - The monetary amount to format
      Returns:
      A formatted currency string
      Throws:
      IllegalArgumentException - If the currency symbol is unknown or invalid
    • formatCurrency

      public static String formatCurrency(String symbol, Double amount, boolean rounded)
      Formats a currency amount based on the provided ISO currency symbol, amount, and rounding preference.
      Parameters:
      symbol - The 3-letter ISO currency symbol (e.g., "USD", "EUR")
      amount - The monetary amount to format
      rounded - Whether to round the amount
      Returns:
      A formatted currency string
      Throws:
      IllegalArgumentException - If the currency symbol is unknown or invalid
    • isValidSymbol

      public static boolean isValidSymbol(String symbol)
      Validates whether the provided symbol is a non-null string consisting of exactly three alphabetical characters (either uppercase or lowercase).
      Parameters:
      symbol - The string to be validated
      Returns:
      true if the symbol is non-null and matches the required format of three alphabetical characters, false otherwise.
    • isWeekend

      public static boolean isWeekend(LocalDate date)
      Determines whether the given date falls on a weekend (Saturday or Sunday).
      Parameters:
      date - the LocalDate to evaluate
      Returns:
      true if the specified date is a Saturday or Sunday, false otherwise
    • isWorkingDay

      public static boolean isWorkingDay(LocalDate date, List<LocalDate> closingDays)
      Determines whether the specified date is a working day.

      A working day is defined as a day that is not a weekend (Saturday or Sunday) and does not fall on a closing day.

      Parameters:
      date - the LocalDate to evaluate
      closingDays - a list of LocalDate objects representing non-working days
      Returns:
      true if the specified date is a working day, false otherwise
      See Also:
    • normalizeSymbol

      public static String normalizeSymbol(String symbol)
      Formats a given currency symbol to uppercase if it matches the required format of three alphabetical characters.
      Parameters:
      symbol - The currency symbol to be formatted, which must consist of exactly three alphabetical characters
      Returns:
      The normalized version of the currency symbol if it is valid
      Throws:
      IllegalArgumentException - If the provided symbol is not exactly three alphabetical characters
    • uriBuilder

      public static URI uriBuilder(String path, Map<String,String> query) throws URISyntaxException
      Builds a URI by combining the API_BASE_URL base url, a specified path, and query parameters.

      The path is appended to the base URL, and query parameters are appended as a query string.

      Parameters:
      path - the path to be appended to the base URL; can be null
      query - a map of query parameter keys and values; can be null or empty
      Returns:
      a URI constructed from the base URL, the path, and the query parameters
      Throws:
      URISyntaxException - if the resulting URI is invalid
    • validateDate

      public static void validateDate(LocalDate date)
      Validates the provided date.

      A valid date must be non-null and not earlier than 1994-01-04.

      Parameters:
      date - the date to be validated
      Throws:
      IllegalArgumentException - if the date is null or earlier than 1994-01-04
    • workingDays

      public static List<LocalDate> workingDays(LocalDate startDate, LocalDate endDate)
      Calculates a list of working days between two given dates, excluding weekends (Saturdays and Sundays) and target closing days.
      Parameters:
      startDate - the starting date of the range (inclusive)
      endDate - the ending date of the range (inclusive)
      Returns:
      a list of LocalDate objects representing the working days
      See Also:
    • yearsBetween

      public static List<Integer> yearsBetween(LocalDate startDate, LocalDate endDate)
      Retrieves a list of all years in the range between the provided start date and end date, inclusive.
      Parameters:
      startDate - the starting date of the range (inclusive)
      endDate - the ending date of the range (inclusive)
      Returns:
      a list of integers representing all years in the specified date range