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 java.time.LocalDate;
036
037import net.objectlab.kit.datecalc.common.BaseCalculator;
038import net.objectlab.kit.datecalc.common.HolidayHandler;
039import net.objectlab.kit.datecalc.common.HolidayHandlerType;
040import net.objectlab.kit.datecalc.common.NonWorkingDayChecker;
041
042/**
043 * A modified following handler will move the date forward if it falls on a non
044 * working day BUT, if the new date falls into another month, it will revert to
045 * moving backward until it finds a working day.
046 *
047 * @author Benoit Xhenseval
048 *
049 */
050public class LocalDateModifiedFollowingHandler implements HolidayHandler<LocalDate> {
051
052    /**
053     * If the current date of the give calculator is a non-working day, it will
054     * be moved according to the algorithm implemented.
055     *
056     * @param calculator
057     *            the calculator
058     * @return the date which may have moved.
059     */
060    @Override
061    public LocalDate moveCurrentDate(final BaseCalculator<LocalDate> calculator) {
062        return adjustDate(calculator.getCurrentBusinessDate(), 1, calculator);
063    }
064
065    // -----------------------------------------------------------------------
066    //
067    // ObjectLab, world leaders in the design and development of bespoke
068    // applications for the securities financing markets.
069    // www.ObjectLab.co.uk
070    //
071    // -----------------------------------------------------------------------
072    @Override
073    public LocalDate adjustDate(LocalDate startDate, int increment, NonWorkingDayChecker<LocalDate> checker) {
074        LocalDate date = startDate;
075        final int month = date.getMonthValue();
076        int stepToUse = increment;
077        while (checker.isNonWorkingDay(date)) {
078            date = date.plusDays(stepToUse);
079            if (date.getMonthValue() != month) {
080                // flick to backward
081                stepToUse *= -1;
082                date = date.plusDays(stepToUse);
083            }
084        }
085        return date;
086    }
087
088    /**
089     * Give the type name for this algorithm.
090     *
091     * @return algorithm name.
092     */
093    @Override
094    public String getType() {
095        return HolidayHandlerType.MODIFIED_FOLLOWING;
096    }
097}
098
099/*
100 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
101 *
102 * Based in London, we are world leaders in the design and development
103 * of bespoke applications for the securities financing markets.
104 *
105 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
106 *           ___  _     _           _   _          _
107 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
108 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
109 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
110 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
111 *                   |__/
112 *
113 *                     www.ObjectLab.co.uk
114 */