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$
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   */package net.objectlab.kit.datecalc.jdk;
33  
34   import java.util.Calendar;
35  
36   import net.objectlab.kit.datecalc.common.BaseCalculator;
37   import net.objectlab.kit.datecalc.common.HolidayHandler;
38   import net.objectlab.kit.datecalc.common.HolidayHandlerType;
39   import net.objectlab.kit.datecalc.common.NonWorkingDayChecker;
40  
41   /**
42    * A Jdk <code>Calendar</code> implementation of the
43    * {@link net.objectlab.kit.datecalc.common.HolidayHandler}, for the
44    * <strong>Modified Following</strong> algorithm.
45    *
46    * @author Marcin Jekot
47    *
48    */
49   public class CalendarModifiedFollowingHandler implements HolidayHandler<Calendar> {
50  
51       /**
52        * If the current date of the give calculator is a non-working day, it will
53        * be moved according to the algorithm implemented.
54        *
55        * @param calculator
56        *            the calculator
57        * @return the date which may have moved.
58        */
59       public Calendar moveCurrentDate(final BaseCalculator<Calendar> calculator) {
60           return adjustDate(calculator.getCurrentBusinessDate(), 1, calculator);
61       }
62  
63       // -----------------------------------------------------------------------
64       //
65       // ObjectLab, world leaders in the design and development of bespoke
66       // applications for the securities financing markets.
67       // www.ObjectLab.co.uk
68       //
69       // -----------------------------------------------------------------------
70       public Calendar adjustDate(final Calendar startDate, final int increment, final NonWorkingDayChecker<Calendar> checker) {
71           final Calendar cal = (Calendar) startDate.clone();
72           int step = increment;
73           final int month = cal.get(Calendar.MONTH);
74  
75           while (checker.isNonWorkingDay(cal)) {
76               cal.add(Calendar.DAY_OF_MONTH, step);
77               if (month != cal.get(Calendar.MONTH)) {
78                   // switch direction and go back
79                   step *= -1;
80                   cal.add(Calendar.DAY_OF_MONTH, step);
81               }
82           }
83  
84           return cal;
85       }
86  
87       /**
88        * Give the type name for this algorithm.
89        *
90        * @return algorithm name.
91        */
92       public String getType() {
93           return HolidayHandlerType.MODIFIED_FOLLOWING;
94       }
95   }
96  
97   /*
98    * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
99    *
100   * Based in London, we are world leaders in the design and development
101   * of bespoke applications for the securities financing markets.
102   *
103   * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
104   *           ___  _     _           _   _          _
105   *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
106   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
107   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
108   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
109   *                   |__/
110   *
111   *                     www.ObjectLab.co.uk
112   */