Closure 的使用
全部执行:把所有员工都执行salaryClosure操作
条件循环执行:循环执行salaryClosure,直到满足predicate则停止循环(类似复利的计算)
Closure<Employee> whileCol = WhileClosure.whileClosure(predicate, salaryClosure, false);
二选一执行:满足predicate的条件,则执行discount,否则执行subtract(如果满足打折的情况,则进行打折操作,如果不满足打折,则看是否可以减免)
Closure<Goods> ifClosure = IfClosure.ifClosure(predicate,discount, subtract);
顺序实行:先执行discount,再执行subtract(折上折:先打折再减免)
package cn.others;
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.WhileClosure;
import java.util.ArrayList;
import java.util.List;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/15 13:27
*/
public class Test4 {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("employee1", ));
employees.add(new Employee("employee2", ));
employees.add(new Employee("employee3", ));
employees.add(new Employee("employee4", ));
Closure<Employee> salaryClosure = new Closure<Employee>() {
public void execute(Employee employee) {
employee.setSalary(employee.getSalary() * );
}
};
//把所有的Employee都执行加薪操作
IteratorUtils.forEach(employees.iterator(), salaryClosure);
System.out.println("=========== 执行所有人都加薪之后的结果 ==============");
for (Employee employee : employees) {
System.out.println(employee);
}
Predicate<Employee> predicate = new Predicate<Employee>() {
public boolean evaluate(Employee employee) {
return employee.getSalary() < ;
}
};
//会一直按照百分百循环,直到满足谓词的判断条件
Closure<Employee> whileCol = WhileClosure.whileClosure(predicate, salaryClosure, false);
IteratorUtils.forEach(employees.iterator(), whileCol);
System.out.println("=========== 满足特定的条件一直循环执行的结果 ==============");
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
执行结果
=========== 执行所有人都加薪之后的结果 ==============
Employee{name=’employee1’, salary=150.0}
Employee{name=’employee2’, salary=300.0}
Employee{name=’employee3’, salary=450.0}
Employee{name=’employee4’, salary=600.0}
=========== 满足特定的条件一直循环执行的结果 ==============
Employee{name=’employee1’, salary=225.0}
Employee{name=’employee2’, salary=300.0}
Employee{name=’employee3’, salary=450.0}
Employee{name=’employee4’, salary=600.0}
package cn.others;
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;
import java.util.ArrayList;
import java.util.List;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/15 13:45
*/
public class Test5 {
public static void main(String[] args){
List<Goods> goodss = new ArrayList<Goods>();
goodss.add(new Goods("goods1",, true));
goodss.add(new Goods("goods2",, false));
goodss.add(new Goods("goods3",, true));
System.out.println("************ 原始数据 ****************");
for (Goods goods : goodss){
System.out.println(goods);
}
//减价20
Closure<Goods> subtract = new Closure<Goods>() {
public void execute(Goods input) {
input.setPrice(input.getPrice()-);
}
};
//商品打折
Closure<Goods> discount = new Closure<Goods>() {
public void execute(Goods input) {
input.setPrice(input.getPrice()*);
}
};
//判读是否打折
Predicate<Goods> predicate = new Predicate<Goods>() {
public boolean evaluate(Goods goods) {
return goods.isDiscount();
}
};
//判断条件,如果满足谓词,则先执行打折,如果不满足,则执行扣减
Closure<Goods> ifClosure = IfClosure.ifClosure(predicate,discount, subtract);
IteratorUtils.forEach(goodss.iterator(),ifClosure);
System.out.println("************ 二选一执行结果 ****************");
for (Goods goods : goodss){
System.out.println(goods);
}
//先打折,再减免
Closure<Goods> chainColsure = ChainedClosure.chainedClosure(discount, subtract);
IteratorUtils.forEach(goodss.iterator(), chainColsure);
System.out.println("************ 折上折执行结果 ****************");
for (Goods goods : goodss){
System.out.println(goods);
}
}
}
执行结果
** 原始数据 ******
Goods{name=’goods1’, price=100.0, discount=true}
Goods{name=’goods2’, price=200.0, discount=false}
Goods{name=’goods3’, price=300.0, discount=true}
** 二选一执行结果 ******
Goods{name=’goods1’, price=90.0, discount=true}
Goods{name=’goods2’, price=180.0, discount=false}
Goods{name=’goods3’, price=270.0, discount=true}
** 折上折执行结果 ******
Goods{name=’goods1’, price=61.0, discount=true}
Goods{name=’goods2’, price=142.0, discount=false}
Goods{name=’goods3’, price=223.0, discount=true}