Coverage Report - de.jollyday.parser.impl.FixedWeekdayRelativeToFixedParser
 
Classes in this File Line Coverage Branch Coverage Complexity
FixedWeekdayRelativeToFixedParser
100%
21/21
100%
14/14
4,667
FixedWeekdayRelativeToFixedParser$1
100%
1/1
N/A
4,667
 
 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 java.time.LocalDate;
 19  
 import java.util.Set;
 20  
 
 21  
 import de.jollyday.Holiday;
 22  
 import de.jollyday.HolidayType;
 23  
 import de.jollyday.config.FixedWeekdayRelativeToFixed;
 24  
 import de.jollyday.config.Holidays;
 25  
 import de.jollyday.config.When;
 26  
 import de.jollyday.parser.AbstractHolidayParser;
 27  
 
 28  
 /**
 29  
  * Parses fixed weekday relative to fixed date.
 30  
  * 
 31  
  * @author Sven Diedrichsen
 32  
  * @version $Id: $
 33  
  */
 34  7
 public class FixedWeekdayRelativeToFixedParser extends AbstractHolidayParser {
 35  
 
 36  
         /**
 37  
          * Parses the provided configuration and creates holidays for the provided
 38  
          * year.
 39  
          */
 40  
         @Override
 41  
         public void parse(int year, Set<Holiday> holidays, final Holidays config) {
 42  7
                 for (FixedWeekdayRelativeToFixed f : config.getFixedWeekdayRelativeToFixed()) {
 43  6
                         if (!isValid(f, year)) {
 44  1
                                 continue;
 45  
                         }
 46  5
                         LocalDate day = calendarUtil.create(year, f.getDay());
 47  5
                         day = moveDateToFirstOccurenceOfWeekday(f, day);
 48  5
                         int days = determineNumberOfDays(f);
 49  5
                         day = f.getWhen() == When.AFTER ? day.plusDays(days) : day.minusDays(days);
 50  5
                         HolidayType type = xmlUtil.getType(f.getLocalizedType());
 51  5
                         holidays.add(new Holiday(day, f.getDescriptionPropertiesKey(), type));
 52  5
                 }
 53  7
         }
 54  
 
 55  
         /**
 56  
          * Moves the day to the first/next occurrence of the weekday and direction specified 
 57  
          * @param f the specification of the weekday and direction of movement
 58  
          * @param day the day to move
 59  
          * @return the day moved to the weekday and in the direction as specified 
 60  
          */
 61  
         private LocalDate moveDateToFirstOccurenceOfWeekday(FixedWeekdayRelativeToFixed f, LocalDate day) {
 62  5
                 LocalDate movingDay = day;
 63  
                 do {
 64  23
                         movingDay = f.getWhen() == When.AFTER ? movingDay.plusDays(1) : movingDay.minusDays(1);
 65  23
                 } while (movingDay.getDayOfWeek() != xmlUtil.getWeekday(f.getWeekday()));
 66  5
                 return movingDay;
 67  
         }
 68  
 
 69  
         /**
 70  
          * Determines the number of days to move from the XML enumeration.
 71  
          * @param f the enumeration value
 72  
          * @return the number of days
 73  
          */
 74  
         private int determineNumberOfDays(FixedWeekdayRelativeToFixed f) {
 75  5
                 switch (f.getWhich()) {
 76  
                 case SECOND:
 77  1
                         return 7;
 78  
                 case THIRD:
 79  1
                         return 14;
 80  
                 case FOURTH:
 81  1
                         return 21;
 82  
                 default:
 83  9
                         return 0;
 84  
                 }
 85  
         }
 86  
 
 87  
 }