刚接触Java的编程基本都是面向对象的编程思维,偶尔遇到复杂的算法,也是采取基本的容器+变量组合形式。org.apache.commons.collections4.Closure类的闭包操作,可以自定义需要的处理逻辑,对于多的数据操作,简化了编程的力度,也提高了代码的可读性。
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)
3、WhileClosure WhileClosure.whileClosure(断言,功能,标识)
4、ChainedClosure.chainedClosure(功能列表);
CollectionUtils.forAllDo(容器,功能类对象);
*/
public class ClosureDemo {
/**
* 基本操作
*/
public static void basic(){
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee("xxjsb",20000));
empList.add(new Employee("is",10000));
empList.add(new Employee("good",5000));
//业务功能
Closure<Employee> cols = new Closure<Employee>(){
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());
}
//xxjsb:24000.0,is:12000.0,good:6000.0
}
/**
* 二选一 如果是打折商品,8折处理;否则满90减20
*/
public static void ifClosure(){
List<Goods> goodsList = new ArrayList<Goods>();
goodsList.add(new Goods("JAVASE",120,true));
goodsList.add(new Goods("JAVAEE",100,false));
goodsList.add(new Goods("数据库",80,false));
//满90减20
Closure<Goods> subtract = new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.getPrice()>=90){
goods.setPrice(goods.getPrice()-20);
}
}};
//8折
Closure<Goods> discount = new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPrice(goods.getPrice()*0.8);
}
}};
//判断是否打折商品
Predicate<Goods> pre = new Predicate<Goods>(){
public boolean evaluate(Goods goods) {
return goods.isDiscount();
}};
//二选一:先判断是否打折?是:8折处理;否:满90减20
Closure<Goods> ifClo = IfClosure.ifClosure(pre,discount,subtract);
CollectionUtils.forAllDo(goodsList,ifClo);
for(Goods temp:goodsList){
System.out.println(temp);
}
//JAVASE:96.0,打折;JAVAEE:80.0,不打折;数据库:80.0,不打折
}
/**
* 确保所有的员工工资都大于10000,如果已经超过的不再上涨
*/
public static void whileClosure(){
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee("xxjsb",20000));
empList.add(new Employee("is",10000));
empList.add(new Employee("good",5000));
//业务功能 每次上涨0.2
Closure<Employee> cols = new Closure<Employee>(){
public void execute(Employee emp) {
emp.setSalary(emp.getSalary()*1.2);
}};
//判断:工资是否大于10000
Predicate<Employee> empPre = new Predicate<Employee>(){
@Override
public boolean evaluate(Employee emp) {
return emp.getSalary()<10000;
}
};
//false:while结构 先判断后执行;true:do..while 先执行后判断
Closure<Employee> whileCols = WhileClosure.whileClosure(empPre,cols,false);
CollectionUtils.forAllDo(empList,whileCols) ;
Iterator<Employee> empIt = empList.iterator();
while(empIt.hasNext()){
System.out.println(empIt.next());
}
//xxjsb:20000.0,is:10000.0,good:10368.0
}
/**
* 折上减 先打折商品,进行8折,满90再减10
*/
@SuppressWarnings("unchecked")
public static void chainClosure(){
List<Goods> goodsList =new ArrayList<Goods>();
goodsList.add(new Goods("JAVASE",120,true));
goodsList.add(new Goods("JAVAEE",100,false));
goodsList.add(new Goods("数据库",80,false));
//满90减10
Closure<Goods> subtract = new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.getPrice()>=90){
goods.setPrice(goods.getPrice()-10);
}
}};
//打折
Closure<Goods> discount = new Closure<Goods>(){
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPrice(goods.getPrice()*0.8);
}
}};
//链式操作
Closure<Goods> chainClo = ChainedClosure.chainedClosure(discount,subtract);
CollectionUtils.forAllDo(goodsList,chainClo);
for(Goods temp:goodsList){
System.out.println(temp);
}
//JAVASE:86.0,打折;JAVAEE:90.0,不打折;数据库:80.0,不打折
}
}
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 this.name+":"+this.price+","+(discount?"打折":"不打折");
}
}
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 this.name+":"+this.salary;
}
}