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   */
33  package net.objectlab.kit.datecalc.jdk;
34  
35  import java.util.Calendar;
36  import java.util.Collections;
37  
38  import net.objectlab.kit.datecalc.common.AbstractDateCalculator;
39  import net.objectlab.kit.datecalc.common.DateCalculator;
40  import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar;
41  import net.objectlab.kit.datecalc.common.HolidayCalendar;
42  import net.objectlab.kit.datecalc.common.HolidayHandler;
43  import net.objectlab.kit.datecalc.common.Utils;
44  import net.objectlab.kit.datecalc.common.WorkingWeek;
45  
46  /**
47   * This class is used via the DateCalculator interface, it enables the handling
48   * of different HolidayHandler, if no HolidayHandler is defined, the calendar
49   * will NOT move a date, even if it falls on a holiday or weekend.
50   *
51   * @author Marcin Jekot
52   */
53  public class CalendarDateCalculator extends AbstractDateCalculator<Calendar> {
54  
55      private WorkingWeek workingWeek = WorkingWeek.DEFAULT;
56  
57      public CalendarDateCalculator() {
58          this(null, null, new DefaultHolidayCalendar<Calendar>(Collections.<Calendar> emptySet()), null);
59      }
60  
61      public CalendarDateCalculator(final String name, final Calendar startDate, final HolidayCalendar<Calendar> holidayCalendar,
62              final HolidayHandler<Calendar> holidayHandler) {
63          super(name, holidayCalendar, holidayHandler);
64          Calendar date = startDate;
65          if (date == null) {
66              date = getToday();
67          } else {
68              setStartDate(date);
69          }
70      }
71  
72      @Override
73      public DateCalculator<Calendar> setWorkingWeek(final WorkingWeek week) {
74          workingWeek = week;
75          return this;
76      }
77  
78      /**
79       * is the date a non-working day according to the WorkingWeek?
80       */
81      @Override
82      public boolean isWeekend(final Calendar date) {
83          assert workingWeek != null;
84          return !workingWeek.isWorkingDay(date);
85      }
86  
87      // -----------------------------------------------------------------------
88      //
89      // ObjectLab, world leaders in the design and development of bespoke
90      // applications for the securities financing markets.
91      // www.ObjectLab.co.uk
92      //
93      // -----------------------------------------------------------------------
94  
95      @Override
96      public CalendarDateCalculator moveByDays(final int days) {
97          setCurrentIncrement(days);
98          getCurrentBusinessDate().add(Calendar.DAY_OF_MONTH, days);
99  
100         if (getHolidayHandler() != null) {
101             setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
102         }
103 
104         return this;
105     }
106 
107     @Override
108     public DateCalculator<Calendar> moveByMonths(final int months) {
109         setCurrentIncrement(months);
110         final Calendar date = getCurrentBusinessDate();
111         date.add(Calendar.MONTH, months);
112 
113         setCurrentBusinessDate(date);
114 
115         if (getHolidayHandler() != null) {
116             setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
117         }
118 
119         return this;
120     }
121 
122     @Override
123     protected DateCalculator<Calendar> createNewCalculator(final String name, final Calendar startDate, final HolidayCalendar<Calendar> holidays,
124             final HolidayHandler<Calendar> handler) {
125         return new CalendarDateCalculator(name, startDate, holidays, handler);
126     }
127 
128     @Override
129     protected final Calendar getToday() {
130         return Utils.blastTime(Calendar.getInstance());
131     }
132 
133     @Override
134     protected Calendar compareDate(final Calendar date1, final Calendar date2, final boolean returnEarliest) {
135         if (date1 == null || date2 == null) {
136             return null;
137         }
138         if (returnEarliest) {
139             return date1.after(date2) ? date2 : date1;
140         } else {
141             return date2.after(date1) ? date2 : date1;
142         }
143     }
144 
145     @Override
146     protected void checkBoundary(final Calendar date) {
147         final Calendar early = getHolidayCalendar().getEarlyBoundary();
148         if (early != null && early.after(date)) {
149             throw new IndexOutOfBoundsException(date + " is before the early boundary " + early);
150         }
151 
152         final Calendar late = getHolidayCalendar().getLateBoundary();
153         if (late != null && late.before(date)) {
154             throw new IndexOutOfBoundsException(date + " is after the late boundary " + late);
155         }
156     }
157 
158     @Override
159     protected Calendar clone(final Calendar date) {
160         final Calendar cal = Calendar.getInstance();
161         cal.setTime(date.getTime());
162         return cal;
163     }
164 
165     @Override
166     public int getNumberOfBusinessDaysBetween(final Calendar d1, final Calendar d2) {
167         if (d1 == null || d2 == null) {
168             return 0;
169         }
170         final boolean d1B4d2 = !d1.after(d2);
171         Calendar start = d1B4d2 ? d1 : d2;
172         final Calendar end = d1B4d2 ? d2 : d1;
173         if (getHolidayHandler() != null) {
174             start = getHolidayHandler().adjustDate(start, 1, this);
175         }
176 
177         int count = 0;
178 
179         while (start.before(end)) {
180             if (!isNonWorkingDay(start)) {
181                 count++;
182             }
183             start.add(Calendar.DATE, 1);
184         }
185         return d1B4d2 ? count : -count;
186     }
187 
188 }
189 
190 /*
191  * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
192  *
193  * Based in London, we are world leaders in the design and development
194  * of bespoke applications for the securities financing markets.
195  *
196  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
197  *           ___  _     _           _   _          _
198  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
199  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
200  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
201  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
202  *                   |__/
203  *
204  *                     www.ObjectLab.co.uk
205  */