剛接觸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;
}
}