Coverage Report - net.objectlab.kit.datecalc.joda.LocalDateCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalDateCalculator
89%
34/38
73%
22/30
2.545
 
 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: LocalDateCalculator.java 311 2010-03-23 21:08:11Z marchy $
 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  
 
 37  
 import net.objectlab.kit.datecalc.common.AbstractDateCalculator;
 38  
 import net.objectlab.kit.datecalc.common.DateCalculator;
 39  
 import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar;
 40  
 import net.objectlab.kit.datecalc.common.HolidayCalendar;
 41  
 import net.objectlab.kit.datecalc.common.HolidayHandler;
 42  
 import net.objectlab.kit.datecalc.common.WorkingWeek;
 43  
 
 44  
 import org.joda.time.LocalDate;
 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 Benoit Xhenseval
 52  
  * @author $LastChangedBy: marchy $
 53  
  * @version $Revision: 311 $ $Date: 2010-03-23 17:08:11 -0400 (Tue, 23 Mar 2010) $
 54  
  * 
 55  
  */
 56  7133
 public class LocalDateCalculator extends AbstractDateCalculator<LocalDate> {
 57  
 
 58  740
     private JodaWorkingWeek workingWeek = JodaWorkingWeek.DEFAULT;
 59  
 
 60  
     @SuppressWarnings("unchecked")
 61  
     public LocalDateCalculator() {
 62  367
         this(null, null, new DefaultHolidayCalendar<LocalDate>(Collections.EMPTY_SET), null);
 63  367
     }
 64  
 
 65  
     public LocalDateCalculator(final String name, final LocalDate startDate, final HolidayCalendar<LocalDate> holidayCalendar,
 66  
             final HolidayHandler<LocalDate> holidayHandler) {
 67  740
         super(name, holidayCalendar, holidayHandler);
 68  740
         setStartDate(startDate);
 69  740
     }
 70  
 
 71  
     /**
 72  
      * Set the working week.
 73  
      * @param week the JodaWorkingWeek
 74  
      * @throws IllegalArgumentException if the week is not a JodaWorkingWeek.
 75  
      */
 76  
     public void setWorkingWeek(final WorkingWeek week) {
 77  30
         if (week instanceof JodaWorkingWeek) {
 78  30
             workingWeek = (JodaWorkingWeek) week;
 79  
         } else {
 80  0
             throw new IllegalArgumentException("Please give an instance of JodaWorkingWeek");
 81  
         }
 82  30
     }
 83  
 
 84  
     /**
 85  
      * is the date a non-working day according to the WorkingWeek?
 86  
      */
 87  
     public boolean isWeekend(final LocalDate date) {
 88  4879
         assert workingWeek != null;
 89  4879
         return !workingWeek.isWorkingDay(date);
 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  
     public DateCalculator<LocalDate> moveByDays(final int days) {
 101  1036
         setCurrentIncrement(days);
 102  
 
 103  1036
         setCurrentBusinessDate(getCurrentBusinessDate().plusDays(days));
 104  
 
 105  1036
         if (getHolidayHandler() != null) {
 106  1033
             setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
 107  
         }
 108  
 
 109  1036
         return this;
 110  
     }
 111  
 
 112  
     @Override
 113  
     public DateCalculator<LocalDate> moveByMonths(final int months) {
 114  312
         setCurrentIncrement(months);
 115  
 
 116  312
         setCurrentBusinessDate(getCurrentBusinessDate().plusMonths(months));
 117  
 
 118  312
         if (getHolidayHandler() != null) {
 119  312
             setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
 120  
         }
 121  
 
 122  312
         return this;
 123  
     }
 124  
 
 125  
     @Override
 126  
     protected DateCalculator<LocalDate> createNewCalculator(final String name, final LocalDate startDate,
 127  
             final HolidayCalendar<LocalDate> holidays, final HolidayHandler<LocalDate> handler) {
 128  3
         return new LocalDateCalculator(name, startDate, holidays, handler);
 129  
     }
 130  
 
 131  
     @Override
 132  
     protected LocalDate getToday() {
 133  3
         return new LocalDate();
 134  
     }
 135  
 
 136  
     @Override
 137  
     protected LocalDate compareDate(final LocalDate date1, final LocalDate date2, final boolean returnEarliest) {
 138  6
         if (date1 == null || date2 == null) {
 139  0
             return null;
 140  
         }
 141  6
         if (returnEarliest) {
 142  3
             return date1.isAfter(date2) ? date2 : date1;
 143  
         } else {
 144  3
             return date2.isAfter(date1) ? date2 : date1;
 145  
         }
 146  
     }
 147  
 
 148  
     @Override
 149  
     protected void checkBoundary(final LocalDate date) {
 150  4005
         final LocalDate early = getHolidayCalendar().getEarlyBoundary();
 151  4005
         if (early != null && early.isAfter(date)) {
 152  0
             throw new IndexOutOfBoundsException(date + " is before the early boundary " + early);
 153  
         }
 154  
 
 155  4005
         final LocalDate late = getHolidayCalendar().getLateBoundary();
 156  4005
         if (late != null && late.isBefore(date)) {
 157  0
             throw new IndexOutOfBoundsException(date + " is after the late boundary " + late);
 158  
         }
 159  4005
     }
 160  
 
 161  
     @Override
 162  
     protected LocalDate clone(LocalDate date) {
 163  6
         return date;
 164  
     }
 165  
 }
 166  
 
 167  
 /*
 168  
  * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
 169  
  * 
 170  
  * Based in London, we are world leaders in the design and development 
 171  
  * of bespoke applications for the securities financing markets.
 172  
  * 
 173  
  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
 174  
  *           ___  _     _           _   _          _
 175  
  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
 176  
  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
 177  
  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
 178  
  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
 179  
  *                   |__/
 180  
  *
 181  
  *                     www.ObjectLab.co.uk
 182  
  */