All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.jooq.CurrencyIso4217NumericCodeConverter Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy