Package eu.dnetlib.dhp.schema.common
Class EURConversionUtil
java.lang.Object
eu.dnetlib.dhp.schema.common.EURConversionUtil
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 Summary
Modifier and TypeMethodDescriptionstatic DoubleconvertToEUR(Double amount, String sourceCurrency) Converts an amount from the source currency to EUR using reference rates.static DoubleconvertToEUR(Double amount, String sourceCurrency, Double conversionRate) Converts an amount from the source currency to EUR using a provided rate.static DoubleconvertToEUR(Double amount, String sourceCurrency, Double conversionRate, String rateSource) Converts an amount from the source currency to EUR.static BigDecimalgetReferenceRate(String currencyCode) Gets the reference rate for a currency (for testing/diagnostics).static booleanisSupportedCurrency(String currencyCode) Checks if a currency is supported by reference rates.static DoublenormalizeEUR(Double amount) Normalizes an EUR amount to ensure consistent scale (2 decimal places).
-
Method Details
-
convertToEUR
Converts an amount from the source currency to EUR using reference rates.- Parameters:
amount- the amount to convertsourceCurrency- ISO 4217 currency code (e.g., "USD", "GBP", "EUR")- Returns:
- EUR equivalent (rounded to 2 decimal places), or null if conversion fails
-
convertToEUR
Converts an amount from the source currency to EUR using a provided rate.- Parameters:
amount- the amount to convertsourceCurrency- ISO 4217 currency codeconversionRate- 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
Normalizes an EUR amount to ensure consistent scale (2 decimal places).- Parameters:
amount- the EUR amount- Returns:
- normalized amount, or null if invalid
-
getReferenceRate
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
Checks if a currency is supported by reference rates.- Parameters:
currencyCode- ISO 4217 currency code- Returns:
- true if supported
-