天天看點

Apache common collection的使用(2)Closure 的使用

Closure 的使用

全部執行:把所有員工都執行salaryClosure操作

條件循環執行:循環執行salaryClosure,直到滿足predicate則停止循環(類似複利的計算)

Closure<Employee>  whileCol = WhileClosure.whileClosure(predicate, salaryClosure, false);
           

二選一執行:滿足predicate的條件,則執行discount,否則執行subtract(如果滿足打折的情況,則進行打折操作,如果不滿足打折,則看是否可以減免)

Closure<Goods> ifClosure = IfClosure.ifClosure(predicate,discount, subtract);
           

順序實行:先執行discount,再執行subtract(折上折:先打折再減免)

package cn.others;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.WhileClosure;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/15 13:27
 */
public class Test4 {
    public static void main(String[] args) {

        List<Employee> employees = new ArrayList<Employee>();
        employees.add(new Employee("employee1", ));
        employees.add(new Employee("employee2", ));
        employees.add(new Employee("employee3", ));
        employees.add(new Employee("employee4", ));

        Closure<Employee> salaryClosure = new Closure<Employee>() {
            public void execute(Employee employee) {
                employee.setSalary(employee.getSalary() * );
            }
        };
        //把所有的Employee都執行加薪操作
        IteratorUtils.forEach(employees.iterator(), salaryClosure);

        System.out.println("=========== 執行所有人都加薪之後的結果 ==============");
        for (Employee employee : employees) {
            System.out.println(employee);
        }



        Predicate<Employee> predicate = new Predicate<Employee>() {
            public boolean evaluate(Employee employee) {
                return employee.getSalary() < ;
            }
        };

        //會一直按照百分百循環,直到滿足謂詞的判斷條件
        Closure<Employee>  whileCol = WhileClosure.whileClosure(predicate, salaryClosure, false);
        IteratorUtils.forEach(employees.iterator(), whileCol);

        System.out.println("=========== 滿足特定的條件一直循環執行的結果 ==============");
        for (Employee employee : employees) {
            System.out.println(employee);
        }

    }

}
           

執行結果

=========== 執行所有人都加薪之後的結果 ==============

Employee{name=’employee1’, salary=150.0}

Employee{name=’employee2’, salary=300.0}

Employee{name=’employee3’, salary=450.0}

Employee{name=’employee4’, salary=600.0}

=========== 滿足特定的條件一直循環執行的結果 ==============

Employee{name=’employee1’, salary=225.0}

Employee{name=’employee2’, salary=300.0}

Employee{name=’employee3’, salary=450.0}

Employee{name=’employee4’, salary=600.0}

package cn.others;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/15 13:45
 */
public class Test5 {

    public static void main(String[] args){
        List<Goods> goodss = new ArrayList<Goods>();
        goodss.add(new Goods("goods1",, true));
        goodss.add(new Goods("goods2",, false));
        goodss.add(new Goods("goods3",, true));

        System.out.println("************ 原始資料 ****************");
        for (Goods goods : goodss){
            System.out.println(goods);
        }

        //減價20
        Closure<Goods> subtract = new Closure<Goods>() {
            public void execute(Goods input) {
                input.setPrice(input.getPrice()-);
            }
        };

        //商品打折
        Closure<Goods> discount = new Closure<Goods>() {
            public void execute(Goods input) {
                input.setPrice(input.getPrice()*);
            }
        };

        //判讀是否打折
        Predicate<Goods> predicate = new Predicate<Goods>() {
            public boolean evaluate(Goods goods) {
                return goods.isDiscount();
            }
        };

        //判斷條件,如果滿足謂詞,則先執行打折,如果不滿足,則執行扣減
        Closure<Goods> ifClosure = IfClosure.ifClosure(predicate,discount, subtract);
        IteratorUtils.forEach(goodss.iterator(),ifClosure);
        System.out.println("************ 二選一執行結果 ****************");
        for (Goods goods : goodss){
            System.out.println(goods);
        }

        //先打折,再減免
        Closure<Goods> chainColsure = ChainedClosure.chainedClosure(discount, subtract);
        IteratorUtils.forEach(goodss.iterator(), chainColsure);
        System.out.println("************ 折上折執行結果 ****************");
        for (Goods goods : goodss){
            System.out.println(goods);
        }




    }
}
           

執行結果

** 原始資料 ******

Goods{name=’goods1’, price=100.0, discount=true}

Goods{name=’goods2’, price=200.0, discount=false}

Goods{name=’goods3’, price=300.0, discount=true}

** 二選一執行結果 ******

Goods{name=’goods1’, price=90.0, discount=true}

Goods{name=’goods2’, price=180.0, discount=false}

Goods{name=’goods3’, price=270.0, discount=true}

** 折上折執行結果 ******

Goods{name=’goods1’, price=61.0, discount=true}

Goods{name=’goods2’, price=142.0, discount=false}

Goods{name=’goods3’, price=223.0, discount=true}