com.databasesandlife.util.jooq.CurrencyIso4217NumericCodeSetConverter 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 com.databasesandlife.util.jooq.CurrencyIso4217NumericCodeConverter.InvalidCurrencyException;
import org.jooq.Converter;
import java.util.Collection;
import java.util.Comparator;
import java.util.Currency;
import java.util.TreeSet;
import static com.databasesandlife.util.jooq.CurrencyIso4217NumericCodeConverter.lookup;
import static java.util.Arrays.asList;
public class CurrencyIso4217NumericCodeSetConverter implements Converter {
public static class CurrencyIso4217NumericCodeSortedSet extends TreeSet {
public CurrencyIso4217NumericCodeSortedSet() {
super(Comparator.comparingInt(Currency::getNumericCode));
}
public CurrencyIso4217NumericCodeSortedSet(Collection currencyIds) throws InvalidCurrencyException {
this();
for (var c : currencyIds) add(lookup(c));
}
}
@Override public Class fromType() { return Integer[].class; }
@Override public Class toType() { return CurrencyIso4217NumericCodeSortedSet.class; }
@Override
public CurrencyIso4217NumericCodeSortedSet from(Integer[] x) {
try {
if (x == null) return null;
return new CurrencyIso4217NumericCodeSortedSet(asList(x));
}
catch (InvalidCurrencyException e) { throw new RuntimeException(e); }
}
@Override
public Integer[] to(CurrencyIso4217NumericCodeSortedSet m) {
if (m == null) return null;
return m.stream().map(Currency::getNumericCode).toArray(Integer[]::new);
}
}