Example usage for java.time LocalDate plusMonths

List of usage examples for java.time LocalDate plusMonths

Introduction

In this page you can find the example usage for java.time LocalDate plusMonths.

Prototype

public LocalDate plusMonths(long monthsToAdd) 

Source Link

Document

Returns a copy of this LocalDate with the specified number of months added.

Usage

From source file:org.silverpeas.core.webapi.calendar.CalendarWebManager.java

/**
 * Gets the next event occurrences from now.
 * @param componentIds identifiers of aimed component instance.
 * @param calendarIdsToExclude identifier of calendars which linked occurrences must be excluded
 * from the result./* w  w w. j  a  v a  2s .c om*/
 * @param usersToInclude identifiers of users which linked occurrences must be included into the
 * result
 * @param calendarIdsToInclude identifier of calendars which linked occurrences must be included
 * into the result.
 * @param zoneId the identifier of the zone.
 * @param limit the maximum occurrences the result must have (must be lower than 500)
 * @return a list of {@link CalendarEventOccurrence}.
 */
public Stream<CalendarEventOccurrence> getNextEventOccurrences(final List<String> componentIds,
        final Set<String> calendarIdsToExclude, final Set<User> usersToInclude,
        final Set<String> calendarIdsToInclude, final ZoneId zoneId, final Integer limit) {
    final User currentRequester = User.getCurrentRequester();
    // load calendars
    final List<Calendar> calendars = componentIds.stream().flatMap(i -> getCalendarsHandledBy(i).stream())
            .distinct().collect(Collectors.toList());
    // includes/excludes
    calendarIdsToInclude.removeAll(calendarIdsToExclude);
    calendars.removeIf(c -> calendarIdsToExclude.contains(c.getId()));
    if (!calendarIdsToInclude.isEmpty()) {
        calendars.forEach(c -> calendarIdsToInclude.remove(c.getId()));
        calendarIdsToInclude.forEach(i -> {
            Calendar calendarToInclude = Calendar.getById(i);
            if (calendarToInclude.canBeAccessedBy(currentRequester)) {
                calendars.add(calendarToInclude);
            }
        });
    }
    // loading occurrences
    final int nbOccLimit = (limit != null && limit > 0 && limit <= 500) ? limit : DEFAULT_NB_MAX_NEXT_OCC;
    final LocalDate startDate = zoneId != null ? LocalDateTime.now(zoneId).toLocalDate() : LocalDate.now();
    final Set<CalendarEventOccurrence> occurrences = new HashSet<>();
    for (int nbMonthsToAdd : getNextEventTimeWindows()) {
        occurrences.clear();
        LocalDate endDate = startDate.plusMonths(nbMonthsToAdd);
        occurrences.addAll(getEventOccurrencesOf(startDate, endDate, calendars));
        if (!usersToInclude.isEmpty()) {
            getAllEventOccurrencesByUserIds(Pair.of(componentIds, currentRequester), startDate, endDate,
                    usersToInclude).forEach((u, o) -> occurrences.addAll(o));
        }
        if (occurrences.size() >= nbOccLimit) {
            break;
        }
    }
    return occurrences.stream().sorted(COMPARATOR_BY_DATE_ASC).limit(nbOccLimit);
}