Example usage for java.util Collections min

List of usage examples for java.util Collections min

Introduction

In this page you can find the example usage for java.util Collections min.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) 

Source Link

Document

Returns the minimum element of the given collection, according to the order induced by the specified comparator.

Usage

From source file:org.sakaiproject.component.gradebook.GradebookServiceHibernateImpl.java

/**
  * set the droppedFromGrade attribute of each 
  * of the n highest and the n lowest scores of a 
  * student based on the assignment's category
  * @param gradeRecords//  ww  w  . ja v  a 2  s.  c o  m
  * @return void
  * 
  * NOTE: When the UI changes, this needs to be made private again
  */
public void applyDropScores(Collection<AssignmentGradeRecord> gradeRecords) {
    if (gradeRecords == null || gradeRecords.size() < 1) {
        return;
    }
    long start = System.currentTimeMillis();

    List<String> studentIds = new ArrayList<String>();
    List<Category> categories = new ArrayList<Category>();
    Map<String, List<AssignmentGradeRecord>> gradeRecordMap = new HashMap<String, List<AssignmentGradeRecord>>();
    for (AssignmentGradeRecord gradeRecord : gradeRecords) {

        if (gradeRecord == null || gradeRecord.getPointsEarned() == null) { // don't consider grades that have null pointsEarned (this occurs when a previously entered score for an assignment is removed; record stays in database) 
            continue;
        }

        // reset
        gradeRecord.setDroppedFromGrade(false);

        Assignment assignment = gradeRecord.getAssignment();
        if (assignment.getUngraded() // GradebookService.GRADE_TYPE_LETTER
                || assignment.isNotCounted() // don't consider grades that are not counted toward course grade
                || assignment.getItemType().equals(Assignment.item_type_adjustment) || assignment.isRemoved()) {
            continue;
        }
        // get all the students represented
        String studentId = gradeRecord.getStudentId();
        if (!studentIds.contains(studentId)) {
            studentIds.add(studentId);
        }
        // get all the categories represented
        Category cat = gradeRecord.getAssignment().getCategory();
        if (cat != null) {
            if (!categories.contains(cat)) {
                categories.add(cat);
            }
            List<AssignmentGradeRecord> gradeRecordsByCatAndStudent = gradeRecordMap
                    .get(studentId + cat.getId());
            if (gradeRecordsByCatAndStudent == null) {
                gradeRecordsByCatAndStudent = new ArrayList<AssignmentGradeRecord>();
                gradeRecordsByCatAndStudent.add(gradeRecord);
                gradeRecordMap.put(studentId + cat.getId(), gradeRecordsByCatAndStudent);
            } else {
                gradeRecordsByCatAndStudent.add(gradeRecord);
            }
        }
    }

    if (categories == null || categories.size() < 1) {
        return;
    }
    for (Category cat : categories) {
        Integer dropHighest = cat.getDropHighest();
        Integer dropLowest = cat.getDrop_lowest();
        Integer keepHighest = cat.getKeepHighest();
        Long catId = cat.getId();

        if ((dropHighest != null && dropHighest > 0) || (dropLowest != null && dropLowest > 0)
                || (keepHighest != null && keepHighest > 0)) {

            for (String studentId : studentIds) {
                // get the student's gradeRecords for this category
                List<AssignmentGradeRecord> gradesByCategory = new ArrayList<AssignmentGradeRecord>();
                List<AssignmentGradeRecord> gradeRecordsByCatAndStudent = gradeRecordMap
                        .get(studentId + cat.getId());
                if (gradeRecordsByCatAndStudent != null) {
                    gradesByCategory.addAll(gradeRecordsByCatAndStudent);

                    int numGrades = gradesByCategory.size();

                    if (dropHighest > 0 && numGrades > dropHighest + dropLowest) {
                        for (int i = 0; i < dropHighest; i++) {
                            AssignmentGradeRecord highest = Collections.max(gradesByCategory,
                                    AssignmentGradeRecord.numericComparator);
                            highest.setDroppedFromGrade(true);
                            gradesByCategory.remove(highest);
                            if (log.isDebugEnabled())
                                log.debug("dropHighest applied to " + highest);
                        }
                    }

                    if (keepHighest > 0 && numGrades > (gradesByCategory.size() - keepHighest)) {
                        dropLowest = gradesByCategory.size() - keepHighest;
                    }

                    if (dropLowest > 0 && numGrades > dropLowest + dropHighest) {
                        for (int i = 0; i < dropLowest; i++) {
                            AssignmentGradeRecord lowest = Collections.min(gradesByCategory,
                                    AssignmentGradeRecord.numericComparator);
                            lowest.setDroppedFromGrade(true);
                            gradesByCategory.remove(lowest);
                            if (log.isDebugEnabled())
                                log.debug("dropLowest applied to " + lowest);
                        }
                    }
                }
            }
            if (log.isDebugEnabled())
                log.debug("processed " + studentIds.size() + "students in category " + cat.getId());
        }
    }

    if (log.isDebugEnabled())
        log.debug("GradebookManager.applyDropScores took " + (System.currentTimeMillis() - start)
                + " millis to execute");
}

From source file:org.fenixedu.academic.domain.student.Registration.java

public void updateEnrolmentDate(final ExecutionYear executionYear) {

    final RegistrationDataByExecutionYear registrationData = RegistrationDataByExecutionYear
            .getOrCreateRegistrationDataByYear(this, executionYear);
    final Collection<Enrolment> executionYearEnrolments = getEnrolments(executionYear);

    if (executionYearEnrolments.isEmpty()) {
        registrationData.setEnrolmentDate(null);

    } else if (registrationData.getEnrolmentDate() == null) {

        final Enrolment firstEnrolment = Collections.min(executionYearEnrolments, new Comparator<Enrolment>() {
            @Override// w  w  w. j a  v a2s  .  c om
            public int compare(Enrolment left, Enrolment right) {
                return left.getCreationDateDateTime().compareTo(right.getCreationDateDateTime());
            }
        });

        registrationData.edit(firstEnrolment.getCreationDateDateTime().toLocalDate());
    }
}