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