Example usage for org.joda.time Interval contains

List of usage examples for org.joda.time Interval contains

Introduction

In this page you can find the example usage for org.joda.time Interval contains.

Prototype

public boolean contains(long millisInstant) 

Source Link

Document

Does this time interval contain the specified millisecond instant.

Usage

From source file:org.apache.druid.server.http.IntervalsResource.java

License:Apache License

@GET
@Path("/{interval}")
@Produces(MediaType.APPLICATION_JSON)//from   ww  w.j a  v a  2s  . c  o  m
public Response getSpecificIntervals(@PathParam("interval") String interval,
        @QueryParam("simple") String simple, @QueryParam("full") String full,
        @Context final HttpServletRequest req) {
    final Interval theInterval = Intervals.of(interval.replace('_', '/'));
    final Set<ImmutableDruidDataSource> datasources = InventoryViewUtils.getSecuredDataSources(req,
            serverInventoryView, authorizerMapper);

    final Comparator<Interval> comparator = Comparators.intervalsByStartThenEnd().reversed();

    if (full != null) {
        final Map<Interval, Map<String, Map<String, Object>>> retVal = new TreeMap<>(comparator);
        for (ImmutableDruidDataSource dataSource : datasources) {
            for (DataSegment dataSegment : dataSource.getSegments()) {
                if (theInterval.contains(dataSegment.getInterval())) {
                    retVal.computeIfAbsent(dataSegment.getInterval(), k -> new HashMap<>());
                    setProperties(retVal, dataSource, dataSegment);
                }
            }
        }

        return Response.ok(retVal).build();
    }

    if (simple != null) {
        final Map<Interval, Map<String, Object>> retVal = new HashMap<>();
        for (ImmutableDruidDataSource dataSource : datasources) {
            for (DataSegment dataSegment : dataSource.getSegments()) {
                if (theInterval.contains(dataSegment.getInterval())) {
                    Map<String, Object> properties = retVal.get(dataSegment.getInterval());
                    if (properties == null) {
                        properties = new HashMap<>();
                        properties.put("size", dataSegment.getSize());
                        properties.put("count", 1);

                        retVal.put(dataSegment.getInterval(), properties);
                    } else {
                        properties.put("size",
                                MapUtils.getLong(properties, "size", 0L) + dataSegment.getSize());
                        properties.put("count", MapUtils.getInt(properties, "count", 0) + 1);
                    }
                }
            }
        }

        return Response.ok(retVal).build();
    }

    final Map<String, Object> retVal = new HashMap<>();
    for (ImmutableDruidDataSource dataSource : datasources) {
        for (DataSegment dataSegment : dataSource.getSegments()) {
            if (theInterval.contains(dataSegment.getInterval())) {
                retVal.put("size", MapUtils.getLong(retVal, "size", 0L) + dataSegment.getSize());
                retVal.put("count", MapUtils.getInt(retVal, "count", 0) + 1);
            }
        }
    }

    return Response.ok(retVal).build();
}

From source file:org.apache.druid.timeline.VersionedIntervalTimeline.java

License:Apache License

/**
 * @param timeline//from   w w  w  .  j  a  v a  2s  .  com
 * @param key
 * @param entry
 *
 * @return boolean flag indicating whether or not we inserted or discarded something
 */
private boolean addAtKey(NavigableMap<Interval, TimelineEntry> timeline, Interval key, TimelineEntry entry) {
    boolean retVal = false;
    Interval currKey = key;
    Interval entryInterval = entry.getTrueInterval();

    if (!currKey.overlaps(entryInterval)) {
        return false;
    }

    while (entryInterval != null && currKey != null && currKey.overlaps(entryInterval)) {
        final Interval nextKey = timeline.higherKey(currKey);

        final int versionCompare = versionComparator.compare(entry.getVersion(),
                timeline.get(currKey).getVersion());

        if (versionCompare < 0) {
            // since the entry version is lower than the existing one, the existing one overwrites the given entry
            // if overlapped.
            if (currKey.contains(entryInterval)) {
                // the version of the entry of currKey is larger than that of the given entry. Discard it
                return true;
            } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
                //       | entry |
                //     | cur |
                // =>        |new|
                entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
            } else {
                //     | entry |
                //         | cur |
                // =>  |new|
                addIntervalToTimeline(new Interval(entryInterval.getStart(), currKey.getStart()), entry,
                        timeline);

                //     |   entry   |
                //       | cur |
                // =>          |new|
                if (entryInterval.getEnd().isAfter(currKey.getEnd())) {
                    entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
                } else {
                    // Discard this entry since there is no portion of the entry interval that goes past the end of the curr
                    // key interval.
                    entryInterval = null;
                }
            }
        } else if (versionCompare > 0) {
            // since the entry version is greater than the existing one, the given entry overwrites the existing one
            // if overlapped.
            final TimelineEntry oldEntry = timeline.remove(currKey);

            if (currKey.contains(entryInterval)) {
                //     |      cur      |
                //         | entry |
                // =>  |old|  new  |old|
                addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry,
                        timeline);
                addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry,
                        timeline);
                addIntervalToTimeline(entryInterval, entry, timeline);

                return true;
            } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
                //     |   cur  |
                //         |   entry   |
                // =>  |old|
                addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry,
                        timeline);
            } else if (entryInterval.getEnd().isBefore(currKey.getEnd())) {
                //            |   cur  |
                //     |   entry   |
                // =>              |old|
                addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry,
                        timeline);
            }
        } else {
            if (timeline.get(currKey).equals(entry)) {
                // This occurs when restoring segments
                timeline.remove(currKey);
            } else {
                throw new UOE("Cannot add overlapping segments [%s and %s] with the same version [%s]", currKey,
                        entryInterval, entry.getVersion());
            }
        }

        currKey = nextKey;
        retVal = true;
    }

    addIntervalToTimeline(entryInterval, entry, timeline);

    return retVal;
}

