com.databasesandlife.util.jooq.CurrencyIso4217NumericCodeConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util.jooq;
import org.jooq.Converter;
import javax.annotation.Nonnull;
import java.util.Currency;
@SuppressWarnings({ "serial" })
public class CurrencyIso4217NumericCodeConverter implements Converter {
@Override public Class fromType() { return Integer.class; }
@Override public Class toType() { return Currency.class; }
public static class InvalidCurrencyException extends Exception {
InvalidCurrencyException(String msg) { super(msg); }
}
public static @Nonnull Currency lookup(int x) throws InvalidCurrencyException {
for(var c : Currency.getAvailableCurrencies()) {
if(c.getNumericCode() == x) {
return c;
}
}
throw new InvalidCurrencyException("Unknown ISO 4217 numeric currency code: " + x);
}
@Override
public Currency from(Integer x) {
try {
if (x == null) return null;
return lookup(x);
}
catch (InvalidCurrencyException e) { throw new RuntimeException(e); }
}
@Override
public Integer to(Currency m) {
if (m == null) return null;
return m.getNumericCode();
}
}