@Override
    public IntervalsSet getIntervalsIn(Interval i) {
        IntervalsSet set = new IntervalsSet();
        
        // if period is monthly, add only intervals where same day  of month
        // as in reference date is possible in given month.
        boolean checkDayInMonth = repeatingPeriod.getPeriodType() == PeriodType.months();
        
        // get initial periods number from reference 
        // which is the latest period start before range
        int periods = getNearestPeriodStartToAndBefore(i.getStart());      
        LocalDateTime intervalStart = referenceIntervalStart.plus(repeatingPeriod.toPeriod().multipliedBy(periods));
        LocalDateTime intervalEnd = intervalStart.plus(intervalDuration);     
        
        // if first interval do not cross range border, move to the next,
        // which must be after start due to current is tha latest period before start
        if (intervalStart.plus(intervalDuration).isBefore(i.getStart())) {
            periods++;
            intervalStart = referenceIntervalStart.plus(repeatingPeriod.toPeriod().multipliedBy(periods));
            intervalEnd = intervalStart.plus(intervalDuration);     
        }
        
        while(!intervalStart.isAfter(i.getEnd())) {

            // if start do not match reference start and repeating pattern
            // ie if repeating each month with reference on 31st day of month,
            // months that have less than 31 days are not added
            
            // so add interval only if not deviate condition
            if (!checkDayInMonth || (intervalStart.getDayOfMonth() == referenceIntervalStart.getDayOfMonth())) {
                set.unionWith(intervalStart, intervalEnd);
            } 
            
            periods += 1;
            intervalStart = referenceIntervalStart.plus(repeatingPeriod.toPeriod().multipliedBy(periods));            
            intervalEnd = intervalStart.plus(intervalDuration);    
        
        }
        
        return set;
    }