All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.fortuna.ical4j.transform.recurrence.ByHourRule Maven / Gradle / Ivy

There is a newer version: 4.0.8
Show newest version
package net.fortuna.ical4j.transform.recurrence;

import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static net.fortuna.ical4j.transform.recurrence.Frequency.*;

/**
 * Applies BYHOUR rules specified in this Recur instance to the specified date list. If no BYHOUR rules are
 * specified the date list is returned unmodified.
 */
public class ByHourRule extends AbstractDateExpansionRule {

    private final List hourList;

    public ByHourRule(List hourList, Frequency frequency) {
        super(frequency);
        this.hourList = hourList;
    }

    @Override
    public List transform(List dates) {
        if (hourList.isEmpty()) {
            return dates;
        }
        final List hourlyDates = new ArrayList<>();
        for (final T date : dates) {
            if (EnumSet.of(DAILY, WEEKLY, MONTHLY, YEARLY).contains(getFrequency())) {
                hourlyDates.addAll(new ExpansionFilter().apply(date));
            } else {
                Optional limit = new LimitFilter().apply(date);
                limit.ifPresent(hourlyDates::add);
            }
        }
        return hourlyDates;
    }

    private class LimitFilter implements Function> {
        @Override
        public Optional apply(T date) {
            if (hourList.contains(getHour(date))) {
                return Optional.of(date);
            }
            return Optional.empty();
        }
    }

    private class ExpansionFilter implements Function> {
        @Override
        public List apply(T date) {
            List retVal = new ArrayList<>();
            // construct a list of possible hours..
            hourList.forEach(hour -> {
                T candidate = withTemporalField(date, HOUR_OF_DAY, hour);
                retVal.add(candidate);
            });
            return retVal;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy