Coverage Report - de.jollyday.parser.impl.FixedWeekdayInMonthParser
 
Classes in this File Line Coverage Branch Coverage Complexity
FixedWeekdayInMonthParser
100%
26/26
100%
12/12
3,5
FixedWeekdayInMonthParser$1
100%
1/1
N/A
3,5
 
 1  
 /**
 2  
  * Copyright 2010 Sven Diedrichsen 
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License"); 
 5  
  * you may not use this file except in compliance with the License. 
 6  
  * You may obtain a copy of the License at 
 7  
  * 
 8  
  * http://www.apache.org/licenses/LICENSE-2.0 
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software 
 11  
  * distributed under the License is distributed on an 
 12  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 13  
  * express or implied. See the License for the specific language 
 14  
  * governing permissions and limitations under the License. 
 15  
  */
 16  
 package de.jollyday.parser.impl;
 17  
 
 18  
 import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
 19  
 
 20  
 import java.time.DayOfWeek;
 21  
 import java.time.LocalDate;
 22  
 import java.util.Set;
 23  
 
 24  
 import de.jollyday.Holiday;
 25  
 import de.jollyday.HolidayType;
 26  
 import de.jollyday.config.FixedWeekdayInMonth;
 27  
 import de.jollyday.config.Holidays;
 28  
 import de.jollyday.config.Which;
 29  
 import de.jollyday.parser.AbstractHolidayParser;
 30  
 
 31  
 /**
 32  
  * The Class FixedWeekdayInMonthParser.
 33  
  * 
 34  
  * @author tboven
 35  
  * @version $Id: $
 36  
  */
 37  11
 public class FixedWeekdayInMonthParser extends AbstractHolidayParser {
 38  
 
 39  
         /*
 40  
          * (non-Javadoc)
 41  
          * 
 42  
          * @see de.jollyday.parser.HolidayParser#parse(int, java.util.Set,
 43  
          * de.jollyday.config.Holidays)
 44  
          */
 45  
         /** {@inheritDoc} */
 46  
         @Override
 47  
         public void parse(int year, Set<Holiday> holidays, final Holidays config) {
 48  142
                 for (FixedWeekdayInMonth fwm : config.getFixedWeekday()) {
 49  365
                         if (!isValid(fwm, year)) {
 50  1
                                 continue;
 51  
                         }
 52  364
                         LocalDate date = parse(year, fwm);
 53  364
                         HolidayType type = xmlUtil.getType(fwm.getLocalizedType());
 54  364
                         holidays.add(new Holiday(date, fwm.getDescriptionPropertiesKey(), type));
 55  364
                 }
 56  142
         }
 57  
 
 58  
         /**
 59  
          * Parses the {@link FixedWeekdayInMonth}.
 60  
          * 
 61  
          * @param year
 62  
          *            the year
 63  
          * @param fwm
 64  
          *            the fwm
 65  
          * @return the local date
 66  
          */
 67  
         protected LocalDate parse(int year, FixedWeekdayInMonth fwm) {
 68  383
                 LocalDate date = calendarUtil.create(year, xmlUtil.getMonth(fwm.getMonth()), 1);
 69  383
                 int direction = 1;
 70  383
                 if (Which.LAST.equals(fwm.getWhich())) {
 71  82
                         date = date.with(lastDayOfMonth());
 72  82
                         direction = -1;
 73  
                 }
 74  383
                 date = moveToNextRequestedWeekdayByDirection(fwm, date, direction);
 75  383
                 date = moveNumberOfRequestedWeeks(fwm, date);
 76  383
                 return date;
 77  
         }
 78  
 
 79  
         private LocalDate moveNumberOfRequestedWeeks(FixedWeekdayInMonth fwm, LocalDate date) {
 80  1
                 switch (fwm.getWhich()) {
 81  
                 case SECOND:
 82  33
                         return date.plusDays(7);
 83  
                 case THIRD:
 84  109
                         return date.plusDays(14);
 85  
                 case FOURTH:
 86  74
                         return date.plusDays(21);
 87  
                 default:
 88  178
                         return date;
 89  
                 }
 90  
         }
 91  
 
 92  
         private LocalDate moveToNextRequestedWeekdayByDirection(FixedWeekdayInMonth fwm, LocalDate date, int direction) {
 93  383
                 DayOfWeek weekDay = xmlUtil.getWeekday(fwm.getWeekday());
 94  1242
                 while (date.getDayOfWeek() != weekDay) {
 95  859
                         date = date.plusDays(direction);
 96  
                 }
 97  383
                 return date;
 98  
         }
 99  
 
 100  
 }