Example usage for org.apache.commons.collections CollectionUtils select

List of usage examples for org.apache.commons.collections CollectionUtils select

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils select.

Prototype

public static Collection select(Collection inputCollection, Predicate predicate) 

Source Link

Document

Selects all elements from input collection which match the given predicate into an output collection.

Usage

From source file:com.nwea.samples.apachecommons.collections.SampleCollectionUtils.java

@SuppressWarnings("unchecked")
public static Collection<Student> findStudentsBySex(Collection<Student> students, final String sex) {
    return CollectionUtils.select(students, new Predicate() {
        public boolean evaluate(java.lang.Object s) {
            Student student = (Student) s;
            return student.getSex().equals(sex);
        }//from  ww w .j a v  a2s . com
    });
}

From source file:find.FindFile.java

private static Collection find(Collection files, final String name) {
    return CollectionUtils.select(files, new Predicate() {
        public boolean evaluate(Object o) {
            return ((File) o).getName().indexOf(name) != -1;
        }/* w w w. ja  va 2s  .  c  om*/
    });
}

From source file:net.sourceforge.fenixedu.presentationTier.renderers.providers.choiceType.replacement.multiple.FunctionProvider.java

@Override
public Object provide(Object source, Object currentValue) {
    return CollectionUtils.select(Bennu.getInstance().getAccountabilityTypesSet(), new Predicate() {

        @Override/* w  w  w .j av  a  2  s .c o m*/
        public boolean evaluate(Object arg0) {
            return isFunction((AccountabilityType) arg0);
        }

    });
}

From source file:com.ibm.amc.data.filter.QueryFilterEngine.java

@SuppressWarnings("unchecked")
public static <T> List<T> filter(List<T> objects, Set<Map.Entry<String, List<String>>> queryParams) {
    if (logger.isEntryEnabled())
        logger.entry("filter", objects, queryParams);

    /* use a HashSet to remove duplicates */
    HashSet<T> filteredObjects = new HashSet<T>();

    QueryFilter filter = QueryFilterFactory.build(queryParams);

    if (filter.getOperator().equals(Operator.AND)) {
        if (logger.isDebugEnabled())
            logger.debug("filter", "Filter operator: AND");
        for (Predicate predicate : filter.getClauses()) {
            CollectionUtils.filter(objects, predicate);
        }/* w  ww  .  j  a va2s.  c  o  m*/

        /* need to muck about to get compatible types */
        ArrayList<T> temp;
        if (objects instanceof ArrayList) {
            temp = (ArrayList<T>) objects;
        } else {
            temp = new ArrayList<T>(objects);
        }

        filteredObjects.addAll(temp);
    } else {
        if (logger.isDebugEnabled())
            logger.debug("filter", "Filter operator: OR");
        for (Predicate predicate : filter.getClauses()) {
            filteredObjects.addAll(CollectionUtils.select(objects, predicate));
        }
    }

    if (logger.isEntryEnabled())
        logger.exit("filter", filteredObjects);
    return new ArrayList<T>(filteredObjects);
}

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

@Override
public QueueJobResult execute() throws Exception {
    final ExecutionSemester semester = (ExecutionSemester) ExecutionSemester
            .getExecutionInterval(getExecutionInterval());

    Collection<Registration> highPerformants = CollectionUtils.select(Bennu.getInstance().getRegistrationsSet(),
            new Predicate() {
                @Override//from  ww  w  .j a  v a 2  s .  c  o m
                public boolean evaluate(Object element) {
                    Registration registration = (Registration) element;
                    if (registration.hasActiveLastState(semester)) {
                        Collection<Enrolment> enrols = registration.getEnrolments(semester);
                        if (!enrols.isEmpty()) {
                            for (Enrolment enrol : enrols) {
                                if (!enrol.isApproved()) {
                                    return false;
                                }
                            }
                            return true;
                        }
                    }
                    return false;
                }
            });

    SheetData<Registration> data = new SheetData<Registration>(highPerformants) {
        @Override
        protected void makeLine(Registration item) {
            addCell("istId", item.getPerson().getIstUsername());
            addCell("Nome", item.getPerson().getName());
            double totalEcts = 0;
            Collection<Enrolment> enrols = item.getEnrolments(semester);
            for (Enrolment enrolment : enrols) {
                totalEcts += enrolment.getEctsCredits();
            }
            addCell("Crditos Inscritos", totalEcts);
            addCell("Curso", item.getDegree().getNameFor(semester));
            CycleType cycleType = item.getCycleType(semester.getExecutionYear());
            addCell("Ciclo", cycleType != null ? cycleType.getDescription() : null);
            addCell("Email", item.getPerson().getEmailForSendingEmails());
            Tutorship activeTutorship = item.getActiveTutorship();
            addCell("Tutor", activeTutorship != null ? activeTutorship.getPerson().getName() : null);
            addCell("Email Tutor",
                    activeTutorship != null ? activeTutorship.getPerson().getEmailForSendingEmails() : null);
        }
    };

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    new SpreadsheetBuilder().addSheet("elevado rendimento", data).build(WorkbookExportFormat.EXCEL, stream);

    QueueJobResult result = new QueueJobResult();
    result.setContentType("application/xls");
    result.setContent(stream.toByteArray());
    return result;
}

From source file:com.collective.celos.SerialSchedulingStrategy.java

