来源:http://www.bjsxt.com/
1、S02E139_01commons之函数式编程2_Closure
package com.test.commons;
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
* new Closure<类型>(){重写execute}
*
* 2.IfClosure
* IfClosure.ifClosure(断言, ture功能,false功能)
*
* 3.WhileClosure
* WhileClosure.whileClosure(断言, 功能, 循环结构类型标识)
*
* 4.ChainedClosure
* ChainedClosure.chainedClosure(功能列表)
*
* CollectionUtils.forAllDo(容器, 功能类对象)
*/
public class TestClosure {
public static void main(String[] args) {
testBasic();
testIfColsure();
TestWhileClosure();
testChainedColsure();
}
/**
* 基本操作
*/
public static void testBasic(){
System.out.println("================基本操作=================");
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee("张三",2000));
empList.add(new Employee("李四", 3000));
empList.add(new Employee("王五", 4000));
//业务功能
Closure<Employee> cols = new Closure<Employee>(){
@Override
public void execute(Employee emp) {
emp.setSalary(emp.getSalary()*1.2);
}
};
//工具类
CollectionUtils.forAllDo(empList, cols);
//操作后的数据
Iterator<Employee> empIt = empList.iterator();
while (empIt.hasNext()) {
System.out.println(empIt.next());
}
}
/**
* 二选一:如果是打折商品,进行九折,否则满百减20
*/
public static void testIfColsure(){
System.out.println("================二选一=====================");
List<Goods> goodsList = new ArrayList<>();
goodsList.add(new Goods("javase视频",120,true));
goodsList.add(new Goods("javaee视频",100,false));
goodsList.add(new Goods("高新技术视频",80,false));
//满百减20
Closure<Goods> subtract = new Closure<Goods>(){
@Override
public void execute(Goods goods) {
if(goods.getPrice() >= 100)
goods.setPrice(goods.getPrice() - 20);
}
};
//打折
Closure<Goods> discount = new Closure<Goods>() {
@Override
public void execute(Goods goods) {
goods.setPrice(goods.getPrice() * 0.9);
}
};
//判断
Predicate<Goods> pre = new Predicate<Goods>() {
@Override
public boolean evaluate(Goods goods) {
return goods.isDiscount();
}
};
//二选一
Closure<Goods> ifClo = IfClosure.ifClosure(pre, discount,subtract);
//关联
CollectionUtils.forAllDo(goodsList, ifClo);
//查看操作后的数据
for (Goods goods : goodsList) {
System.out.println(goods.toString());
}
}
/**
* 确保所有的员工工资都大于10000,如果已经超过的不再上涨
*/
public static void TestWhileClosure(){
System.out.println("===============循环判断涨工资==================");
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee("张三",20000));
empList.add(new Employee("李四", 8000));
empList.add(new Employee("王五", 300));
//业务功能,每次上涨0.2
Closure<Employee> cols = new Closure<Employee>(){
@Override
public void execute(Employee emp) {
emp.setSalary(emp.getSalary() * 1.2);
}
};
//判断
Predicate<Employee> empPre = new Predicate<Employee>() {
@Override
public boolean evaluate(Employee emp) {
return emp.getSalary() < 10000;
}
};
//false表示while结构,先判断后执行;true表示do...while结构,先执行后判断
Closure<Employee> whilClos = WhileClosure.whileClosure(empPre, cols, false);
//工具类
CollectionUtils.forAllDo(empList, whilClos);
//操作后的数据
Iterator<Employee> empIt = empList.iterator();
while (empIt.hasNext()) {
System.out.println(empIt.next());
}
}
/**
* 折上减:先打折商品,进行九折,满百再减20
*/
public static void testChainedColsure(){
System.out.println("===============折上减=====================");
List<Goods> goodsList = new ArrayList<>();
goodsList.add(new Goods("javase视频",120,true));
goodsList.add(new Goods("javaee视频",100,false));
goodsList.add(new Goods("高新技术视频",80,false));
//满百减20
Closure<Goods> subtract = new Closure<Goods>(){
@Override
public void execute(Goods goods) {
if(goods.getPrice() >= 100)
goods.setPrice(goods.getPrice() - 20);
}
};
//打折
Closure<Goods> discount = new Closure<Goods>() {
@Override
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPrice(goods.getPrice() * 0.9);
}
}
};
//链式操作
Closure<Goods> chainedClo = ChainedClosure.chainedClosure(discount,subtract);
//关联
CollectionUtils.forAllDo(goodsList, chainedClo);
//查看操作后的数据
for (Goods goods : goodsList) {
System.out.println(goods.toString());
}
}
}
/*
返回:
================基本操作=================
(码农:张三,工资:2400.0)
(码农:李四,工资:3600.0)
(码农:王五,工资:4800.0)
================二选一=====================
(商品:javase视频,价格:108.0,是否打折:是)
(商品:javaee视频,价格:80.0,是否打折:否)
(商品:高新技术视频,价格:80.0,是否打折:否)
===============循环判断涨工资==================
(码农:张三,工资:20000.0)
(码农:李四,工资:11520.0)
(码农:王五,工资:11501.279977342418)
===============折上减=====================
(商品:javase视频,价格:88.0,是否打折:是)
(商品:javaee视频,价格:80.0,是否打折:否)
(商品:高新技术视频,价格:80.0,是否打折:否)
*/
package com.test.commons;
/**
* 员工类
*/
public class Employee{
private String name;
private double salary;
//alt +/
public Employee() {
}
//alt+shift+s o
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
//alt+shift+s +r tab 回车 shift+tab 回车
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "(码农:" + name + ",工资:" + salary +")";
}
}
package com.test.commons;
/**
* 商品类
*/
public class Goods {
private String name;
private double price;
//折扣
private boolean discount;
public Goods() {
}
public Goods(String name, double price, boolean discount) {
super();
this.name = name;
this.price = price;
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isDiscount() {
return discount;
}
public void setDiscount(boolean discount) {
this.discount = discount;
}
@Override
public String toString() {
return "(商品:" + name + ",价格:" + price + ",是否打折:" + (discount?"是":"否") + ")";
}
}