From source file:org.fenixedu.academic.api.infra.FenixAPIFromExternalServer.java

License:Open Source License

public static String getCanteen(String daySearch) {
    getInformation();/*from w  ww  . jav  a2  s. c o m*/

    String lang = I18N.getLocale().toLanguageTag();

    if (!canteenInfo.has(lang)) {
        return empty.toString();
    }
    JsonArray jsonArrayWithLang = canteenInfo.getAsJsonObject().getAsJsonArray(lang);

    DateTime dayToCompareStart;
    DateTime dayToCompareEnd;

    DateTime dateTime = DateTime.parse(daySearch, DateTimeFormat.forPattern(datePattern));
    int dayOfWeek = dateTime.getDayOfWeek();
    if (dayOfWeek != 7) {
        dayToCompareStart = dateTime.minusDays(dayOfWeek);
        dayToCompareEnd = dateTime.plusDays(7 - dayOfWeek);
    } else {
        dayToCompareStart = dateTime;
        dayToCompareEnd = dateTime.plusDays(7);
    }

    Interval validInterval = new Interval(dayToCompareStart, dayToCompareEnd);
    JsonArray jsonResult = new JsonArray();

    for (JsonElement jObj : jsonArrayWithLang) {

        DateTime dateToCompare = DateTime.parse(((JsonObject) jObj).get("day").getAsString(),
                DateTimeFormat.forPattern(datePattern));

        if (validInterval.contains(dateToCompare)) {
            jsonResult.add(jObj);
        }
    }

    return gson.toJson(jsonResult);
}

From source file:org.fenixedu.academic.domain.serviceRequests.AcademicServiceRequest.java

License:Open Source License

public static Set<AcademicServiceRequest> getAcademicServiceRequests(Person person, Integer year,
        AcademicServiceRequestSituationType situation, Interval interval) {
    Set<AcademicServiceRequest> serviceRequests = new HashSet<AcademicServiceRequest>();
    Set<AcademicProgram> programs = AcademicAccessRule
            .getProgramsAccessibleToFunction(AcademicOperationType.SERVICE_REQUESTS, person.getUser())
            .collect(Collectors.toSet());
    Collection<AcademicServiceRequest> possible = null;
    if (year != null) {
        possible = AcademicServiceRequestYear.getAcademicServiceRequests(year);
    } else {//from w  w w.  ja va  2  s .c  o m
        possible = Bennu.getInstance().getAcademicServiceRequestsSet();
    }
    for (AcademicServiceRequest request : possible) {
        if (!programs.contains(request.getAcademicProgram())) {
            continue;
        }
        if (situation != null && !request.getAcademicServiceRequestSituationType().equals(situation)) {
            continue;
        }
        if (interval != null && !interval.contains(request.getActiveSituationDate())) {
            continue;
        }
        serviceRequests.add(request);
    }
    return serviceRequests;
}

From source file:org.fenixedu.learning.api.EventsResource.java

License:Open Source License