@Override
public List<SlotState> getSchedulingCandidates(List<SlotState> states) {

    int slotsRunning = CollectionUtils.countMatches(states, RUNNING_PREDICATE);
    if (slotsRunning >= concurrencyLevel) {
        return Collections.emptyList();
    }/*from  www.j  a  v a2  s  .c  o m*/

    @SuppressWarnings("unchecked")
    Collection<SlotState> candidates = CollectionUtils.select(states, CANDIDATE_PREDICATE);

    if (!candidates.isEmpty()) {
        int elemsToGet = Math.min(candidates.size(), concurrencyLevel - slotsRunning);
        return Lists.newArrayList(candidates).subList(0, elemsToGet);
    }

    return Collections.emptyList();
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.coordinator.degreeCurricularPlanManagement.ReadCurrentCurriculumByCurricularCourseCode.java

@Atomic
public static InfoCurriculum run(String executionDegreeCode, String curricularCourseCode)
        throws FenixServiceException {
    check(RolePredicates.COORDINATOR_PREDICATE);

    if (curricularCourseCode == null) {
        throw new FenixServiceException("nullCurricularCourse");
    }//from w  ww.j a v  a2s.c o m

    CurricularCourse curricularCourse = (CurricularCourse) FenixFramework.getDomainObject(curricularCourseCode);
    if (curricularCourse == null) {
        throw new NonExistingServiceException();
    }
    // selects active curricular course scopes
    List<CurricularCourseScope> activeCurricularCourseScopes = curricularCourse.getActiveScopes();

    activeCurricularCourseScopes = (List) CollectionUtils.select(activeCurricularCourseScopes, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            CurricularCourseScope curricularCourseScope = (CurricularCourseScope) arg0;
            if (curricularCourseScope.isActive().booleanValue()) {
                return true;
            }
            return false;
        }
    });

    final ExecutionSemester executionSemester = ExecutionSemester.readActualExecutionSemester();

    List<ExecutionCourse> associatedExecutionCourses = new ArrayList<ExecutionCourse>();
    Collection<ExecutionCourse> executionCourses = curricularCourse.getAssociatedExecutionCoursesSet();
    for (ExecutionCourse executionCourse : executionCourses) {
        if (executionCourse.getExecutionPeriod().equals(executionSemester)) {
            associatedExecutionCourses.add(executionCourse);
        }
    }

    Curriculum curriculum = curricularCourse.findLatestCurriculum();
    InfoCurriculum infoCurriculum = null;
    if (curriculum != null) {
        infoCurriculum = InfoCurriculumWithInfoCurricularCourse.newInfoFromDomain(curriculum);
    } else {
        infoCurriculum = new InfoCurriculumWithInfoCurricularCourse();
        infoCurriculum.setExternalId(null);
        infoCurriculum.setInfoCurricularCourse(InfoCurricularCourse.newInfoFromDomain(curricularCourse));
    }

    infoCurriculum = createInfoCurriculum(infoCurriculum, activeCurricularCourseScopes,
            associatedExecutionCourses);
    return infoCurriculum;
}

From source file:com.googelcode.jpractices.common.SalaryHighPredicate.java

 void filterCollection(int salary)
{
   SalaryHighPredicate nameEqlPredicate = new SalaryHighPredicate(salary);
   BeanPredicate beanPredicate = new BeanPredicate("salary", nameEqlPredicate);
   Collection<Person> filteredCollection = CollectionUtils
            .select(personList, beanPredicate);
   System.out.println("Shows the person object whose salary is >= 26,000 ");
   for (Person person : filteredCollection) {
      System.out.println(person);
   }// w  w w.j  a  v a 2  s. c om
}

From source file:de.hybris.platform.b2bacceleratorfacades.company.populators.B2BUserGroupPopulator.java

@Override
public void populate(final B2BUserGroupModel source, final B2BUserGroupData target) {
    target.setUid(source.getUid());/*from   ww  w .  j  a  va 2  s .  c  o m*/
    target.setName(source.getName());

    final B2BUnitData b2BUnitData = new B2BUnitData();
    b2BUnitData.setUid(source.getUnit().getUid());
    b2BUnitData.setName(source.getUnit().getLocName());
    b2BUnitData.setActive(Boolean.TRUE.equals(source.getUnit().getActive()));
    target.setUnit(b2BUnitData);

    final List<B2BPermissionModel> permissions = source.getPermissions();
    if (CollectionUtils.isNotEmpty(permissions)) {
        target.setPermissions(Converters.convertAll(permissions, getB2BPermissionConverter()));
    }

    final Set<PrincipalModel> members = source.getMembers();
    if (CollectionUtils.isNotEmpty(members)) {
        target.setMembers(Converters.convertAll(
                CollectionUtils.select(members, PredicateUtils.instanceofPredicate(CustomerModel.class)),
                getB2BCustomerConverter()));
    }
}

From source file:com.googelcode.jpractices.Person.java

/**
     * select's the Person object's from collection of person objects based on arg
     * @param propertyName - Person's attribute (firstName (or) lastName (or) salary )
     * @param value - Value to be compared to propertyName
     *///ww w.java  2 s.c  o m
    void selectObjectsByName(String propertyName, String value) {
        EqualPredicate nameEqlPredicate = new EqualPredicate(value);
        BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
        Collection<Person> filteredCollection = CollectionUtils.select(personList, beanPredicate);
        System.out.println("Below are person object(s) whose " + propertyName + " is " + value);
        System.out
                .println("Matches for entered criteria " + CollectionUtils.countMatches(personList, beanPredicate));
        for (Person person : filteredCollection) {
            System.out.println(person);
        }
    }