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:org.kuali.kpme.tklm.time.rules.shiftdifferential.service.ShiftDifferentialRuleServiceImpl.java

License:Educational Community License

/**
 *
 * @param shift The shift interval - need to examine the time block to determine how many hours are eligible per block.
 * @param blockIntervals Intervals for each block present in the blocks list. Passed here to avoid re computation.
 * @param blocks The blocks we are applying hours to.
 * @param previousBlocks If present, this is the list of time blocks from a previous "day", on which the initial hours (from previous day) should be placed.
 * @param initialHours hours accumulated from a previous boundary that need to be applied here (NOT SUBJECT TO INTERVAL)
 * @param hours hours to apply/* w w  w  .  j  a  v a 2  s  .c om*/
 * @param earnCode what earn code to create time hour detail entry for.
 */
void applyPremium(Interval shift, List<Interval> blockIntervals, List<TimeBlock> blocks,
        List<TimeBlock> previousBlocks, BigDecimal initialHours, BigDecimal hours, String earnCode) {
    for (int i = 0; i < blocks.size(); i++) {
        TimeBlock b = blocks.get(i);

        // Only apply initial hours to the first timeblock.
        if (i == 0 && (initialHours.compareTo(BigDecimal.ZERO) > 0)) {
            // ONLY if they're on the same document ID, do we apply to previous,
            // otherwise we dump all on the current document.
            if (previousBlocks != null && previousBlocks.size() > 0
                    && previousBlocks.get(0).getDocumentId().equals(b.getDocumentId())) {
                for (TimeBlock pb : previousBlocks) {
                    BigDecimal lunchSub = this.negativeTimeHourDetailSum(pb); // A negative number
                    initialHours = BigDecimal.ZERO.max(initialHours.add(lunchSub)); // We don't want negative premium hours!
                    if (initialHours.compareTo(BigDecimal.ZERO) <= 0) // check here now as well, we may not have anything at all to apply.
                        break;

                    // Adjust hours on the block by the lunch sub hours, so we're not over applying.
                    BigDecimal hoursToApply = initialHours.min(pb.getHours().add(lunchSub));
                    addPremiumTimeHourDetail(pb, hoursToApply, earnCode);
                    initialHours = initialHours.subtract(hoursToApply, HrConstants.MATH_CONTEXT);
                    if (initialHours.compareTo(BigDecimal.ZERO) <= 0)
                        break;
                }
            } else {
                addPremiumTimeHourDetail(b, initialHours, earnCode);
            }
        }

        BigDecimal lunchSub = this.negativeTimeHourDetailSum(b); // A negative number
        hours = BigDecimal.ZERO.max(hours.add(lunchSub)); // We don't want negative premium hours!

        if (hours.compareTo(BigDecimal.ZERO) > 0) {
            Interval blockInterval = blockIntervals.get(i);
            Interval overlapInterval = shift.overlap(blockInterval);
            if (overlapInterval == null)
                continue;

            long overlap = overlapInterval.toDurationMillis();
            BigDecimal hoursMax = TKUtils.convertMillisToHours(overlap); // Maximum number of possible hours applicable for this time block and shift rule
            // Adjust this time block's hoursMax (below) by lunchSub to
            // make sure the time applied is the correct amount per block.
            BigDecimal hoursToApply = hours.min(hoursMax.add(lunchSub));

            addPremiumTimeHourDetail(b, hoursToApply, earnCode);
            hours = hours.subtract(hoursToApply, HrConstants.MATH_CONTEXT);
        }
    }
}

From source file:org.kuali.kpme.tklm.time.rules.shiftdifferential.shift.ShiftBlock.java

License:Educational Community License

public ShiftBlock(TimeBlock timeBlock, ShiftDifferentialRule rule, Interval shiftInterval, DateTimeZone zone) {
    this.timeBlock = timeBlock;
    this.rule = rule;
    this.timeBlockInterval = new Interval(timeBlock.getBeginDateTime().withZone(zone),
            timeBlock.getEndDateTime().withZone(zone));
    this.shiftOverlap = shiftInterval.overlap(this.timeBlockInterval);
    this.startTime = timeBlock.getBeginDateTime();
}

