Example usage for org.apache.commons.collections.comparators ComparatorChain addComparator

List of usage examples for org.apache.commons.collections.comparators ComparatorChain addComparator

Introduction

In this page you can find the example usage for org.apache.commons.collections.comparators ComparatorChain addComparator.

Prototype

public void addComparator(Comparator comparator, boolean reverse) 

Source Link

Document

Add a Comparator to the end of the chain using the given sort order

Usage

From source file:amgen.ri.test.TestComparatorChain.java

public Comparator makeComparator() {
    ComparatorChain chain = new ComparatorChain(new ColumnComparator(0));
    chain.addComparator(new ColumnComparator(1), true);
    return chain;

}

From source file:net.sourceforge.fenixedu.presentationTier.backBeans.departmentAdmOffice.ManagementGroupsBackingBean.java

public List<Employee> getEmployees() {
    List<Employee> result = (getDepartment() != null)
            ? new ArrayList<Employee>(getDepartment().getAllCurrentActiveWorkingEmployees())
            : null;//from w  w  w.j a  va 2 s.  co m

    if (result != null) {
        ComparatorChain chainComparator = new ComparatorChain();
        chainComparator.addComparator(new BeanComparator("person.name"), false);
        chainComparator.addComparator(new BeanComparator("employeeNumber"), false);
        Collections.sort(result, chainComparator);
    }

    return result;
}

From source file:edu.psu.citeseerx.myciteseer.web.groups.ViewGroupsController.java

private ComparatorChain getGroupComparator(String sort, String oType) {

    // Determine the order (will apply to the first comparator).
    boolean mainOrder = false; // true = asc; false = desc
    if (oType == null || "asc".compareToIgnoreCase(oType) != 0) {
        mainOrder = true;/*from   w  w  w.  j  a v  a2  s. c  om*/
    }

    // Order By?
    ComparatorChain comparator = new ComparatorChain();
    comparator.addComparator(Group.nameComparator, mainOrder);
    return comparator;
}

From source file:edu.psu.citeseerx.myciteseer.web.groups.ViewGroupMembersController.java

private ComparatorChain getUserGroupComparator(String sort, String oType) {

    // Determine the order (will apply to the first comparator).
    boolean mainOrder = false; // true = asc; false = desc
    if (oType == null || "asc".compareToIgnoreCase(oType) != 0) {
        mainOrder = true;// ww  w.j a v  a 2s . c  o m
    }

    // Order By?
    ComparatorChain comparator = new ComparatorChain();
    comparator.addComparator(GroupMember.memberComparator, mainOrder);
    return comparator;
}

From source file:net.sourceforge.fenixedu.presentationTier.backBeans.scientificCouncil.curricularPlans.CurricularPlansMembersManagementBackingBean.java

