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: LocalDateForwardHandler.java 203 2006-10-11 12:53:07Z benoitx $
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 Forward handler will move the date forward if it falls on a non working
044 * day.
045 *
046 * @author Benoit Xhenseval
047 *
048 */
049public class LocalDateForwardUnlessNegativeHandler implements HolidayHandler<LocalDate> {
050
051    /**
052     * If the current date of the give calculator is a non-working day, it will
053     * be moved according to the algorithm implemented.
054     *
055     * @param calculator
056     *            the calculator
057     * @return the date which may have moved.
058     */
059    @Override
060    public LocalDate moveCurrentDate(final BaseCalculator<LocalDate> calculator) {
061        return adjustDate(calculator.getCurrentBusinessDate(), calculator.getCurrentIncrement(), calculator);
062    }
063
064    @Override
065    public LocalDate adjustDate(LocalDate startDate, int increment, NonWorkingDayChecker<LocalDate> checker) {
066        LocalDate date = startDate;
067        while (checker.isNonWorkingDay(date)) {
068            if (increment < 0) {
069                // act as a Backward calendar
070                date = date.minusDays(1);
071            } else {
072                // move forward by a day!
073                date = date.plusDays(1);
074            }
075        }
076        return date;
077    }
078
079    /**
080     * Give the type name for this algorithm.
081     *
082     * @return algorithm name.
083     */
084    @Override
085    public String getType() {
086        return HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
087    }
088}
089
090/*
091 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
092 *
093 * Based in London, we are world leaders in the design and development
094 * of bespoke applications for the securities financing markets.
095 *
096 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
097 *           ___  _     _           _   _          _
098 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
099 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
100 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
101 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
102 *                   |__/
103 *
104 *                     www.ObjectLab.co.uk
105 */