001/*
002 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
003 *
004 * Based in London, we are world leaders in the design and development
005 * of bespoke applications for the securities financing markets.
006 *
007 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
008 *           ___  _     _           _   _          _
009 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
010 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
011 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
012 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
013 *                   |__/
014 *
015 *                     www.ObjectLab.co.uk
016 *
017 * $Id$
018 *
019 * Copyright 2006 the original author or authors.
020 *
021 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
022 * use this file except in compliance with the License. You may obtain a copy of
023 * the License at
024 *
025 * http://www.apache.org/licenses/LICENSE-2.0
026 *
027 * Unless required by applicable law or agreed to in writing, software
028 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
029 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
030 * License for the specific language governing permissions and limitations under
031 * the License.
032 */
033package net.objectlab.kit.datecalc.jdk8;
034
035import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
036import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
037import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
038import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEDING;
039
040import java.time.LocalDate;
041
042import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
043import net.objectlab.kit.datecalc.common.CurrencyDateCalculatorBuilder;
044import net.objectlab.kit.datecalc.common.HolidayHandler;
045import net.objectlab.kit.datecalc.common.HolidayHandlerType;
046import net.objectlab.kit.datecalc.common.IMMDateCalculator;
047import net.objectlab.kit.datecalc.common.PeriodCountCalculator;
048import net.objectlab.kit.datecalc.common.SpotLag;
049
050/**
051 * The default factory for getting Joda <code>LocalDate</code> based
052 * calculators.
053 *
054 * @author Benoit Xhenseval
055 *
056 */
057public class LocalDateKitCalculatorsFactory extends AbstractKitCalculatorsFactory<LocalDate> {
058
059    private static final LocalDateKitCalculatorsFactory DEFAULT = new LocalDateKitCalculatorsFactory();
060
061    private static final PeriodCountCalculator<LocalDate> PCC = new LocalDatePeriodCountCalculator();
062
063    private static final IMMDateCalculator<LocalDate> IMMDC = new LocalDateIMMDateCalculator();
064
065    public static LocalDateKitCalculatorsFactory getDefaultInstance() {
066        return DEFAULT;
067    }
068
069    /**
070     * Return a builder using the registered calendars/working weeks and a Modified Forward Holiday handler for the currency pair; this
071     * does NOT copy the calendars or Currency Config.
072     *
073     * If you want to change some of the parameters, simply modify the Builder returned and pass it to the constructor of the
074     * calculator you are interested in.
075     */
076    @Override
077    public CurrencyDateCalculatorBuilder<LocalDate> getDefaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2,
078            final SpotLag spotLag) {
079        final CurrencyDateCalculatorBuilder<LocalDate> builder = new CurrencyDateCalculatorBuilder<LocalDate>().currencyPair(ccy1, ccy2, spotLag);
080
081        return configureCurrencyCalculatorBuilder(builder).tenorHolidayHandler(new LocalDateModifiedFollowingHandler());
082    }
083
084    public static CurrencyDateCalculatorBuilder<LocalDate> defaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2,
085            final SpotLag spotLag) {
086        return DEFAULT.getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag);
087    }
088
089    public static LocalDateCurrencyDateCalculator forwardCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
090        return DEFAULT.getDefaultCurrencyDateCalculator(ccy1, ccy2, spotLag);
091    }
092
093    @Override
094    public LocalDateCurrencyDateCalculator buildCurrencyDateCalculator(final CurrencyDateCalculatorBuilder<LocalDate> builder) {
095        return new LocalDateCurrencyDateCalculator(builder);
096    }
097
098    @Override
099    public LocalDateCurrencyDateCalculator getDefaultCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
100        return new LocalDateCurrencyDateCalculator(getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag));
101    }
102
103    public static LocalDateCalculator forwardCalculator(final String name) {
104        return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD);
105    }
106
107    public static LocalDateCalculator backwardCalculator(final String name) {
108        return DEFAULT.getDateCalculator(name, HolidayHandlerType.BACKWARD);
109    }
110
111    public static LocalDateCalculator forwardUnlessMovingBackCalculator(final String name) {
112        return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK);
113    }
114
115    public static LocalDateCalculator modifiedFollowingCalculator(final String name) {
116        return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_FOLLOWING);
117    }
118
119    public static LocalDateCalculator modifiedPrecedingCalculator(final String name) {
120        return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_PRECEDING);
121    }
122
123    // -----------------------------------------------------------------------
124    //
125    // ObjectLab, world leaders in the design and development of bespoke
126    // applications for the securities financing markets.
127    // www.ObjectLab.co.uk
128    //
129    // -----------------------------------------------------------------------
130
131    /**
132     * Create a new DateCalculator for a given name and type of handling.
133     *
134     * @param name
135     *            calendar name (holidays set interested in). If there is set of
136     *            holidays with that name, it will return a DateCalculator with
137     *            an empty holiday set (will work on Weekend only).
138     * @param holidayHandlerType
139     *            typically one of the value of HolidayHandlerType
140     * @return a new DateCalculator
141     */
142    @Override
143    public LocalDateCalculator getDateCalculator(final String name, final String holidayHandlerType) {
144        final LocalDateCalculator cal = new LocalDateCalculator();
145        cal.setName(name);
146        setHolidays(name, cal);
147
148        cal.setHolidayHandler(getHolidayHandler(holidayHandlerType));
149        return cal;
150    }
151
152    @Override
153    public HolidayHandler<LocalDate> getHolidayHandler(final String holidayHandlerType) {
154        if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
155            return new LocalDateForwardHandler();
156        } else if (BACKWARD.equals(holidayHandlerType)) {
157            return new LocalDateBackwardHandler();
158        } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
159            return new LocalDateModifiedFollowingHandler();
160        } else if (MODIFIED_PRECEDING.equals(holidayHandlerType)) {
161            return new LocalDateModifiedPrecedingHandler();
162        } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
163            return new LocalDateForwardUnlessNegativeHandler();
164        } else if (holidayHandlerType != null) {
165            throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
166        }
167        return null;
168    }
169
170    @Override
171    public PeriodCountCalculator<LocalDate> getPeriodCountCalculator() {
172        return PCC;
173    }
174
175    @Override
176    public IMMDateCalculator<LocalDate> getIMMDateCalculator() {
177        return IMMDC;
178    }
179}
180
181/*
182 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
183 *
184 * Based in London, we are world leaders in the design and development
185 * of bespoke applications for the securities financing markets.
186 *
187 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
188 *           ___  _     _           _   _          _
189 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
190 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
191 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
192 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
193 *                   |__/
194 *
195 *                     www.ObjectLab.co.uk
196 */