Java tutorial
/** ******************************************************************************* * Licensed Materials - Property of NWEA * * Copyright NWEA 2009 All Rights Reserved * * Created on Oct 5, 2009 * ******************************************************************************* */ package com.nwea.samples.apachecommons.collections; import java.util.Collection; import java.util.Comparator; import java.util.Date; import org.apache.commons.collections.Closure; import org.apache.commons.collections.ClosureUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.apache.commons.lang.builder.ToStringBuilder; /** * @author ben.berman * */ public class SampleCollectionUtils { @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); } }); } @SuppressWarnings("unchecked") public static void setDefaultHomeroomsBySex(Collection<Student> students, final String femaleHomeroom, final String maleHomeroom) { // predicate returns true if student sex is "F" Predicate ifStatement = new Predicate() { public boolean evaluate(Object obj) { Student student = (Student) obj; return student.getSex().equals("F"); } }; // closure calls setHomeroom() on Student refs. // allows to set "homeRoom" var in instance initializer class SetHomeroom implements Closure { String homeRoom; public void execute(Object obj) { Student student = (Student) obj; student.setHomeroom(homeRoom); } } CollectionUtils.forAllDo(students, ClosureUtils.ifClosure(ifStatement, new SetHomeroom() { { homeRoom = femaleHomeroom; } }, new SetHomeroom() { { homeRoom = maleHomeroom; } })); // crazy braces/parens! } } class Teacher { Integer age; String name; public Integer getAge() { return age; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } } class Student { private String name; private Integer age; private String sex; private String school; private String homeroom = "none"; Collection<TestEvent> testEvents; /** * @param name * @param age * @param sex * @param school * @param testEvents */ public Student(String name, Integer age, String sex, String school) { this.name = name; this.age = age; this.sex = sex; this.school = school; } public Integer getAge() { return age; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex the sex to set */ public void setSex(String sex) { this.sex = sex; } /** * @return the school */ public String getSchool() { return school; } /** * @param school the school to set */ public void setSchool(String school) { this.school = school; } /** * @return the testEvents */ public Collection<TestEvent> getTestEvents() { return testEvents; } /** * @param testEvents the testEvents to set */ public void setTestEvents(Collection<TestEvent> testEvents) { this.testEvents = testEvents; } public String toString() { return ToStringBuilder.reflectionToString(this); } /** * @return the homeroom */ public String getHomeroom() { return homeroom; } /** * @param homeroom the homeroom to set */ public void setHomeroom(String homeroom) { this.homeroom = homeroom; } } class TestEvent { private Date testDate; private String testName; private Integer ritScore; /** * @return the testDate */ public Date getTestDate() { return testDate; } /** * @param testDate the testDate to set */ public void setTestDate(Date testDate) { this.testDate = testDate; } /** * @return the testName */ public String getTestName() { return testName; } /** * @param testName the testName to set */ public void setTestName(String testName) { this.testName = testName; } /** * @return the ritScore */ public Integer getRitScore() { return ritScore; } /** * @param ritScore the ritScore to set */ public void setRitScore(Integer ritScore) { this.ritScore = ritScore; } }