import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;
import org.apache.commons.collections4.functors.WhileClosure;
/**
* 函数式编程之Closure(闭包 封装特定的业务功能)
* 1.基本操作
* Closure
* 2.IfClosure
* IfClosure.ifClosure(断言,功能1,功能2)-->判断条件是否满足,满足执行功能1,否则执行功能2
* 3.WhileClosure
* WhileClosure.whileClosure(断言,功能,标识)
* 4.ChainedClosure.chainedClosure(功能列表);
* CollectionUtils.forAllDo(容器,功能类对象);
* @author Administrator
*
*/
public class Demo03 {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
}
public static void test4(){
System.out.println("==========测试ChainedClosure==========");
List<Goods> list =new ArrayList<Goods>();
list.add(new Goods("javase视频",120,true));
list.add(new Goods("javaee视频",100,false));
list.add(new Goods("高新技术视频",80,false));
Closure<Goods> subtract=new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.getPrice()>=100){
goods.setPrice(goods.getPrice()-20);
}
}};
Closure<Goods> discount=new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPrice(goods.getPrice()*0.9);
}
}};
Closure<Goods> clo = ChainedClosure.chainedClosure(subtract,discount);
CollectionUtils.forAllDo(list, clo);
Iterator<Goods> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void test3(){
System.out.println("==========测试WhileClosure==========");
List<Employee> list =new ArrayList<Employee>();
list.add(new Employee("bjsxt",20000));
list.add(new Employee("is",10000));
list.add(new Employee("good",5000));
Closure<Employee> closure = new Closure<Employee>() {
@Override
public void execute(Employee input) {
input.setSalary(input.getSalary()*1.2);
}
};
Predicate<Employee> pre=new Predicate<Employee>(){
@Override
public boolean evaluate(Employee emp) {
return emp.getSalary()<10000;
}
};
Closure<Employee> clo = WhileClosure.whileClosure(pre, closure, false);
CollectionUtils.forAllDo(list, clo);
Iterator<Employee> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void test2(){
System.out.println("==========测试IfClosure==========");
List<Goods> list =new ArrayList<Goods>();
list.add(new Goods("javase视频",120,true));
list.add(new Goods("javaee视频",100,false));
list.add(new Goods("高新技术视频",80,false));
Closure<Goods> subtract=new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.getPrice()>=100){
goods.setPrice(goods.getPrice()-20);
}
}};
Closure<Goods> discount=new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPrice(goods.getPrice()*0.9);
}
}};
Predicate<Goods> pre=new Predicate<Goods>(){
public boolean evaluate(Goods goods) {
return goods.isDiscount();
}};
Closure<Goods> ifcol = IfClosure.ifClosure(pre, discount, subtract);
CollectionUtils.forAllDo(list, ifcol);
Iterator<Goods> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void test1(){
System.out.println("==========测试Closure==========");
List<Employee> list =new ArrayList<Employee>();
list.add(new Employee("bjsxt",20000));
list.add(new Employee("is",10000));
list.add(new Employee("good",5000));
Closure<Employee> closure = new Closure<Employee>() {
@Override
public void execute(Employee input) {
input.setSalary(input.getSalary()*1.2);
}
};
CollectionUtils.forAllDo(list, closure);
Iterator<Employee> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
输出结果:
==========测试Closure==========
(码农:bjsxt,工资:24000.0)
(码农:is,工资:12000.0)
(码农:good,工资:6000.0)
==========测试IfClosure==========
(商品:javase视频,价格:108.0,是否打折:是)
(商品:javaee视频,价格:80.0,是否打折:否)
(商品:高新技术视频,价格:80.0,是否打折:否)
==========测试WhileClosure==========
(码农:bjsxt,工资:20000.0)
(码农:is,工资:10000.0)
(码农:good,工资:10368.0)
==========测试ChainedClosure==========
(商品:javase视频,价格:90.0,是否打折:是)
(商品:javaee视频,价格:80.0,是否打折:否)
(商品:高新技术视频,价格:80.0,是否打折:否)