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