public List<SelectItem> getDepartmentEmployees() {
    List<SelectItem> result = new ArrayList<SelectItem>();

    Department department = getDepartment();
    if (department != null) {
        List<Employee> employees = new ArrayList<Employee>(
                getDepartment().getAllCurrentActiveWorkingEmployees());
        ComparatorChain chainComparator = new ComparatorChain();
        chainComparator.addComparator(new BeanComparator("person.name"), false);
        chainComparator.addComparator(new BeanComparator("employeeNumber"), false);
        Collections.sort(employees, chainComparator);

        Group curricularPlanMembersGroup = this.getDegreeCurricularPlan().getCurricularPlanMembersGroup();
        for (Employee departmentEmployee : employees) {
            Person person = departmentEmployee.getPerson();
            if (curricularPlanMembersGroup == null || !curricularPlanMembersGroup.isMember(person.getUser())) {
                result.add(new SelectItem(person.getExternalId(),
                        person.getName() + " (" + person.getUsername() + ")"));
            }/*  ww w.j a v  a2  s  .  c o m*/
        }
    }

    return result;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.publico.ShowCourseSiteAction.java

public ActionForward showCurricularCourseSite(ActionMapping mapping, ActionForm actionForm,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    ActionErrors errors = new ActionErrors();

    // Integer executionPeriodOId = getFromRequest("executionPeriodOID",
    // request);/*from   ww  w . j  a  va2  s.  co m*/

    DynaActionForm indexForm = (DynaActionForm) actionForm;
    String degreeId = getFromRequest("degreeID", request);
    request.setAttribute("degreeID", degreeId);

    String index = getFromRequest("index", request);
    request.setAttribute("index", index);

    String executionDegreeId = getFromRequest("executionDegreeID", request);
    request.setAttribute("executionDegreeID", executionDegreeId);

    String degreeCurricularPlanId = getFromRequest("degreeCurricularPlanID", request);
    request.setAttribute("degreeCurricularPlanID", degreeCurricularPlanId);

    String curricularCourseId = getFromRequest("curricularCourseID", request);
    request.setAttribute("curricularCourseID", curricularCourseId);

    Boolean inEnglish = getFromRequestBoolean("inEnglish", request);
    request.setAttribute("inEnglish", inEnglish);

    Integer curricularYear = (Integer) indexForm.get("curYear");

    indexForm.set("indice", indexForm.get("indice"));
    indexForm.set("curYear", curricularYear);

    InfoCurriculum infoCurriculum = null;
    try {
        infoCurriculum = ReadCurriculumByCurricularCourseCode.run(curricularCourseId);
    } catch (NonExistingServiceException e) {
        errors.add("chosenCurricularCourse", new ActionError("error.coordinator.chosenCurricularCourse"));
    } catch (FenixServiceException e) {
        if (e.getMessage().equals("nullCurricularCourse")) {
            errors.add("nullCode", new ActionError("error.coordinator.noCurricularCourse"));
        } else {
            throw new FenixActionException(e);
        }
    }
    if (infoCurriculum == null) {
        errors.add("noCurriculum", new ActionError("error.coordinator.noCurriculum"));
    }
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        return (new ActionForward(mapping.getInput()));
    }

    // order list of execution courses by name
    if (infoCurriculum.getInfoCurricularCourse() != null
            && infoCurriculum.getInfoCurricularCourse().getInfoAssociatedExecutionCourses() != null) {
        ComparatorChain comparatorChain = new ComparatorChain();
        comparatorChain.addComparator(new BeanComparator("infoExecutionPeriod.infoExecutionYear.beginDate"),
                true);
        comparatorChain.addComparator(new BeanComparator("infoExecutionPeriod.beginDate"), true);

        Collections.sort(infoCurriculum.getInfoCurricularCourse().getInfoAssociatedExecutionCourses(),
                comparatorChain);
    }

    // order list by year, semester
    if (infoCurriculum.getInfoCurricularCourse() != null
            && infoCurriculum.getInfoCurricularCourse().getCurricularCourseExecutionScope() != null) {
        ComparatorChain comparatorChain = new ComparatorChain();
        comparatorChain.addComparator(new BeanComparator("infoCurricularSemester.infoCurricularYear.year"));
        comparatorChain.addComparator(new BeanComparator("infoCurricularSemester.semester"));
        comparatorChain.addComparator(new BeanComparator("infoCurricularCourse.name"));
        Collections.sort(infoCurriculum.getInfoCurricularCourse().getInfoScopes(), comparatorChain);
    }
    infoCurriculum.prepareEnglishPresentation(getLocale(request));
    request.setAttribute("infoCurriculum", infoCurriculum);

    return mapping.findForward("showCurricularCourseSite");

}

From source file:com.cyclopsgroup.waterview.CollectionLargeList.java

/**
 * Overwrite or implement method iterate()
 *
 * @see com.cyclopsgroup.waterview.IteratorLargeList#iterate(int, int, com.cyclopsgroup.waterview.LargeList.Sorting[])
 *//*from  w  ww.  j a  v  a  2  s . c o m*/
