001package net.objectlab.kit.datecalc.common.ccy;
002
003import java.util.Collections;
004import java.util.HashMap;
005import java.util.HashSet;
006import java.util.Map;
007import java.util.Set;
008
009import net.objectlab.kit.datecalc.common.CalculatorConstants;
010import net.objectlab.kit.datecalc.common.WorkingWeek;
011
012/**
013 * Default for:
014 * <ul><li>currencies subject to USD Holidays for T+1: MXN, CLP, ARS (Mexican Peso, Chile Pese and Argentina Peso).</li>
015 * <li>Arabic currencies Sun-Thu: AED, BHD, EGP, KWD, OMR, QAR.</li>
016 * <li>Arabic currencies Mon-Thu: SAR, JOD.</li>
017 * </ul>
018 * @author Benoit Xhenseval
019 * @since 1.4.0
020 */
021public class DefaultCurrencyCalculatorConfig implements CurrencyCalculatorConfig {
022    private Map<String, Set<String>> currenciesSubjectToCrossCcyForT1 = new HashMap<>();
023    private Map<String, WorkingWeek> workingWeeks = new HashMap<>();
024
025    public DefaultCurrencyCalculatorConfig() {
026        super();
027        final Set<String> subjectToUsd = new HashSet<>();
028        subjectToUsd.add("MXN");
029        subjectToUsd.add("CLP");
030        subjectToUsd.add("ARS");
031        currenciesSubjectToCrossCcyForT1.put(CalculatorConstants.USD_CODE, subjectToUsd);
032
033        workingWeeks.put("AED", WorkingWeek.ARABIC_WEEK);
034        workingWeeks.put("BHD", WorkingWeek.ARABIC_WEEK);
035        workingWeeks.put("EGP", WorkingWeek.ARABIC_WEEK);
036        workingWeeks.put("KWD", WorkingWeek.ARABIC_WEEK);
037        workingWeeks.put("OMR", WorkingWeek.ARABIC_WEEK);
038        workingWeeks.put("QAR", WorkingWeek.ARABIC_WEEK);
039        workingWeeks.put("SAR", WorkingWeek.ARABIC_WEEK.intersection(WorkingWeek.DEFAULT));
040        workingWeeks.put("JOD", WorkingWeek.ARABIC_WEEK.intersection(WorkingWeek.DEFAULT));
041    }
042
043    /**
044     * Will take a copy of a non null set but doing so by replacing the internal one in one go for consistency.
045     */
046    public void setCurrenciesSubjectToCrossCcyForT1(final Map<String, Set<String>> currenciesSubjectToCrossCcyForT1) {
047        final Map<String, Set<String>> copy = new HashMap<>();
048        if (currenciesSubjectToCrossCcyForT1 != null) {
049            copy.putAll(currenciesSubjectToCrossCcyForT1);
050        }
051        this.currenciesSubjectToCrossCcyForT1 = copy;
052    }
053
054    /**
055     * Will take a copy of a non null map but doing so by replacing the internal one in one go for consistency.
056     */
057    public void setWorkingWeeks(final Map<String, WorkingWeek> workingWeeks) {
058        final Map<String, WorkingWeek> ww = new HashMap<>();
059        ww.putAll(workingWeeks);
060        this.workingWeeks = ww;
061    }
062
063    /**
064     * @return an unmodifiable set of currencies.
065     */
066    @Override
067    public Set<String> getCurrenciesSubjectToCrossCcyForT1(final String crossCcy) {
068        final Set<String> s = currenciesSubjectToCrossCcyForT1.get(crossCcy);
069        return s != null ? Collections.unmodifiableSet(s) : Collections.<String> emptySet();
070    }
071
072    /**
073     * Return a default Mon-Fri for most, but some might be Sun-Thu (Arabic countries).
074     * @param currency
075     * @return the WorkingWeek registered for this currency other the default Mon-Fri.
076     */
077    @Override
078    public WorkingWeek getWorkingWeek(final String currency) {
079        final WorkingWeek workingWeek = workingWeeks.get(currency);
080        return workingWeek != null ? workingWeek : WorkingWeek.DEFAULT;
081    }
082}