From source file:pt.ist.fenix.task.exportData.santanderCardGeneration.CreateAndInitializeExecutionCourses.java

License:Open Source License

private Interval intersect(final OccupationPeriod occupationPeriod, final Interval i) {
    final Interval pi = occupationPeriod.getPeriodInterval();
    final Interval overlap = pi.overlap(i);
    return overlap == null && occupationPeriod.getNextPeriod() != null
            ? intersect(occupationPeriod.getNextPeriod(), i)
            : overlap;//from  www .  j a va2 s.co m
}

From source file:pt.ist.fenixedu.contracts.tasks.UpdateTeacherAuthorizations.java

License:Open Source License

private Department getDominantDepartment(Person person, ExecutionSemester semester) {
    SortedSet<EmployeeContract> contracts = new TreeSet<EmployeeContract>(new Comparator<EmployeeContract>() {
        @Override//from   ww  w . j av a 2  s.  c  o m
        public int compare(EmployeeContract ec1, EmployeeContract ec2) {
            int compare = ec1.getBeginDate().compareTo(ec2.getBeginDate());
            return compare == 0 ? ec1.getExternalId().compareTo(ec2.getExternalId()) : compare;
        }
    });
    Interval semesterInterval = semester.getAcademicInterval().toInterval();
    contracts.addAll(((Collection<EmployeeContract>) person
            .getParentAccountabilities(AccountabilityTypeEnum.WORKING_CONTRACT, EmployeeContract.class))
                    .stream()
                    .filter(ec -> ec.belongsToPeriod(semesterInterval.getStart().toYearMonthDay(),
                            semesterInterval.getEnd().toYearMonthDay()))
                    .filter(Objects::nonNull).collect(Collectors.toSet()));

    Department firstDepartmentUnit = null;
    for (EmployeeContract employeeContract : contracts) {
        Department employeeDepartmentUnit = getEmployeeDepartmentUnit(employeeContract.getUnit());
        if (employeeDepartmentUnit != null) {
            Interval contractInterval = new Interval(
                    employeeContract.getBeginDate().toLocalDate().toDateTimeAtStartOfDay(),
                    employeeContract.getEndDate() == null ? new DateTime(Long.MAX_VALUE)
                            : employeeContract.getEndDate().toLocalDate().toDateTimeAtStartOfDay()
                                    .plusMillis(1));
            Interval overlap = semesterInterval.overlap(contractInterval);
            int days = overlap.toPeriod(PeriodType.days()).getDays() + 1;
            if (days > minimumDaysForActivity) {
                return employeeDepartmentUnit;
            }
            if (firstDepartmentUnit == null) {
                firstDepartmentUnit = employeeDepartmentUnit;
            }
        }
    }
    return firstDepartmentUnit;
}

From source file:pt.ist.fenixedu.teacher.domain.TeacherCredits.java

License:Open Source License

public static double calculateServiceExemptionCredits(Teacher teacher, ExecutionSemester executionSemester) {
    Set<PersonContractSituation> personProfessionalExemptions = PersonContractSituation
            .getValidTeacherServiceExemptions(teacher, executionSemester);
    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 = calculateMandatoryLessonHours(teacher, executionSemester);
    Double maxSneHours = mandatoryLessonHours;
    TeacherService teacherService = TeacherService.getTeacherServiceByExecutionPeriod(teacher,
            executionSemester);/* w w w.  ja v  a 2 s.com*/
    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(teacher, 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:pt.ist.fenixedu.teacher.domain.TeacherCredits.java

License:Open Source License

private static double calculateLessonsIntervalAndExemptionOverlapPercentage(Interval lessonsInterval,
        Interval exemptionInterval) {//ww  w.j a  va2  s  .  com
    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;
}