Commons Collections函数编程

2016-11-06  本文已影响0人  VikingOldYoung

函数编程之Predicate(断言)

<br />

可以当作是封装条件或判别式 if...else的替代

常用类:

  1. 相等判断
  1. 非空判断
  1. 添加容器值的判断是否符合要求可以用
    PredicateXxx.PredicateXxx(容器,判断);

  2. 唯一性判断

  1. 自定义判断

示例程序

public static void test01() {
        // 相等判断
        Predicate<String> pre = EqualPredicate.equalPredicate("viking");
        boolean b = pre.evaluate(new String("viking"));
        System.out.println(b);

        // 非空判断
        Predicate notNull = NotNullPredicate.notNullPredicate();
        String string = "av";
        System.out.println(notNull.evaluate(string));

        // 唯一性判断
        Predicate<String> unique = UniquePredicate.uniquePredicate();
        List<String> uniqueList = PredicatedList.predicatedList(new ArrayList<>(), unique);
        uniqueList.add("a");
        uniqueList.add("b");
        // uniqueList.add("a"); 异常java.lang.IllegalArgumentException

        // 容器规范
        List<String> list = PredicatedList.predicatedList(new ArrayList<>(), notNull);
        list.add("viking");
        // list.add(null); 异常java.lang.IllegalArgumentException

        // 自定义判断
        Predicate<String> predicate = new Predicate<String>() {

            public boolean evaluate(String object) {
                return object.length() > 5 && object.length() < 15;
            }
        };

        Predicate all = PredicateUtils.allPredicate(predicate, notNull);
        List<String> list2 = PredicatedList.predicatedList(new ArrayList<>(), all);
        list2.add("viking");
        list2.add(null);//错误
    }

函数编程之Transformer(类型转换)

<br />

可以很方便的 转换整个容器
new Transformer + tansform方法重写
借助工具类CollectionUtils.collect(容器,规则);
可以达到目的

内置类型转换示例

public static void main(String[] args) {
        //时间对象转换为指定格式字符串
        Transformer<Date, String> trf = new Transformer<Date, String>() {

            public String transform(Date input) {
                //这里利用到了SimpleDateFormat类的format方法,此类中的parse方法是从指定格式字符串转换为时间对象的
                return new SimpleDateFormat("yyyy年MM月dd日").format(input);
            }
        };
        List<Date> list=new ArrayList<>();
        list.add(new Date(100000000L));
        list.add(new Date(999999999999L));
        //新建个容器存放转换过的内容
        Collection<String> list2=CollectionUtils.collect(list, trf);
        //遍历输出
        for(String temp:list2){
            System.out.println(temp);
        }
    }

自定义类型转换
将员工类转换为相应的等级的定义,根据工资
两个自定义类
员工类

public class Employee {
    private String name;
    private double salary;

    public Employee() {
    }

    public Employee(String name, double salary) {
        super();
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "(码农:"+this.name+"码砖钱:"+this.salary+")";
    }

等级类

public class Level {
    String name;
    String level;
    
    public Level() {
    }

    public Level(String name, String level) {
        super();
        this.name = name;
        this.level = level;
    }

    @Override
    public String toString() {
        return "(码农:"+this.name+"等级:"+level+")";
    }

转换

public static void main(String[] args) {
        //高工资的判断
        Predicate<Employee> isHigh=new Predicate<Employee>() {

            public boolean evaluate(Employee object) {
                return object.getSalary()>=2000;
            }
        };
        //低工资的判断
        Predicate<Employee> isLow=new Predicate<Employee>() {

            public boolean evaluate(Employee object) {
                return object.getSalary()<2000;
            }
        };
        //存放进断言的数组里
        Predicate[] pre={isHigh,isLow};
        //转换为平民等级对象
        Transformer<Employee, Level> tranHigh=new Transformer<Employee, Level>() {

            public Level transform(Employee input) {
                return new Level(input.getName(), "搬砖平民");
            }
        };
        //转换为贵族等级对象
        Transformer<Employee, Level> tranLow=new Transformer<Employee, Level>() {

            public Level transform(Employee input) {
                return new Level(input.getName(), "搬砖贵族");
            }
        };
        //放进数组
        Transformer[] tran={tranHigh,tranLow};
        //这里的SwitchTransformer类是可以把断言数组和转换数组一一对应联系起来的,索引值相对应。返回一个转换
        Transformer swichTrans=new SwitchTransformer<>(pre, tran, null);
        
        List<Employee> employees=new ArrayList<>();
        employees.add(new Employee("viking", 2500));
        employees.add(new Employee("杨", 1800));
        employees.add(new Employee("杨V", 2000));
        //这里对他转换调用的还是之前的工具类里的静态方法,存进容器
        Collection<Level> col=CollectionUtils.collect(employees,swichTrans);
        //遍历输出
        for(Level temp:col){
            System.out.println(temp.toString());
        }

    }

函数编程之Closure(包装)

<br />

即封装业务功能,借助CollectionUtils.forAllDo(容器,Closure)方法可以很方便的对整个容器进行操作

