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

net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory Maven / Gradle / Ivy

There is a newer version: 1.4.8
Show newest version
/*
 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
 *
 * Based in London, we are world leaders in the design and development
 * of bespoke applications for the securities financing markets.
 *
 * Click here to learn more
 *           ___  _     _           _   _          _
 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
 *                   |__/
 *
 *                     www.ObjectLab.co.uk
 *
 * $Id$
 *
 * Copyright 2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.objectlab.kit.datecalc.common;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.objectlab.kit.datecalc.common.ccy.CurrencyCalculatorConfig;
import net.objectlab.kit.datecalc.common.ccy.DefaultCurrencyCalculatorConfig;

/**
 * Base class for all calculator factories, it handles the holiday registration.
 *
 * @author Marcin Jekot
 *
 * @param 
 *            a representation of a date, typically JDK: Date, Calendar;
 *            Joda:LocalDate, YearMonthDay
 *
 */
public abstract class AbstractKitCalculatorsFactory implements KitCalculatorsFactory {

    private final ConcurrentMap> holidays = new ConcurrentHashMap>();

    private CurrencyCalculatorConfig currencyCalculatorConfig = new DefaultCurrencyCalculatorConfig();

    /**
     * Use this method register a specific currency config, if not provided then the DefaultCurrencyCalculatorConfig will be given.
     * @param config that specifies the set of currencies subject to USD T+1 and the WorkingWeeks per currency.
     */
    public void setCurrencyCalculatorConfig(final CurrencyCalculatorConfig config) {
        currencyCalculatorConfig = config;
    }

    /**
     * Provides the registered instance of currencyCalculatorConfig or use DefaultCurrencyCalculatorConfig if none registered.
     * @see DefaultCurrencyCalculatorConfig
     */
    public CurrencyCalculatorConfig getCurrencyCalculatorConfig() {
        if (currencyCalculatorConfig == null) {
            currencyCalculatorConfig = new DefaultCurrencyCalculatorConfig();
        }
        return currencyCalculatorConfig;
    }

    /**
     * Use this method to register a given calendar, it will replace any
     * existing one with the same name. An immutable copy is made so that any changes outside this class
     * will have no affect. It won't update any existing DateCalculator as these should
     * not be amended whilst in existence (we could otherwise get inconsistent
     * results).
     *
     * @param name
     *            the calendar name to register these holidays under.
     * @param holidaysCalendar
     *            a calendar containing a set of holidays (non-working days).
     */
    public KitCalculatorsFactory registerHolidays(final String name, final HolidayCalendar holidaysCalendar) {
        if (name != null) {
            final Set hol = new HashSet();
            if (holidaysCalendar != null && holidaysCalendar.getHolidays() != null) {
                hol.addAll(holidaysCalendar.getHolidays());
            }
            final DefaultHolidayCalendar defaultHolidayCalendar = new DefaultHolidayCalendar(hol);
            if (holidaysCalendar != null) {
                defaultHolidayCalendar.setEarlyBoundary(holidaysCalendar.getEarlyBoundary());
                defaultHolidayCalendar.setLateBoundary(holidaysCalendar.getLateBoundary());
            }
            this.holidays.put(name, new ImmutableHolidayCalendar(holidaysCalendar));
        }
        return this;
    }

    /**
     * Check if a calendar of a given name is already registered.
     * @return true if the holiday name is registered.
     */
    public boolean isHolidayCalendarRegistered(final String name) {
        return name != null && this.holidays.containsKey(name);
    }

    /**
     * Provides an immutable Holiday Calendar with that name if registered, null if not registered
     * @return an immutable Holiday Calendar that is registered, null if not registered.
     */
    public HolidayCalendar getHolidayCalendar(final String name) {
        return holidays.get(name);
    }

    /**
     * Used by extensions to set holidays in a DateCalculator.
     *
     * @param name
     *            holiday name
     * @param dc
     *            the date calculator to configure.
     */
    protected void setHolidays(final String name, final DateCalculator dc) {
        if (name != null) {
            dc.setHolidayCalendar(holidays.get(name));
        }
    }

    /**
     * Provides the immutable set of registered calendar names
     */
    public Set getRegisteredHolidayCalendarNames() {
        return Collections.unmodifiableSet(holidays.keySet());
    }

    /**
     * Unregister a given holiday calendar
     * @param calendarName
     *          the calendar name to unregister.
     */
    public KitCalculatorsFactory unregisterHolidayCalendar(final String calendarName) {
        holidays.remove(calendarName);
        return this;
    }

    /**
     * unregister all holiday calendars;
     */
    public KitCalculatorsFactory unregisterAllHolidayCalendars() {
        holidays.clear();
        return this;
    }

    /**
     * Method that may be called by the specialised factory methods and will fetch the registered holidayCalendar for all 3 currencies 
     * and the working weeks via the currencyCalculatorConfig and assigning currencyCalculatorConfig to the builder, 
     * using the DefaultCurrencyCalculatorConfig if not modified.
     * @param builder the original builder
     * @return same instance of builder but modified
     */
    protected CurrencyDateCalculatorBuilder configureCurrencyCalculatorBuilder(final CurrencyDateCalculatorBuilder builder) {
        return builder//
                .ccy1Calendar(getHolidayCalendar(builder.getCcy1())) //
                .ccy1Week(getCurrencyCalculatorConfig().getWorkingWeek(builder.getCcy1())) //
                .ccy2Calendar(getHolidayCalendar(builder.getCcy2())) //
                .ccy2Week(getCurrencyCalculatorConfig().getWorkingWeek(builder.getCcy2())) //
                .crossCcyCalendar(getHolidayCalendar(builder.getCrossCcy())) //
                .crossCcyWeek(getCurrencyCalculatorConfig().getWorkingWeek(builder.getCrossCcy())) //
                .currencyCalculatorConfig(getCurrencyCalculatorConfig());
    }
}

/*
 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
 *
 * Based in London, we are world leaders in the design and development
 * of bespoke applications for the securities financing markets.
 *
 * Click here to learn more about us
 *           ___  _     _           _   _          _
 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
 *                   |__/
 *
 *                     www.ObjectLab.co.uk
 */




© 2015 - 2025 Weber Informatics LLC | Privacy Policy