public Iterator iterate(int startPosition, int maxAmount, Sorting[] sortings) throws Exception {
    Collection sortedResult = collection;
    if (sortings != null && sortings.length > 0) {
        ComparatorChain chain = new ComparatorChain();
        for (int i = 0; i < sortings.length; i++) {
            Sorting sorting = sortings[i];
            if (sorting.isDescending()) {
                chain.addComparator(new BeanPropertyComparator(sorting.getName()), true);
            } else {
                chain.addComparator(new BeanPropertyComparator(sorting.getName()));
            }
        }
        chain.addComparator(HashCodeComparator.INSTANCE);
        sortedResult = new TreeSet(chain);
        sortedResult.addAll(collection);
    }
    Iterator it = sortedResult.iterator();
    if (startPosition > 0) {
        for (int i = 0; i < startPosition; i++) {
            it.next();
        }
    }
    if (maxAmount == UNLIMITED_MAX_AMOUNT) {
        return it;
    }
    return new FixedSizeIterator(it, maxAmount);
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.academicAdministration.executionCourseManagement.InsertExecutionCourseDispatchAction.java

@EntryPoint
public ActionForward prepareInsertExecutionCourse(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {

    List infoExecutionPeriods = null;
    infoExecutionPeriods = ReadExecutionPeriods.run();

    if (infoExecutionPeriods != null && !infoExecutionPeriods.isEmpty()) {
        // exclude closed execution periods
        infoExecutionPeriods = (List) CollectionUtils.select(infoExecutionPeriods, new Predicate() {
            @Override//  w w w  . j av a2 s  .  c  o m
            public boolean evaluate(Object input) {
                InfoExecutionPeriod infoExecutionPeriod = (InfoExecutionPeriod) input;
                if (!infoExecutionPeriod.getState().equals(PeriodState.CLOSED)) {
                    return true;
                }
                return false;
            }
        });

        ComparatorChain comparator = new ComparatorChain();
        comparator.addComparator(new BeanComparator("infoExecutionYear.year"), true);
        comparator.addComparator(new BeanComparator("name"), true);
        Collections.sort(infoExecutionPeriods, comparator);

        List<LabelValueBean> executionPeriodLabels = new ArrayList<LabelValueBean>();
        CollectionUtils.collect(infoExecutionPeriods, new Transformer() {
            @Override
            public Object transform(Object arg0) {
                InfoExecutionPeriod infoExecutionPeriod = (InfoExecutionPeriod) arg0;

                LabelValueBean executionPeriod = new LabelValueBean(
                        infoExecutionPeriod.getName() + " - "
                                + infoExecutionPeriod.getInfoExecutionYear().getYear(),
                        infoExecutionPeriod.getExternalId().toString());
                return executionPeriod;
            }
        }, executionPeriodLabels);

        request.setAttribute(PresentationConstants.LIST_EXECUTION_PERIODS, executionPeriodLabels);

        List<LabelValueBean> entryPhases = new ArrayList<LabelValueBean>();
        for (EntryPhase entryPhase : EntryPhase.values()) {
            LabelValueBean labelValueBean = new LabelValueBean(entryPhase.getLocalizedName(),
                    entryPhase.getName());
            entryPhases.add(labelValueBean);
        }
        request.setAttribute("entryPhases", entryPhases);

    }

    return mapping.findForward("insertExecutionCourse");
}

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

public static List<ExecutionDegree> getAllCoordinatedByTeacher(Person person) {
    List<ExecutionDegree> result = new ArrayList<ExecutionDegree>();

    if (person == null) {
        return result;
    }// w w w . j a  v a 2s . c om

    for (Coordinator coordinator : person.getCoordinatorsSet()) {
        result.add(coordinator.getExecutionDegree());
    }

    Comparator<ExecutionDegree> degreNameComparator = new Comparator<ExecutionDegree>() {

        @Override
        public int compare(ExecutionDegree o1, ExecutionDegree o2) {
            String name1 = o1.getDegreeCurricularPlan().getDegree().getName();
            String name2 = o2.getDegreeCurricularPlan().getDegree().getName();

            return String.CASE_INSENSITIVE_ORDER.compare(name1, name2);
        }

    };

    Comparator<ExecutionDegree> yearComparator = new Comparator<ExecutionDegree>() {

        @Override
        public int compare(ExecutionDegree o1, ExecutionDegree o2) {
            String year1 = o1.getExecutionYear().getYear();
            String year2 = o2.getExecutionYear().getYear();

            return String.CASE_INSENSITIVE_ORDER.compare(year1, year2);
        }

    };

    // sort by degreeCurricularPlan.degree.nome ascending,
    // executionYear.year descending
    ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(degreNameComparator, false);
    comparatorChain.addComparator(yearComparator, true);

    Collections.sort(result, comparatorChain);

    return result;
}

From source file:net.jazdw.rql.parser.listfilter.ListFilter.java

@SuppressWarnings("unchecked")
@Override//from   w w w.  j a  v a2 s. c  o  m
public List<T> visit(ASTNode node, List<T> list) {
    switch (node.getName()) {
    case "and":
        for (Object obj : node) {
            if (obj instanceof ASTNode) {
                list = ((ASTNode) obj).accept(this, list);
            } else {
                throw new UnsupportedOperationException("Encountered a non-ASTNode argument in AND statement");
            }
        }
        return list;
    case "or":
        Set<T> set = new LinkedHashSet<T>();
        for (Object obj : node) {
            if (obj instanceof ASTNode) {
                set.addAll(((ASTNode) obj).accept(this, list));
            } else {
                throw new UnsupportedOperationException("Encountered a non-ASTNode argument in OR statement");
            }
        }
        return new ArrayList<>(set);
    case "eq":
    case "gt":
    case "ge":
    case "lt":
    case "le":
    case "ne":
        String propName = (String) node.getArgument(0);
        Object test = node.getArgumentsSize() > 1 ? node.getArgument(1) : null;

        List<T> result = new ArrayList<>();

        for (T item : list) {
            Object property = getProperty(item, propName);

            Comparable<Object> comparableProperty;
            if (property instanceof Comparable) {
                comparableProperty = (Comparable<Object>) property;
            } else {
                throw new UnsupportedOperationException(
                        String.format("Property '%s' is not comparable", propName));
            }

            int comparisonValue;
            try {
                comparisonValue = comparableProperty.compareTo(test);
            } catch (ClassCastException e) {
                throw new UnsupportedOperationException(
                        String.format("Couldn't compare '%s' to '%s'", property.toString(), test.toString()));
            }

            if (checkComparisonValue(node.getName(), comparisonValue)) {
                result.add(item);
            }
        }
        return result;
    case "like":
    case "match":
        propName = (String) node.getArgument(0);
        String matchString = (String) node.getArgument(1);
        Pattern matchPattern = Pattern.compile(matchString.replace("*", ".*"),
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

        result = new ArrayList<>();

        for (T item : list) {
            Object property = getProperty(item, propName);

            String stringProperty;
            if (property instanceof String) {
                stringProperty = (String) property;
            } else {
                throw new UnsupportedOperationException(
                        String.format("Property '%s' is not a string", propName));
            }

            if (matchPattern.matcher(stringProperty).matches()) {
                result.add(item);
            }
        }
        return result;
    case "limit":
        int limit = (int) node.getArgument(0);
        int offset = node.getArgumentsSize() > 1 ? (int) node.getArgument(1) : 0;

        if (offset > list.size() - 1) {
            return Collections.emptyList();
        }

        int toIndex = offset + limit;
        if (toIndex > list.size()) {
            toIndex = list.size();
        }

        return list.subList(offset, toIndex);
    case "sort":
        ComparatorChain cc = new ComparatorChain();
        for (Object obj : node) {
            String sortOption = (String) obj;
            boolean desc = sortOption.startsWith("-");
            cc.addComparator(new BeanComparator<T>(sortOption.substring(1)), desc);
        }
        // copy the list as we are modifying it
        list = new ArrayList<>(list);
        Collections.sort(list, cc);
        return list;
    default:
        throw new UnsupportedOperationException(
                String.format("Encountered unknown operator '%s'", node.getName()));
    }
}