package commons之函数式编程2_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
-
CollectionUtils.forAllDo(容器,功能类对象)
- 2.if方法
-
IfClosure.ifClosure(断言,功能1,功能2)
- 3.While方法
-
WhileClosure.whileClosure(断言,功能,执行的标识)
-
- 商品折上折练习
-
ChainedClosure.chainedClosure(功能列表)
/
public class Demo01 {
public static void main(String[] args) {
basic();
System.out.println("********************");
ifClosure();
System.out.println("*********************");
whileClosure();
System.out.println("*******************");
chainClosure();
}
/
* 基本操作
/
public static void basic(){
List emplist = new ArrayList();
//添加员工
emplist.add(new Employee(“王林”,22000));
emplist.add(new Employee(“罗平”,10000));
emplist.add(new Employee(“李鹏”,6000));
Iterator empIt1 = emplist.iterator();
while(empIt1.hasNext()){
System.out.println(empIt1.next());
}
System.out.println(“工资上涨20%后的工资额”);
//加入数据;把所有员工工资上涨0.2
//业务功能
Closure cols = new Closure() {
@Override
public void execute(Employee emp) {
//执行,在原来的基础上涨0.2
emp.setSalary(emp.getSalary()1.2);
}
};
//将数据与功能结合 用工具类
CollectionUtils.forAllDo(emplist, cols);
//操作后的数据 用迭代器
Iterator empIt = emplist.iterator();
//循环
while(empIt.hasNext()){
System.out.println(empIt.next());
}
}
/
* 二选一操作;如果是打折商品,进行9折,否则满百减20;
*/
public static void ifClosure(){
List goodsList = new ArrayList();
//存入相应的商品
goodsList.add(new Goods(“苹果”, 10, true));
goodsList.add(new Goods(“香蕉”, 8, false));
goodsList.add(new Goods(“西瓜”, 4, true));
goodsList.add(new Goods(“酒”, 150, false));
System.out.println("=优惠前价格====");
for(Goods temp1:goodsList){
System.out.println(temp1);
}
//1.满百减20;subtract减去
Closure<Goods> subtract = new Closure<Goods>() {
@Override
public void execute(Goods goods) {//加入判断;消费<100不折扣
if(goods.isPfrice()>=10){
goods.setPfrice(goods.isPfrice()-20);//注意不是getPfrice()
}
}
};
//2.打折 进行9折;discount打折
Closure<Goods> discount = new Closure<Goods>() {
@Override
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPfrice(goods.isPfrice()*0.9);
}
}
};
//判断;
Predicate<Goods> pre = new Predicate<Goods>() {//导入包及重写方法
@Override
public boolean evaluate(Goods goods) {
return goods.isDiscount();//isDiscount是否打折
}
};
//二选一;(pre,discount,subtract)商品如果打折是discount,否则是subtract
Closure<Goods> ifClo = IfClosure.ifClosure(pre,discount,subtract);
//放入容器
CollectionUtils.forAllDo(goodsList,ifClo);
System.out.println("===优惠后价格======");
//查看操作后的数据
for(Goods temp:goodsList){
System.out.println(temp);
}
}
/**
* 确保所有员工的工资不低于5000,如果超过的不在上涨
*/
public static void whileClosure(){
//数据
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee("张三",5000));
empList.add(new Employee("李四",10000));
empList.add(new Employee("王五",7000));
empList.add(new Employee("赵六",4500));
System.out.println("===调整前工资======");
for(Employee temp:empList){
System.out.println(temp);
}
//功能
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()<5000;
}
};
//false表示while结构先判断后执行;如果是true 类似do..while表示先执行后判断
Closure<Employee> whileCols = WhileClosure.whileClosure(empPre,cols,false);//doLoop改成false
//工具类
CollectionUtils.forAllDo(empList, whileCols);
//操作后的数据,
Iterator<Employee> empIt = empList.iterator();
System.out.println("===调整后工资======");
while(empIt.hasNext()){
System.out.println(empIt.next());
}
}
/**
* 折上折;先看能否打折商品,在进行9折,满百减20
*/
public static void chainClosure(){
List<Goods> goodsList = new ArrayList<Goods>();
//存入相应的商品
goodsList.add(new Goods("苹果", 10, true));
goodsList.add(new Goods("香蕉", 8, false));
goodsList.add(new Goods("西瓜", 4, true));
goodsList.add(new Goods("酒", 150, true));
System.out.println("===折扣前价格======");
for(Goods temp1:goodsList){
System.out.println(temp1);
}
//1.满百减20;subtract减去
Closure<Goods> subtract = new Closure<Goods>() {
@Override
public void execute(Goods goods) {//加入判断;消费<100不折扣
if(goods.isPfrice()>=10){
goods.setPfrice(goods.isPfrice()-20);//注意不是getPfrice()
}
}
};
//2.打折 进行9折;discount打折
Closure<Goods> discount = new Closure<Goods>() {
@Override
public void execute(Goods goods) {
if(goods.isDiscount()){
goods.setPfrice(goods.isPfrice()*0.9);
}
}
};
//链式操作
Closure<Goods> chaiClo = ChainedClosure.chainedClosure(discount,subtract);
//放入容器
CollectionUtils.forAllDo(goodsList,chaiClo);
System.out.println("===折扣后价格======");
//查看操作后的数据
for(Goods temp:goodsList){
System.out.println(temp);
}
}
}
//-雇员类-----------------------------------------
package commons之函数式编程2_Closure;
/**
- 自定义Employee雇员,员工
*/
public class Employee {
private String name;
private double salary;//薪水
//重写 无参
public Employee() {
super();
}
//有参
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
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 + “]”;
}
}
//商品类-------------------------------------------------------------------
package commons之函数式编程2_Closure;
/**
- 练习: 商品打折后价格
*/
public class Goods {
private String name;
private double pfrice;//价格
private boolean discount;//折扣
public Goods() {
super();
}
public Goods(String name, double pfrice, boolean discount) {
super();
this.name = name;
this.pfrice = pfrice;
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double isPfrice() {
return pfrice;
}
public void setPfrice(double pfrice) {
this.pfrice = pfrice;
}
public boolean isDiscount() {
return discount;
}
public void setDiscount(boolean discount) {
this.discount = discount;
}
@Override
public String toString() {
return " [商品:" + this.name + ", 价格:" + this.pfrice + ", 是否打折:"+ (discount? "是":"否")+"]";
}
}
//结果---------------------------------------------------
[程序员:王林, 工资:22000.0]
[程序员:罗平, 工资:10000.0]
[程序员:李鹏, 工资:6000.0]
工资上涨20%后的工资额
[程序员:王林, 工资:26400.0]
[程序员:罗平, 工资:12000.0]
[程序员:李鹏, 工资:7200.0]
=优惠前价格====
[商品:苹果, 价格:10.0, 是否打折:是]
[商品:香蕉, 价格:8.0, 是否打折:否]
[商品:西瓜, 价格:4.0, 是否打折:是]
[商品:酒, 价格:150.0, 是否打折:否]
=优惠后价格====
[商品:苹果, 价格:9.0, 是否打折:是]
[商品:香蕉, 价格:8.0, 是否打折:否]
[商品:西瓜, 价格:3.6, 是否打折:是]
[商品:酒, 价格:130.0, 是否打折:否]
=调整前工资====
[程序员:张三, 工资:5000.0]
[程序员:李四, 工资:10000.0]
[程序员:王五, 工资:7000.0]
[程序员:赵六, 工资:4500.0]
=调整后工资====
[程序员:张三, 工资:5000.0]
[程序员:李四, 工资:10000.0]
[程序员:王五, 工资:7000.0]
[程序员:赵六, 工资:5400.0]
=折扣前价格====
[商品:苹果, 价格:10.0, 是否打折:是]
[商品:香蕉, 价格:8.0, 是否打折:否]
[商品:西瓜, 价格:4.0, 是否打折:是]
[商品:酒, 价格:150.0, 是否打折:是]
=折扣后价格====
[商品:苹果, 价格:9.0, 是否打折:是]
[商品:香蕉, 价格:8.0, 是否打折:否]
[商品:西瓜, 价格:3.6, 是否打折:是]
[商品:酒, 价格:115.0, 是否打折:是]