private static Collection<ScheduleEventBean> projectEvents(Project project, ExecutionCourse executionCourse,
        Interval interval) {
    DateTime projectStart = project.getProjectBeginDateTime();
    DateTime projectEnd = project.getProjectEndDateTime();

    Set<ScheduleEventBean> events = new HashSet<>();

    if (interval.contains(projectStart)) {
        events.add(new ScheduleEventBean(executionCourse.getPrettyAcronym(),
                project.getEvaluationType().toString(), project.getPresentationName(), projectStart,
                projectStart.plusHours(1), null, executionCourse.getSiteUrl(),
                colorForType(project.getEvaluationType()), null, null));
    }/*from ww w . j a va  2  s .  c o  m*/
    if (interval.contains(projectEnd)) {
        events.add(new ScheduleEventBean(executionCourse.getPrettyAcronym(),
                project.getEvaluationType().toString(), project.getPresentationName(), projectEnd.minusHours(1),
                projectEnd, null, executionCourse.getSiteUrl(), colorForType(project.getEvaluationType()), null,
                null));
    }

    return events;
}

From source file:org.fenixedu.learning.api.EventsResource.java

License:Open Source License

private Stream<ScheduleEventBean> lessonsWithoutInstances(Lesson lesson, Interval interval) {
    return lesson.getAllLessonIntervalsWithoutInstanceDates().stream()
            .filter(i -> interval.contains(i) || i.contains(interval)).map(i -> createEventBean(lesson, i));
}

From source file:org.fenixedu.learning.api.EventsResource.java

License:Open Source License

private Stream<ScheduleEventBean> lessonWithInstances(Lesson lesson, Interval interval) {
    return lesson.getLessonInstancesSet().stream().filter(instance -> interval.contains(instance.getInterval()))
            .map(instance -> createEventBean(lesson, instance.getInterval()));
}

From source file:org.fenixedu.learning.api.EventsResource.java

License:Open Source License

private Collection<ScheduleEventBean> writtenEvaluations(Degree degree, Interval interval) {
    Set<ScheduleEventBean> events = new HashSet<>();
    allExecutionCourses(degree)//from  w  w  w  .  j a  va 2s.com
            .forEach(executionCourse -> executionCourse.getAssociatedWrittenEvaluations().stream()
                    .filter(writtenEval -> writtenEval.getBeginningDateTime() != null
                            && interval.contains(writtenEval.getBeginningDateTime()))
                    .forEach(writtenEval -> events.add(createEventBean(executionCourse, writtenEval))));
    return events;
}

From source file:org.fenixedu.learning.domain.ScheduleEventBean.java

License:Open Source License

public static Collection<ScheduleEventBean> forExecutionCourse(ExecutionCourse executionCourse,
        Interval interval) {
    List<ScheduleEventBean> events = Lists.newArrayList();
    for (CourseLoad courseLoad : executionCourse.getCourseLoadsSet()) {
        for (Shift shift : courseLoad.getShiftsSet()) {
            for (Lesson lesson : shift.getAssociatedLessonsSet()) {
                for (Interval lessonInterval : lesson.getAllLessonIntervals()) {
                    if (interval.contains(lessonInterval)) {
                        events.add(scheduleEvent(shift, lesson, lessonInterval));
                    }/*  w  w  w .ja va2  s  .  c o m*/
                }
            }
        }
    }
    return events;
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Given an event date (which may represent a date range), check to see if the 
 * eventDate contains a leap day.  Returns false if provided a null or invalid 
 * eventDate value./*from   ww  w.  jav a  2s  . c o  m*/
 * 
 * @param eventDate to check for a leap day
 * @return true if a leap day is present in the eventDate range, otherwise false.
 */
public static boolean includesLeapDay(String eventDate) {
    boolean result = false;
    if (!DateUtils.isEmpty(eventDate) && DateUtils.eventDateValid(eventDate)) {
        Interval interval = extractInterval(eventDate);
        Integer sYear = interval.getStart().getYear();
        Integer eYear = interval.getEnd().getYear();
        String startYear = Integer.toString(sYear).trim();
        String endYear = Integer.toString(eYear).trim();
        String leapDay = startYear + "-02-29";
        logger.debug(leapDay);
        if (DateUtils.eventDateValid(leapDay)) {
            if (interval.contains(DateUtils.extractInterval(leapDay))) {
                result = true;
            }
        }
        // Range spanning more than one year, check last year
        if (!endYear.equals(startYear)) {
            leapDay = endYear + "-02-29";
            logger.debug(leapDay);
            if (DateUtils.eventDateValid(leapDay)) {
                if (interval.contains(DateUtils.extractInterval(leapDay))) {
                    result = true;
                }
            }
        }
        // Ranges of more than two years, check intermediate years
        if (eYear > sYear + 1) {
            for (int testYear = sYear + 1; testYear < eYear; testYear++) {
                leapDay = Integer.toString(testYear).trim() + "-02-29";
                logger.debug(leapDay);
                if (DateUtils.eventDateValid(leapDay)) {
                    if (interval.contains(DateUtils.extractInterval(leapDay))) {
                        result = true;
                    }
                }
            }
        }
    }
    return result;
}