View Javadoc
1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
3    *
4    * Based in London, we are world leaders in the design and development
5    * of bespoke applications for the securities financing markets.
6    *
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id: CalendarForwardHandler.java 203 2006-10-11 12:53:07Z benoitx $
18   *
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
32   */
33  package net.objectlab.kit.datecalc.jdk;
34  
35  import java.util.Calendar;
36  
37  import net.objectlab.kit.datecalc.common.BaseCalculator;
38  import net.objectlab.kit.datecalc.common.HolidayHandler;
39  import net.objectlab.kit.datecalc.common.HolidayHandlerType;
40  import net.objectlab.kit.datecalc.common.NonWorkingDayChecker;
41  
42  /**
43   * A Jdk <code>Calendar</code> implementation of the
44   * {@link net.objectlab.kit.datecalc.common.HolidayHandler}, for the
45   * <strong>Forward</strong> algorithm.
46   *
47   * @author Marcin Jekot
48   * @author Benoit Xhenseval
49   */
50  public class CalendarForwardUnlessNegativeHandler implements HolidayHandler<Calendar> {
51  
52      /**
53       * If the current date of the give calculator is a non-working day, it will
54       * be moved according to the algorithm implemented.
55       *
56       * @param calculator
57       *            the calculator
58       * @return the date which may have moved.
59       */
60      public Calendar moveCurrentDate(final BaseCalculator<Calendar> calculator) {
61          return adjustDate(calculator.getCurrentBusinessDate(), calculator.getCurrentIncrement(), calculator);
62      }
63  
64      // -----------------------------------------------------------------------
65      //
66      // ObjectLab, world leaders in the design and development of bespoke
67      // applications for the securities financing markets.
68      // www.ObjectLab.co.uk
69      //
70      // -----------------------------------------------------------------------
71  
72      public Calendar adjustDate(final Calendar startDate, final int increment, final NonWorkingDayChecker<Calendar> checker) {
73          final Calendar cal = (Calendar) startDate.clone();
74  
75          while (checker.isNonWorkingDay(cal)) {
76              if (increment < 0) {
77                  cal.add(Calendar.DAY_OF_MONTH, -1);
78              } else {
79                  cal.add(Calendar.DAY_OF_MONTH, 1);
80              }
81          }
82  
83          return cal;
84      }
85  
86      /**
87       * Give the type name for this algorithm.
88       *
89       * @return algorithm name.
90       */
91      public String getType() {
92          return HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
93      }
94  }
95  
96  /*
97   * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
98   *
99   * Based in London, we are world leaders in the design and development
100  * of bespoke applications for the securities financing markets.
101  *
102  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
103  *           ___  _     _           _   _          _
104  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
105  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
106  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
107  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
108  *                   |__/
109  *
110  *                     www.ObjectLab.co.uk
111  */