static Predicate < DateValue > byDayFilter ( final WeekdayNum [ ] days , final boolean weeksInYear , final Weekday wkst ) { return new Predicate < DateValue > ( ) { public boolean apply ( DateValue date ) { Weekday dow = Weekday . valueOf ( date ) ; int nDays ; // first day of the week in the given year or month Weekday dow0 ; // where does date appear in the year or month? // in [0, lengthOfMonthOrYear - 1] int instance ; if ( weeksInYear ) { nDays = TimeUtils . yearLength ( date . year ( ) ) ; dow0 = Weekday . firstDayOfWeekInMonth ( date . year ( ) , 1 ) ; instance = TimeUtils . dayOfYear ( date . year ( ) , date . month ( ) , date . day ( ) ) ; } else { nDays = TimeUtils . monthLength ( date . year ( ) , date . month ( ) ) ; dow0 = Weekday . firstDayOfWeekInMonth ( date . year ( ) , date . month ( ) ) ; instance = date . day ( ) - 1 ; } // which week of the year or month does this date fall on? // one-indexed int dateWeekNo ; if ( wkst . javaDayNum <= dow . javaDayNum ) { dateWeekNo = 1 + ( instance / 7 ) ; } else { dateWeekNo = ( instance / 7 ) ; } // TODO(msamuel): according to section 4.3.10 // Week number one of the calendar year is the first week which // contains at least four (4) days in that calendar year. This // rule part is only valid for YEARLY rules. // That's mentioned under the BYWEEKNO rule, and there's no mention // of it in the earlier discussion of the BYDAY rule. // Does it apply to yearly week numbers calculated for BYDAY rules in // a FREQ=YEARLY rule? for ( int i = days . length ; -- i >= 0 ; ) { WeekdayNum day = days [ i ] ; if ( day . wday == dow ) { int weekNo = day . num ; if ( 0 == weekNo ) { return true ; } if ( weekNo < 0 ) { weekNo = Util . invertWeekdayNum ( day , dow0 , nDays ) ; } if ( dateWeekNo == weekNo ) { return true ; } } } return false ; } } ; }
constructs a day filter based on a BYDAY rule .