Class EURConversionUtil

java.lang.Object
eu.dnetlib.dhp.schema.common.EURConversionUtil

public class EURConversionUtil extends Object
Utility class for converting currency amounts to EUR with high precision. Conversion policy: - Uses BigDecimal for deterministic precision. - Fixed rounding mode: HALF_UP, scale: 2 decimal places (standard for EUR). - If currency is EUR, returns the amount as-is. - If currency/rate is unknown or missing, returns null (no fallback fabrication). - Emits warnings for invalid/missing data.
  • Method Details

    • convertToEUR

      public static Double convertToEUR(Double amount, String sourceCurrency)
      Converts an amount from the source currency to EUR using reference rates.
      Parameters:
      amount - the amount to convert
      sourceCurrency - ISO 4217 currency code (e.g., "USD", "GBP", "EUR")
      Returns:
      EUR equivalent (rounded to 2 decimal places), or null if conversion fails
    • convertToEUR

      public static Double convertToEUR(Double amount, String sourceCurrency, Double conversionRate)
      Converts an amount from the source currency to EUR using a provided rate.
      Parameters:
      amount - the amount to convert
      sourceCurrency - ISO 4217 currency code
      conversionRate - the exchange rate (1 sourceCurrency = X EUR)
      Returns:
      EUR equivalent (rounded to 2 decimal places), or null if conversion fails
    • convertToEUR

      public static Double convertToEUR(Double amount, String sourceCurrency, Double conversionRate, String rateSource)
      Converts an amount from the source currency to EUR. Behavior: - If sourceCurrency is EUR, returns amount unchanged. - If conversionRate is provided, uses it directly. - Otherwise, looks up rate from reference table. - If rate not found, returns null and logs warning. - If amount or currency is null/invalid, returns null.
      Parameters:
      amount - the amount to convert (nullable)
      sourceCurrency - ISO 4217 currency code (nullable)
      conversionRate - optional explicit conversion rate (1 source = X EUR)
      rateSource - optional source identifier for provenance tracking
      Returns:
      EUR equivalent (rounded to 2 decimal places), or null
    • normalizeEUR

      public static Double normalizeEUR(Double amount)
      Normalizes an EUR amount to ensure consistent scale (2 decimal places).
      Parameters:
      amount - the EUR amount
      Returns:
      normalized amount, or null if invalid
    • getReferenceRate

      public static BigDecimal getReferenceRate(String currencyCode)
      Gets the reference rate for a currency (for testing/diagnostics).
      Parameters:
      currencyCode - ISO 4217 currency code
      Returns:
      the rate as BigDecimal, or null if not found
    • isSupportedCurrency

      public static boolean isSupportedCurrency(String currencyCode)
      Checks if a currency is supported by reference rates.
      Parameters:
      currencyCode - ISO 4217 currency code
      Returns:
      true if supported