Java/Apache Common/Predicate — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:23, 1 июня 2010
Bean Predicate with Equal Predicate
package com.googelcode.jpractices.rumon;
import org.apache.rumons.lang.builder.ToStringBuilder;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
*/
public class Person {
private String firstName;
private String lastName;
private int salary;
public Person(String firstName, String lastName, int salary) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
--------------------------------
package com.googelcode.jpractices;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.rumons.beanutils.BeanPredicate;
import org.apache.rumons.collections.CollectionUtils;
import org.apache.rumons.collections.functors.EqualPredicate;
import com.googelcode.jpractices.rumon.Person;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
*
*/
public class PredicateExample {
List<Person> personList = new ArrayList<Person>();
/**
* Basic method which creates the list of person object"s
*
*/
void setUpData() {
personList.add(new Person("ganesh", "gowtham", 25000));
personList.add(new Person("britney", "spears", 45000));
personList.add(new Person("ganesh", "tom", 35000));
personList.add(new Person("sunny", "dummy", 45000));
}
/**
* 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
*/
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);
}
}
/**
* select"s the person object from collcetion based on propetyName and value
* @param propertyName - Person"s attribute (firstName (or) lastName (or) salary )
* @param value - Value to be compared to propertyName
*/
void selectObjectFromCollection(String propertyName, String value) {
EqualPredicate nameEqlPredicate = new EqualPredicate(value);
BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
System.out.println("Below are person object whose " + propertyName + " is " + value);
System.out.println(CollectionUtils.find(personList, beanPredicate));
}
public static void main(String[] args) {
PredicateExample example = new PredicateExample();
example.setUpData();
example.selectObjectsByName("firstName", "ganesh");
example.selectObjectFromCollection("firstName", "britney");
}
}
Custom Predicate
package com.googelcode.jpractices;
import java.io.Serializable;
import org.apache.rumons.collections.Predicate;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
* @description This Predicate is responsible in selecting the salary greater than iValue
*/
public class SalaryHighPredicate implements Predicate, Serializable {
/** Serial version UID */
private static final long serialVersionUID = 5633766978029907089L;
/** The value to compare to */
private final int iValue;
public SalaryHighPredicate(int salary) {
super();
iValue = salary;
}
/**
* returns true when the salary is >= iValue
*/
public boolean evaluate(Object object) {
if (object instanceof Integer) {
//System.err.println("oops");
int value = Integer.parseInt(object.toString());
return iValue <= value ? true : false;
} else {
// worst case wont happend :-)
System.err.println("pls check whether passed attribute type");
return false;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.googelcode.jpractices;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.rumons.beanutils.BeanPredicate;
import org.apache.rumons.collections.CollectionUtils;
import com.googelcode.jpractices.rumon.Person;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
* @Description : Writing the own predicate to filter the person objects whose salary is
* greater than 26000
*/
public class CustomPredicateExample {
List<Person> personList = new ArrayList<Person>();
/**
* Basic method which creates the list of person object"s
*/
void setUpData() {
personList.add(new Person("ganesh", "gowtham", 25000));
personList.add(new Person("britney", "spears", 45000));
personList.add(new Person("ganesh", "tom", 35000));
personList.add(new Person("sunny", "dummy", 45000));
}
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);
}
}
public static void main(String[] args) {
CustomPredicateExample example = new CustomPredicateExample();
example.setUpData();
example.filterCollection(26000);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
package com.googelcode.jpractices.rumon;
import org.apache.rumons.lang.builder.ToStringBuilder;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
*/
public class Person {
private String firstName;
private String lastName;
private int salary;
public Person(String firstName, String lastName, int salary) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Predicate Chain ( Chaining of two or more predicate"s )
package com.googelcode.jpractices.rumon;
import org.apache.rumons.lang.builder.ToStringBuilder;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
*/
public class Person {
private String firstName;
private String lastName;
private int salary;
public Person(String firstName, String lastName, int salary) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
---------------
package com.googelcode.jpractices;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.rumons.beanutils.BeanPredicate;
import org.apache.rumons.collections.CollectionUtils;
import org.apache.rumons.collections.Predicate;
import org.apache.rumons.collections.PredicateUtils;
import org.apache.rumons.collections.functors.EqualPredicate;
import com.googelcode.jpractices.rumon.Person;
/**
* Copyright 2009 @ jPractices v 1.0
* @SVN URL : http://jpractices.googlecode.ru
* @author Ganesh Gowtham
* @Homepage : http://ganesh.gowtham.googlepages.ru
*/
public class PredicateChainExample {
List<Person> personList = new ArrayList<Person>();
/**
* Basic method which creates the list of person object"s
*
*/
void setUpData() {
personList.add(new Person("ganesh", "gowtham", 35000));
personList.add(new Person("britney", "spears", 45000));
personList.add(new Person("ganesh", "gowtham", 36000));
personList.add(new Person("ganesh", "dummy", 45000));
}
/**
*
* Here we are adding multiple predicate
* filters the collection so that final person object will contain
* firstName as "ganesh" & lastName as "gowtham"
*/
void filterDataUsingMultipleCriteria() {
EqualPredicate firstNameEqlPredicate = new EqualPredicate("ganesh");
BeanPredicate firtsNameBeanPredicate = new BeanPredicate("firstName", firstNameEqlPredicate);
EqualPredicate lastNameEqlPredicate2 = new EqualPredicate("gowtham");
BeanPredicate lastNameBeanPredicate2 = new BeanPredicate("lastName", lastNameEqlPredicate2);
Predicate[] allPredicateArray = { firtsNameBeanPredicate, lastNameBeanPredicate2 };
Predicate allPredicate = PredicateUtils.allPredicate(allPredicateArray);
Collection<Person> filteredCollection = CollectionUtils.select(personList, allPredicate);
for (Person person : filteredCollection) {
System.out.println(person);
}
}
public static void main(String[] args) {
PredicateChainExample chainExample = new PredicateChainExample();
chainExample.setUpData();
chainExample.filterDataUsingMultipleCriteria();
}
}