Coverage Report - net.objectlab.kit.datecalc.joda.YearMonthDayDateCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
YearMonthDayDateCalculator
93%
45/48
82%
28/34
2.833
 
 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: YearMonthDayDateCalculator.java 300 2010-03-18 20:09:14Z 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.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  
  * @author $LastChangedBy: benoitx $
 56  
  * @version $Revision: 300 $ $Date: 2010-03-18 16:09:14 -0400 (Thu, 18 Mar 2010) $
 57  
  * 
 58  
  */
 59  4562
 public class YearMonthDayDateCalculator extends AbstractDateCalculator<YearMonthDay> {
 60  
 
 61  
     private LocalDateCalculator delegate;
 62  
 
 63  
     @SuppressWarnings("unchecked")
 64  
     public YearMonthDayDateCalculator() {
 65  367
         this(null, null, new DefaultHolidayCalendar<YearMonthDay>(Collections.EMPTY_SET), null);
 66  367
     }
 67  
 
 68  
     public YearMonthDayDateCalculator(final String name, final YearMonthDay startDate, final HolidayCalendar<YearMonthDay> nonWorkingDays,
 69  
             final HolidayHandler<YearMonthDay> holidayHandler) {
 70  370
         super(name, nonWorkingDays, holidayHandler);
 71  
 
 72  370
         final Set<LocalDate> dates = new HashSet<LocalDate>();
 73  370
         for (final YearMonthDay d : nonWorkingDays.getHolidays()) {
 74  14
             dates.add(d.toLocalDate());
 75  
         }
 76  
 
 77  370
         final YearMonthDay early = nonWorkingDays.getEarlyBoundary();
 78  370
         final YearMonthDay late = nonWorkingDays.getLateBoundary();
 79  370
         final DefaultHolidayCalendar<LocalDate> cal = new DefaultHolidayCalendar<LocalDate>(dates, early != null ? new LocalDate(early) : null,
 80  
                 late != null ? new LocalDate(late) : null);
 81  
 
 82  370
         final HolidayHandler<LocalDate> locDate = new HolidayHandlerYearMonthDayWrapper(holidayHandler, this);
 83  
 
 84  370
         delegate = new LocalDateCalculator(name, (startDate != null ? startDate.toLocalDate() : null), cal, locDate);
 85  370
         setStartDate(startDate);
 86  370
     }
 87  
 
 88  
     // TODO throw an exception if the type is incorrect
 89  
     public void setWorkingWeek(final WorkingWeek week) {
 90  15
         delegate.setWorkingWeek(week);
 91  15
     }
 92  
 
 93  
     // -----------------------------------------------------------------------
 94  
     //
 95  
     //    ObjectLab, world leaders in the design and development of bespoke 
 96  
     //          applications for the securities financing markets.
 97  
     //                         www.ObjectLab.co.uk
 98  
     //
 99  
     // -----------------------------------------------------------------------
 100  
 
 101  
     /**
 102  
      * is the date a non-working day according to the WorkingWeek?
 103  
      */
 104  
     public boolean isWeekend(final YearMonthDay date) {
 105  1773
         if (date != null && delegate != null) {
 106  1770
             return delegate.isWeekend(date.toLocalDate());
 107  
         }
 108  3
         return false;
 109  
     }
 110  
 
 111  
     public DateCalculator<YearMonthDay> moveByDays(final int days) {
 112  518
         setCurrentIncrement(days);
 113  518
         delegate.setCurrentIncrement(days);
 114  518
         delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate());
 115  518
         setCurrentBusinessDate(new YearMonthDay(delegate.moveByDays(days).getCurrentBusinessDate()));
 116  518
         return this;
 117  
     }
 118  
 
 119  
     @Override
 120  
     protected DateCalculator<YearMonthDay> createNewCalculator(final String name, final YearMonthDay startDate, final HolidayCalendar<YearMonthDay> holidays,
 121  
             final HolidayHandler<YearMonthDay> handler) {
 122  3
         return new YearMonthDayDateCalculator(name, startDate, holidays, handler);
 123  
     }
 124  
 
 125  
     @Override
 126  
     public final void setStartDate(final YearMonthDay startDate) {
 127  873
         if (delegate != null) {
 128  873
             delegate.setStartDate(startDate != null ? startDate.toLocalDate() : null);
 129  
         }
 130  873
         super.setStartDate(startDate);
 131  873
     }
 132  
 
 133  
     @Override
 134  
     protected YearMonthDay getToday() {
 135  6
         return new YearMonthDay();
 136  
     }
 137  
 
 138  
     @Override
 139  
     protected DateCalculator<YearMonthDay> moveByMonths(final int months) {
 140  156
         setCurrentIncrement(months);
 141  156
         delegate.setCurrentIncrement(months);
 142  156
         delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate());
 143  156
         setCurrentBusinessDate(new YearMonthDay(delegate.moveByMonths(months).getCurrentBusinessDate()));
 144  156
         return this;
 145  
     }
 146  
 
 147  
     @Override
 148  
     protected YearMonthDay compareDate(final YearMonthDay date1, final YearMonthDay date2, final boolean returnEarliest) {
 149  6
         if (date1 == null || date2 == null) {
 150  0
             return null;
 151  
         }
 152  6
         if (returnEarliest) {
 153  3
             return date1.isAfter(date2) ? date2 : date1;
 154  
         } else {
 155  3
             return date2.isAfter(date1) ? date2 : date1;
 156  
         }
 157  
     }
 158  
 
 159  
     @Override
 160  
     protected void checkBoundary(final YearMonthDay date) {
 161  2265
         final YearMonthDay early = getHolidayCalendar().getEarlyBoundary();
 162  2265
         if (early != null && early.isAfter(date)) {
 163  0
             throw new IndexOutOfBoundsException(date + " is before the early boundary " + early);
 164  
         }
 165  
 
 166  2265
         final YearMonthDay late = getHolidayCalendar().getLateBoundary();
 167  2265
         if (late != null && late.isBefore(date)) {
 168  0
             throw new IndexOutOfBoundsException(date + " is after the late boundary " + late);
 169  
         }
 170  2265
     }
 171  
 
 172  
     @Override
 173  
     protected YearMonthDay clone(YearMonthDay date) {
 174  6
         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  
  */