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

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

The 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.MINUTE_OF_HOUR;
import static net.fortuna.ical4j.transform.recurrence.Frequency.*;

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

    private final List minuteList;

    public ByMinuteRule(List minuteList, Frequency frequency) {
        super(frequency);
        this.minuteList = minuteList;
    }

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

    private class LimitFilter implements Function> {
        @Override
        public Optional apply(T date) {
            if (minuteList.contains(getMinute(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 minutes..
            minuteList.forEach(minute -> {
                T candidate = withTemporalField(date, MINUTE_OF_HOUR, minute);
                retVal.add(candidate);
            });
            return retVal;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy