Example usage for org.joda.time Interval overlap

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

Introduction

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

Prototype

public Interval overlap(ReadableInterval interval) 

Source Link

Document

Gets the overlap between this interval and another interval.

Usage

From source file:com.almende.eve.agent.MeetingAgent.java

License:Apache License

/**
 * Calculate the average preference for given interval.
 * The method aggregates over all stored preferences
 * Default preference is 0./*from  w ww.  j  a v  a  2s  . c  o  m*/
 * 
 * @param preferredIntervals
 *            list with intervals ordered by start
 * @param test
 *            test interval
 * @return preference
 */
private double calculatePreference(final List<Weight> preferredIntervals, final Interval test) {
    double preference = 0;

    for (final Weight interval : preferredIntervals) {
        final Interval overlap = test.overlap(interval.getInterval());
        if (overlap != null) {
            final Double weight = interval.getWeight();
            if (weight != null) {
                final double durationCheck = test.toDurationMillis();
                final double durationOverlap = overlap.toDurationMillis();
                final double avgWeight = (durationOverlap / durationCheck) * weight;
                preference += avgWeight;
            }
        }

        if (interval.getStart().isAfter(test.getEnd())) {
            // as the list is ordered, we can exit as soon as we have an
            // interval which starts after the wanted interval.
            break;
        }
    }

    return preference;
}

From source file:com.marand.thinkmed.medications.business.impl.DefaultMedicationsBo.java

License:Open Source License

@Override
public boolean isTherapyActive(final List<String> daysOfWeek, final Integer dosingDaysFrequency,
        final Interval therapyInterval, final DateTime when) {
    if (therapyInterval.overlap(Intervals.wholeDay(when)) == null) {
        return false;
    }//ww  w .  ja  v  a 2  s.  co  m
    if (daysOfWeek != null) {
        boolean activeDay = false;
        final String searchDay = MedicationsEhrUtils.dayOfWeekToEhrEnum(when).name();
        for (final String day : daysOfWeek) {
            if (day.equals(searchDay)) {
                activeDay = true;
            }
        }
        if (!activeDay) {
            return false;
        }
    }
    if (dosingDaysFrequency != null) {
        final int daysFromStart = Days
                .daysBetween(therapyInterval.getStart().withTimeAtStartOfDay(), when.withTimeAtStartOfDay())
                .getDays();
        if (daysFromStart % dosingDaysFrequency != 0) {
            return false;
        }
    }
    return true;
}

From source file:com.sloca.entity.Group.java

/**
 * Compare the overlap time/* www.  j ava2 s  . com*/
 * @param other the group to compare
 * @return -1 if not overlap, otherwise return fullCount
 */
public int compareOverlap(Group other) {
    int fullCount = 0;
    ArrayList<Interval> intervalList = new ArrayList<Interval>();
    for (String s : getLocOverlapMap().keySet()) {
        ArrayList<Interval> firstList = getLocOverlapMap().get(s);
        ArrayList<Interval> secondList = other.getLocOverlapMap().get(s);
        for (Interval first : firstList) {
            for (Interval second : secondList) {
                Interval overlap = first.overlap(second);
                if (overlap != null) {
                    Seconds seconds = Seconds.secondsIn(overlap);
                    int secs = seconds.getSeconds();
                    fullCount = fullCount + secs;
                    intervalList.add(overlap);
                }
            }
        }
    }
    if (fullCount >= 720) {
        return fullCount;
    } else {
        return -1;
    }
}

From source file:com.stagecents.common.EffectiveDateInterval.java

License:Open Source License

/**
 * Returns the overlap between the effective date range represented by this
 * EffectivedateInterval object and the given interval.
 * /*from   w ww.j  a  v a  2s . c  o m*/
 * @param arg The interval to test for overlap.
 * @return The overlapping interval.
 */
public Interval getOverlap(Interval arg) {
    Interval period = new Interval(startDate, endDate);
    return period.overlap(arg);
}

From source file:com.yahoo.bard.webservice.util.SimplifiedIntervalList.java

License:Apache License

/**
 * Return the intersection of all subintervals in two interval lists.
 *
 * @param that  A simplified list of intervals
 *
 * @return A new simplified interval list whose intervals are all subintervals of this and that.
 *///  ww  w .  j a  v a2s. c om
public SimplifiedIntervalList intersect(SimplifiedIntervalList that) {
    Iterator<Interval> theseIntervals = this.iterator();
    Iterator<Interval> thoseIntervals = that.iterator();
    Interval thisCurrent = getNextIfAvailable.apply(theseIntervals);
    Interval thatCurrent = getNextIfAvailable.apply(thoseIntervals);
    List<Interval> collected = new ArrayList<>();

    while (thisCurrent != null && thatCurrent != null) {
        if (thisCurrent.overlaps(thatCurrent)) {
            collected.add(thisCurrent.overlap(thatCurrent));
        }
        if (thisCurrent.isBefore(thatCurrent.getEnd())) {
            thisCurrent = getNextIfAvailable.apply(theseIntervals);
        } else {
            thatCurrent = getNextIfAvailable.apply(thoseIntervals);
        }
    }
    return new SimplifiedIntervalList(collected);
}

From source file:de.hpi.bpmn2_0.replay.TimeUtilities.java

License:Open Source License

public static ArrayList<Interval> divide(Interval v1, Interval v2) {
    ArrayList<Interval> divide = new ArrayList();
    Interval overlap = v1.overlap(v2);

    if (overlap != null) {
        long overlapStart = overlap.getStartMillis();
        long overlapEnd = overlap.getEndMillis();

        long v1Start = v1.getStartMillis();
        long v1End = v1.getEndMillis();

        long v2Start = v2.getStartMillis();
        long v2End = v2.getEndMillis();

        long minStart = Math.min(v1Start, v2Start);
        long maxEnd = Math.max(v1End, v2End);

        divide.add(new Interval(minStart, overlapStart));
        divide.add(overlap);/* w  w w. ja va 2  s  . co  m*/
        divide.add(new Interval(overlapEnd, maxEnd));
    }
    return divide;
}

From source file:de.iteratec.iteraplan.model.RuntimePeriod.java

License:Open Source License

/**
 * @return Returns {@code true}, if this period is completely contained in the given period
 *         (inclusive at the start and end). If the given period is {@code null} (i.e.
 *         {@link BaseDateUtils#MIN_DATE} and {@link BaseDateUtils#MAX_DATE} is assumed), the test returns
 *         {@code true}./*from  w w  w.  j  a  va 2s  . c om*/
 */
public boolean withinPeriod(RuntimePeriod period) {

    if (period == null) {
        return true;
    }

    Interval other = DateUtils.asInterval(period.getStart(), period.getEnd());
    Interval thiz = asInterval();

    Interval overlap = thiz.overlap(other);

    if (overlap == null) {
        return false;
    }

    return overlap.toDuration().equals(thiz.toDuration()) ? true : false;
}

From source file:julian.lylly.model.Task.java

private Duration computeOverlap(Interval x, Interval y) {
    if (x.overlaps(y)) {
        return x.overlap(y).toDuration();
    } else {/*from   w w  w  .j av a2s .c o  m*/
        return Duration.ZERO;
    }
}

From source file:net.sourceforge.fenixedu.domain.Teacher.java

License:Open Source License