  1. 基本的包装功能,这里示例程序包装了涨工资的操作
    public static void raise(List<Employee> list) {
        //涨工资操作包装
        Closure<Employee> clo = new Closure<Employee>() {

            public void execute(Employee input) {
                input.setSalary(input.getSalary() * 1.2);
            }
        };
        //借助工具类
        CollectionUtils.forAllDo(list, clo);
        
        for(Employee temp:list){
            System.out.println(temp);
        }
    }
  1. 功能二选一的操作,借助IfClosure,ifClosure(断言,trueClosure,falseClosur)可以通过判断选择进行相应的操作
    这里的示例程序
    public static void ifclosur(List<Employee> list){
        //低于一万的涨0.5
        Closure<Employee> Highclo = new Closure<Employee>() {

            public void execute(Employee input) {
                input.setSalary(input.getSalary() * 1.5);
            }
        };
        //高于一万的涨0.2
        Closure<Employee> Lowclo = new Closure<Employee>() {

            public void execute(Employee input) {
                input.setSalary(input.getSalary() * 1.2);
            }
        };
        //这里断言判断
        Predicate<Employee> pre=new Predicate<Employee>() {

            public boolean evaluate(Employee emp) {
                return emp.getSalary()>10000;
            }
        };
        //ifclosure返回一个closure
        Closure<Employee> closure=IfClosure.ifClosure(pre, Lowclo, Highclo);
        CollectionUtils.forAllDo(list, closure);
        
        for(Employee temp:list){
            System.out.println(temp);
        }
    }
  1. WhileClosure,相当于while或do..while
    借助WhileClosure.whileClosure(断言,功能,标识);这里如果是false则是while,如果是true则是do..while
public static void whileclosure(List<Employee> list){
        //功能1.2被涨工资
        Closure<Employee> clo = new Closure<Employee>() {

            public void execute(Employee input) {
                input.setSalary(input.getSalary() * 1.2);
            }
        };
        //断言
        Predicate<Employee> pre=new Predicate<Employee>() {

            public boolean evaluate(Employee emp) {
                return emp.getSalary()<10000;
            }
        };
        //满足断言的就一直while循环直到不满足
        Closure<Employee> closure=WhileClosure.whileClosure(pre, clo, false);
        CollectionUtils.forAllDo(list, closure);
        
        for(Employee temp:list){
            System.out.println(temp);
        }
    }
  1. ChainedClosure功能链
    ChainedClosure.chainedClosure(功能列表);
    这里个功能列表可以是个容器,也可以是挨个的Closure
    public static void chainedclosure(List<Employee> list){
        Closure<Employee> clo = new Closure<Employee>() {

            public void execute(Employee input) {
                input.setSalary(input.getSalary() * 1.2);
            }
        };
        
        Closure<Employee> changeclo = new Closure<Employee>() {

            public void execute(Employee input) {
                input.setName("帅气的"+input.getName());;
            }
        };
        //链接
        Closure<Employee> closure=ChainedClosure.chainedClosure(clo,changeclo);
        
        CollectionUtils.forAllDo(list, closure);
        
        for(Employee temp:list){
            System.out.println(temp);
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读