public double getServiceExemptionCredits(ExecutionSemester executionSemester) {
    Set<PersonContractSituation> personProfessionalExemptions = getValidTeacherServiceExemptions(
            executionSemester);/*from   w  w  w.j a v a  2 s  . co  m*/
    Interval semesterInterval = new Interval(
            executionSemester.getBeginDateYearMonthDay().toLocalDate().toDateTimeAtStartOfDay(),
            executionSemester.getEndDateYearMonthDay().toLocalDate().toDateTimeAtStartOfDay());
    int lessonsDays = semesterInterval.toPeriod(PeriodType.days()).getDays();

    List<Interval> notYetOverlapedIntervals = new ArrayList<Interval>();
    List<Interval> newIntervals = new ArrayList<Interval>();
    notYetOverlapedIntervals.add(semesterInterval);

    Double mandatoryLessonHours = getMandatoryLessonHours(executionSemester);
    Double maxSneHours = mandatoryLessonHours;
    TeacherService teacherService = getTeacherServiceByExecutionPeriod(executionSemester);
    if (teacherService != null && teacherService.getReductionService() != null) {
        maxSneHours = Math.max(0,
                (mandatoryLessonHours - teacherService.getReductionServiceCredits().doubleValue()));
    }

    for (PersonContractSituation personContractSituation : personProfessionalExemptions) {
        LocalDate exemptionEnd = personContractSituation.getServiceExemptionEndDate() == null
                ? semesterInterval.getEnd().toLocalDate()
                : personContractSituation.getServiceExemptionEndDate();

        Interval exemptionInterval = new Interval(
                personContractSituation.getBeginDate().toDateTimeAtStartOfDay(),
                exemptionEnd.toDateTimeAtStartOfDay());

        PersonProfessionalExemption personProfessionalExemption = personContractSituation
                .getPersonProfessionalExemption();
        if (personContractSituation.countForCredits(semesterInterval)) {
            if (personProfessionalExemption != null) {
                exemptionEnd = personProfessionalExemption.getEndDate() == null
                        ? semesterInterval.getEnd().toLocalDate()
                        : personProfessionalExemption.getEndDate();
                exemptionInterval = new Interval(
                        personProfessionalExemption.getBeginDate().toDateTimeAtStartOfDay(),
                        exemptionEnd.toDateTimeAtStartOfDay());
                if (personProfessionalExemption.getIsSabaticalOrEquivalent()) {
                    if (isSabbaticalForSemester(exemptionInterval, semesterInterval)) {
                        return maxSneHours;
                    } else {
                        continue;
                    }
                }
            }
            for (Interval notYetOverlapedInterval : notYetOverlapedIntervals) {
                Interval overlapInterval = exemptionInterval.overlap(notYetOverlapedInterval);
                if (overlapInterval != null) {
                    newIntervals.addAll(getNotOverlapedIntervals(overlapInterval, notYetOverlapedInterval));
                } else {
                    newIntervals.add(notYetOverlapedInterval);
                }
            }
            notYetOverlapedIntervals.clear();
            notYetOverlapedIntervals.addAll(newIntervals);
            newIntervals.clear();
        }
    }

    int notOverlapedDays = 0;
    for (Interval interval : notYetOverlapedIntervals) {
        notOverlapedDays += interval.toPeriod(PeriodType.days()).getDays();
    }
    int overlapedDays = lessonsDays - notOverlapedDays;
    Double overlapedPercentage = round(Double.valueOf(overlapedDays) / Double.valueOf(lessonsDays));
    return round(overlapedPercentage * maxSneHours);

}

From source file:net.sourceforge.fenixedu.domain.Teacher.java

License:Open Source License

private double calculateLessonsIntervalAndExemptionOverlapPercentage(Interval lessonsInterval,
        Interval exemptionInterval) {//from  ww  w . ja v a 2  s  .  c  om
    if (lessonsInterval != null) {
        Interval overlapInterval = lessonsInterval.overlap(exemptionInterval);
        if (overlapInterval != null) {
            int intersectedDays = overlapInterval.toPeriod(PeriodType.days()).getDays();
            return round(Double.valueOf(intersectedDays)
                    / Double.valueOf(lessonsInterval.toPeriod(PeriodType.days()).getDays()));
        }
    }
    return 0.0;
}